Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

jest.disableAutomock();

const { RuleTester } = require('eslint');
const validStyles = require('../src/stylex-valid-styles').default;
const nonstandardStyles =
require('../src/stylex-no-nonstandard-styles').default;

const tester = new RuleTester({
parser: require.resolve('hermes-eslint'),
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
});
const code = (body) => `import * as stylex from '@stylexjs/stylex'; ${body}`;

tester.run('computed property keys', validStyles, {
valid: [
code(
`const property = 'color'; stylex.create({root: {[property]: 'red'}});`,
),
code(`const property = 'color'; const alias = property;
stylex.create({root: {[alias]: 'red'}});`),
code(`const property = 'colro'; { const property = 'color';
stylex.create({root: {[property]: 'red'}}); }`),
code(`const property = 'color'; const alias = property;
function build(property) { return stylex.create({root: {[alias]: 'red'}}); }`),
code(`const property = 'color'; function build() { const alias = property;
{ const property = alias; return stylex.create({root: {[property]: 'red'}}); } }`),
code(`const property = 'color';
stylex.create({root: {[property]: {default: 'red', ':hover': 'blue'}}});`),
{
code: code(`const property = ('color' as const) satisfies string;
stylex.create({root: {[property]: 'red'}});`),
parser: require.resolve('@typescript-eslint/parser'),
},
],
invalid: [
{
code: code(`const property = 'color'; function build(property) {
return stylex.create({root: {[property]: 'red'}}); }`),
errors: [{ message: 'Computed key cannot depend on function argument' }],
},
{
code: code(`const property = 'color'; { const property = 'colro';
stylex.create({root: {[property]: 'red'}}); }`),
errors: [{ message: 'This is not a key that is allowed by stylex' }],
},
{
code: code(`let property = 'color'; property = 'width';
stylex.create({root: {[property]: 'red'}});`),
errors: [{ message: 'Computed key cannot be resolved.' }],
},
{
code: code(`const property = alias; const alias = property;
stylex.create({root: {[property]: 'red'}});`),
errors: [{ message: 'Computed key cannot be resolved.' }],
},
{
code: code(`const property = alias; const alias = property;
stylex.create({root: {[property]: {default: 'red'}}});`),
errors: [{ message: 'Keys must be strings' }],
},
{
code: code(`const property = getProperty();
stylex.create({root: {[property]: 'red'}});`),
errors: [{ message: 'Computed key cannot be resolved.' }],
},
],
});

tester.run('computed nonstandard property keys', nonstandardStyles, {
valid: [
code(`const property = 'float'; function build(property) {
return stylex.create({root: {[property]: 'start'}}); }`),
code(`const property = alias; const alias = property;
stylex.create({root: {[property]: {default: 'start'}}});`),
],
invalid: [
{
code: code(`const property = 'float';
stylex.create({root: {[property]: {default: 'start'}}});`),
output: code(`const property = 'float';
stylex.create({root: {[property]: {default: 'inline-start'}}});`),
errors: [
{
message: /inline-start/,
suggestions: [
{
desc: 'Replace "start" with "inline-start"?',
output: code(`const property = 'float';
stylex.create({root: {[property]: {default: 'inline-start'}}});`),
},
],
},
],
},
{
code: code(
`const property = 'float'; stylex.create({root: {[property]: 'start'}});`,
),
output: code(
`const property = 'float'; stylex.create({root: {[property]: 'inline-start'}});`,
),
errors: [
{
message: /inline-start/,
suggestions: [
{
desc: 'Replace "start" with "inline-start"?',
output: code(
`const property = 'float'; stylex.create({root: {[property]: 'inline-start'}});`,
),
},
],
},
],
},
],
});
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,30 @@ type SkipOptions = {
filter?: ((token: AST.Token) => boolean) | undefined,
};

export type Scope = {
set: $ReadOnlyMap<
string,
{
defs: $ReadOnlyArray<{
type: string,
node: ESTree.Node,
parent?: ?ESTree.Node,
...
}>,
references: $ReadOnlyArray<{
init?: boolean,
isWrite(): boolean,
...
}>,
...
},
>,
upper: ?Scope,
...
};

