Skip to content

Commit fd72aa9

Browse files
SamTV12345claude
andcommitted
fix(react-tokens): emit valid modules for reserved-word token names
The new switch component tokens produce a `switch` module, and the generator emitted `export const switch = ...`, which is a syntax error that breaks every consumer of the token files (including the docs client build). Reserved words now get a prefixed local binding that is re-exported under the original name, so the public API is unchanged. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent a8f21ee commit fd72aa9

1 file changed

Lines changed: 29 additions & 5 deletions

File tree

packages/react-tokens/scripts/writeTokens.mjs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,33 @@ import { generateTokens } from './generateTokens.mjs';
44

55
const outDir = resolve(import.meta.dirname, '../dist');
66

7-
const writeESMExport = (tokenName, tokenString) =>
7+
// Words that cannot be used as binding identifiers (e.g. the `switch` component token),
8+
// so `export const <token>` / `declare const <token>` would be a syntax error.
9+
// prettier-ignore
10+
const RESERVED_WORDS = new Set([
11+
'await', 'break', 'case', 'catch', 'class', 'const', 'continue', 'debugger', 'default', 'delete',
12+
'do', 'else', 'enum', 'export', 'extends', 'false', 'finally', 'for', 'function', 'if',
13+
'implements', 'import', 'in', 'instanceof', 'interface', 'let', 'new', 'null', 'package',
14+
'private', 'protected', 'public', 'return', 'static', 'super', 'switch', 'this', 'throw',
15+
'true', 'try', 'typeof', 'var', 'void', 'while', 'with', 'yield'
16+
]);
17+
18+
const getLocalName = (tokenName) => (RESERVED_WORDS.has(tokenName) ? `_${tokenName}` : tokenName);
19+
20+
const writeESMExport = (tokenName, tokenString) => {
21+
const localName = getLocalName(tokenName);
22+
const exportStatement =
23+
localName === tokenName
24+
? `export const ${tokenName} = ${tokenString};`
25+
: `const ${localName} = ${tokenString};\nexport { ${localName} as ${tokenName} };`;
826
outputFileSync(
927
join(outDir, 'esm/', `${tokenName}.js`),
1028
`
11-
export const ${tokenName} = ${tokenString};
12-
export default ${tokenName};
29+
${exportStatement}
30+
export default ${localName};
1331
`.trim()
1432
);
33+
};
1534

1635
const writeCJSExport = (tokenName, tokenString) =>
1736
outputFileSync(
@@ -25,9 +44,14 @@ exports["default"] = exports.${tokenName};
2544
);
2645

2746
const writeDTSExport = (tokenName, tokenString) => {
47+
const localName = getLocalName(tokenName);
48+
const exportStatement =
49+
localName === tokenName
50+
? `export const ${tokenName}: ${tokenString};`
51+
: `declare const ${localName}: ${tokenString};\nexport { ${localName} as ${tokenName} };`;
2852
const text = `
29-
export const ${tokenName}: ${tokenString};
30-
export default ${tokenName};
53+
${exportStatement}
54+
export default ${localName};
3155
`.trim();
3256
const filename = `${tokenName}.d.ts`;
3357
outputFileSync(join(outDir, 'esm', filename), text);

0 commit comments

Comments
 (0)