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
Expand Up @@ -257,6 +257,85 @@ describe('@stylexjs/babel-plugin', () => {
}).toThrow(messages.ILLEGAL_PROP_VALUE);
});

test('invalid value: NaN', () => {
expect(() => {
transform(`
import * as stylex from '@stylexjs/stylex';
const notANumber = 'a';
const styles = stylex.create({
default: {
opacity: notANumber * 2,
},
});
`);
}).toThrow(messages.ILLEGAL_PROP_NUMBER_VALUE);
});

test('invalid value: Infinity', () => {
expect(() => {
transform(`
import * as stylex from '@stylexjs/stylex';
const styles = stylex.create({
default: {
zIndex: 1 / 0,
},
});
`);
}).toThrow(messages.ILLEGAL_PROP_NUMBER_VALUE);
});

test('invalid value: NaN in array', () => {
expect(() => {
transform(`
import * as stylex from '@stylexjs/stylex';
const styles = stylex.create({
default: {
opacity: [1, NaN],
},
});
`);
}).toThrow(messages.ILLEGAL_PROP_NUMBER_VALUE);
});

test('invalid value: NaN in conditional styles', () => {
expect(() => {
transform(`
import * as stylex from '@stylexjs/stylex';
const styles = stylex.create({
default: {
opacity: {
default: 1,
':hover': -Infinity,
},
},
});
`);
}).toThrow(messages.ILLEGAL_PROP_NUMBER_VALUE);
});

test('invalid value: NaN from arithmetic on imported defineConsts', () => {
expect(() => {
transform(
`
import * as stylex from '@stylexjs/stylex';
import { colors } from './constants.stylex.js';
const styles = stylex.create({
default: {
opacity: colors.accent * 2,
},
});
`,
{
filename: `${__dirname}/__fixtures__/App.js`,
unstable_moduleResolution: {
type: 'commonJS',
rootDir: `${__dirname}/__fixtures__`,
},
},
);
}).toThrow(messages.ILLEGAL_PROP_NUMBER_VALUE);
});

test('invalid value: non-static', () => {
cleanExpect(() => {
transform(`
Expand Down
2 changes: 2 additions & 0 deletions packages/@stylexjs/babel-plugin/src/shared/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export const ILLEGAL_PROP_VALUE =
'A style value can only contain an array, string or number.';
export const ILLEGAL_PROP_ARRAY_VALUE =
'A style array value can only contain strings or numbers.';
export const ILLEGAL_PROP_NUMBER_VALUE =
'A style value cannot be NaN or Infinity. Check for arithmetic that cannot be evaluated at compile time, such as on imported defineConsts values.';
export const ILLEGAL_NAMESPACE_VALUE = 'A StyleX namespace must be an object.';
export const INVALID_PSEUDO = 'Invalid pseudo selector, not on the whitelist.';
export const INVALID_PSEUDO_OR_AT_RULE = 'Invalid pseudo or at-rule.';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
import * as messages from '../messages';
import { isPlainObject } from '../utils/object-utils';

// `NaN` and `Infinity` are numbers in JS, but they are not valid CSS values.
// They usually come from arithmetic that could not be evaluated as intended,
// e.g. multiplying imported `defineConsts` values, which compile to strings.
function validateNumber(value: number): void {
if (!Number.isFinite(value)) {
throw new Error(messages.ILLEGAL_PROP_NUMBER_VALUE);
}
}

export function validateNamespace(
namespace: mixed,
conditions: $ReadOnlyArray<string> = [],
Expand All @@ -20,12 +29,20 @@ export function validateNamespace(
const ns: { +[string]: mixed } = namespace;
for (const key in ns) {
const val = ns[key];
if (val === null || typeof val === 'string' || typeof val === 'number') {
if (typeof val === 'number') {
validateNumber(val);
continue;
}
if (val === null || typeof val === 'string') {
continue;
}
if (Array.isArray(val)) {
for (const v of val) {
if (v === null || typeof v === 'string' || typeof v === 'number') {
if (typeof v === 'number') {
validateNumber(v);
continue;
}
if (v === null || typeof v === 'string') {
continue;
}
throw new Error(messages.ILLEGAL_PROP_ARRAY_VALUE);
Expand Down Expand Up @@ -73,14 +90,22 @@ function validateConditionalStyles(
if (conditions.includes(key)) {
throw new Error(messages.DUPLICATE_CONDITIONAL);
}
if (typeof v === 'number') {
validateNumber(v);
continue;
}
// $FlowFixMe[invalid-compare]
if (v === null || typeof v === 'string' || typeof v === 'number') {
if (v === null || typeof v === 'string') {
continue;
}
if (Array.isArray(v)) {
for (const vv of v) {
if (typeof vv === 'number') {
validateNumber(vv);
continue;
}
// $FlowFixMe[invalid-compare]
if (vv === null || typeof vv === 'string' || typeof vv === 'number') {
if (vv === null || typeof vv === 'string') {
continue;
}
throw new Error(messages.ILLEGAL_PROP_VALUE);
Expand Down