export type SourceCode = {
getScope(node: ESTree.Node): Scope,
lines: string[],
getTokensBetween(
left: $ReadOnly<{ ...ESTree.Property, ... }>,
Expand Down Expand Up @@ -116,11 +139,11 @@ export type RuleContext = {

// getDeclaredVariables(node: ESTree.Node): Scope.Variable[];

filename: string;
filename: string,

physicalFilename: string;
physicalFilename: string,

cwd: string;
cwd: string,

// getScope(): Scope.Scope;

Expand Down
10 changes: 7 additions & 3 deletions packages/@stylexjs/eslint-plugin/src/rules/isNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,20 @@ export const isNumber: RuleCheck = makeVariableCheckingRule(
}

if (node.type === 'Identifier' && context) {
// $FlowFixMe[prop-missing] Flow libdefs doesn't know SourceCode has `getScope`
let scope = getSourceCode(context).getScope(node);
while (scope.upper != null && !scope.set.has(node.name)) {
scope = scope.upper;
while (!scope.set.has(node.name)) {
const upper = scope.upper;
if (upper == null) {
break;
}
scope = upper;
}
const variable = scope.set.get(node.name);
const def = variable?.defs?.[0];

const isLocalConst =
def?.node?.type === 'VariableDeclarator' &&
def.parent?.type === 'VariableDeclaration' &&
def.parent?.kind === 'const';

return isLocalConst || variables?.get(node.name)?.type === 'number'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import isStylexResolvedVarsToken from './rules/isStylexResolvedVarsToken';
import isCSSVariable from './rules/isCSSVariable';
import evaluate from './utils/evaluate';
import resolveKey from './utils/resolveKey';
import getSourceCode from './utils/getSourceCode';
import {
CSSProperties,
convertToStandardProperties,
Expand Down Expand Up @@ -180,7 +181,7 @@ const stylexNoNonstandardStyles = {
: key.type === 'Identifier'
? !style.computed
? key.name
: resolveKey(key, variables)
: resolveKey(key, variables, getSourceCode(context))
: null;
if (isStylexResolvedVarsToken(key, stylexResolvedVarsTokenImports)) {
return;
Expand Down Expand Up @@ -218,9 +219,11 @@ const stylexNoNonstandardStyles = {
return;
}
if (style.computed && styleKey.type !== 'Literal') {
const val = evaluate(styleKey, variables);
const val = evaluate(styleKey, variables, getSourceCode(context));
if (val != null && val !== 'ARG') {
styleKey = val;
} else {
return;
}
}
const key =
Expand Down
68 changes: 39 additions & 29 deletions packages/@stylexjs/eslint-plugin/src/stylex-valid-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import isStylexResolvedVarsToken from './rules/isStylexResolvedVarsToken';
import isStylexEnvMember from './rules/isStylexEnvMember';
import isCSSVariable from './rules/isCSSVariable';
import evaluate from './utils/evaluate';
import resolveKey from './utils/resolveKey';
import formatPropertiesWithNodeIndentation from './utils/formatPropertiesWithNodeIndentation';
import getSourceCode from './utils/getSourceCode';
import {
Expand Down Expand Up @@ -662,22 +661,14 @@ const stylexValidStyles = {
message: 'You cannot nest styles more than one level deep',
} as Rule.ReportDescriptor);
}
const key = style.key;
let key: Expression | PrivateIdentifier = style.key;
if (key.type === 'PrivateIdentifier') {
return context.report({
node: key,
loc: key.loc,
message: 'Private properties are not allowed in stylex',
} as Rule.ReportDescriptor);
}
const keyName =
key.type === 'Literal'
? key.value
: key.type === 'Identifier'
? !style.computed
? key.name
: resolveKey(key, variables)
: null;
if (isStylexResolvedVarsToken(key, stylexResolvedVarsTokenImports)) {
return undefined;
}
Expand All @@ -691,6 +682,23 @@ const stylexValidStyles = {
),
);
}
if (style.computed && key.type !== 'Literal') {
const resolved = evaluate(key, variables, getSourceCode(context));
if (resolved == null || resolved === 'ARG') {
return context.report({
node: style.key,
loc: style.key.loc,
message: 'Keys must be strings',
} as Rule.ReportDescriptor);
}
key = resolved;
}
const keyName =
key.type === 'Literal'
? key.value
: key.type === 'Identifier'
? key.name
: null;
if (
typeof keyName !== 'string' ||
(key.type !== 'Literal' && key.type !== 'Identifier')
Expand Down Expand Up @@ -771,7 +779,7 @@ const stylexValidStyles = {
styleXEnvImports,
);
let isStylexWhenCall = false;
if (style.computed && styleKey.type !== 'Literal') {
if (style.computed && styleKey.type !== 'Literal' && !isStylexEnvKey) {
if (
styleKey.type === 'CallExpression' &&
styleKey.callee.type === 'MemberExpression'
Expand All @@ -789,24 +797,26 @@ const stylexValidStyles = {
(calleeObject.type === 'Identifier' &&
styleXWhenImports.has(calleeObject.name) &&
calleeProperty.type === 'Identifier');

if (!isStylexWhenCall) {
const val = evaluate(styleKey, variables);
if (val == null) {
return context.report({
node: style.key,
loc: style.key.loc,
message: 'Computed key cannot be resolved.',
} as Rule.ReportDescriptor);
} else if (val === 'ARG') {
return context.report({
node: style.key,
loc: style.key.loc,
message: 'Computed key cannot depend on function argument',
} as Rule.ReportDescriptor);
} else {
styleKey = val;
}
}
if (!isStylexWhenCall) {
const val = evaluate(styleKey, variables, getSourceCode(context));
if (val == null) {
return context.report({
node: style.key,
loc: style.key.loc,
message:
styleKey.type === 'MemberExpression'
? 'All keys in a stylex object must be static literal values.'
: 'Computed key cannot be resolved.',
} as Rule.ReportDescriptor);
} else if (val === 'ARG') {
return context.report({
node: style.key,
loc: style.key.loc,
message: 'Computed key cannot depend on function argument',
} as Rule.ReportDescriptor);
} else {
styleKey = val;
}
}
}
Expand Down
Loading