Skip to content

Commit 2402433

Browse files
authored
Merge pull request #940 from facebook/fix/num-var-prefix
fix: Escape special characters from debug var names
2 parents 723538c + da17c23 commit 2402433

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

packages/babel-plugin/__tests__/stylex-transform-define-vars-test.js

+35
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,41 @@ describe('@stylexjs/babel-plugin', () => {
317317
`);
318318
});
319319

320+
test('transforms variables object with numeric keys and add stylex.inject in debug mode', () => {
321+
expect(
322+
transform(
323+
`
324+
import stylex from 'stylex';
325+
export const buttonTheme = stylex.defineVars({
326+
'10': {
327+
default: 'blue',
328+
'@media (prefers-color-scheme: dark)': 'lightblue',
329+
'@media print': 'white',
330+
},
331+
'1.5 pixels': {
332+
default: 'grey',
333+
'@media (prefers-color-scheme: dark)': 'rgba(0, 0, 0, 0.8)',
334+
},
335+
'corner#radius': 10,
336+
'@@primary': {
337+
default: 'pink',
338+
},
339+
});
340+
`,
341+
{ ...defaultOpts, debug: true },
342+
).code,
343+
).toMatchInlineSnapshot(`
344+
"import stylex from 'stylex';
345+
export const buttonTheme = {
346+
"10": "var(--_10-xhaj83f)",
347+
"1.5 pixels": "var(--_1_5_pixels-xhrybbu)",
348+
"corner#radius": "var(--corner_radius-x19gjwfn)",
349+
"@@primary": "var(--__primary-x9xh8rb)",
350+
__themeName__: "x568ih9"
351+
};"
352+
`);
353+
});
354+
320355
test('transforms variables object in non-haste env', () => {
321356
expect(
322357
transform(

packages/babel-plugin/src/utils/evaluate-path.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,13 @@ function evaluateThemeRef(
174174

175175
const { debug, enableDebugClassNames } = state.traversalState.options;
176176

177+
const varSafeKey = (
178+
key[0] >= '0' && key[0] <= '9' ? `_${key}` : key
179+
).replace(/[^a-zA-Z0-9]/g, '_');
180+
177181
const varName =
178182
debug && enableDebugClassNames
179-
? key +
183+
? varSafeKey +
180184
'-' +
181185
state.traversalState.options.classNamePrefix +
182186
utils.hash(strToHash)

packages/shared/src/stylex-define-vars.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@ export default function styleXDefineVars<Vars: VarsConfig>(
5353
const variablesMap: {
5454
+[string]: { +nameHash: string, +value: VarsConfigValue },
5555
} = objMap(variables, (value, key) => {
56-
// Created hashed variable names with fileName//themeName//key
56+
const varSafeKey = (
57+
key[0] >= '0' && key[0] <= '9' ? `_${key}` : key
58+
).replace(/[^a-zA-Z0-9]/g, '_');
5759
const nameHash = key.startsWith('--')
5860
? key.slice(2)
5961
: debug && enableDebugClassNames
60-
? key + '-' + classNamePrefix + createHash(`${themeName}.${key}`)
62+
? varSafeKey + '-' + classNamePrefix + createHash(`${themeName}.${key}`)
6163
: classNamePrefix + createHash(`${themeName}.${key}`);
6264

6365
if (isCSSType(value)) {

0 commit comments

Comments
 (0)