diff --git a/packages/@stylexjs/babel-plugin/__tests__/validation-stylex-create-test.js b/packages/@stylexjs/babel-plugin/__tests__/validation-stylex-create-test.js index 4d35378ff..897d00782 100644 --- a/packages/@stylexjs/babel-plugin/__tests__/validation-stylex-create-test.js +++ b/packages/@stylexjs/babel-plugin/__tests__/validation-stylex-create-test.js @@ -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(` diff --git a/packages/@stylexjs/babel-plugin/src/shared/messages.js b/packages/@stylexjs/babel-plugin/src/shared/messages.js index bfbd96841..fccd60b97 100644 --- a/packages/@stylexjs/babel-plugin/src/shared/messages.js +++ b/packages/@stylexjs/babel-plugin/src/shared/messages.js @@ -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.'; diff --git a/packages/@stylexjs/babel-plugin/src/shared/preprocess-rules/basic-validation.js b/packages/@stylexjs/babel-plugin/src/shared/preprocess-rules/basic-validation.js index 142e25a01..2d3d6553e 100644 --- a/packages/@stylexjs/babel-plugin/src/shared/preprocess-rules/basic-validation.js +++ b/packages/@stylexjs/babel-plugin/src/shared/preprocess-rules/basic-validation.js @@ -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 = [], @@ -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); @@ -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);