Skip to content

Commit b4d26c3

Browse files
author
SamTV12345
committed
fix(react-tokens): avoid duplicate default export for reserved-word tokens
A token named `default` would have produced both an aliased default re-export and `export default`, which is a duplicate export. The alias is now skipped for `default`, and `eval`/`arguments` (invalid binding names in strict mode) are treated as reserved as well.
1 parent 08d3ffb commit b4d26c3

1 file changed

Lines changed: 19 additions & 13 deletions

File tree

packages/react-tokens/scripts/writeTokens.mjs

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

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

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.
7+
// Words that cannot be used as binding identifiers in strict mode code (e.g. the
8+
// `switch` component token), so `export const <token>` / `declare const <token>`
9+
// would be a syntax error.
910
// prettier-ignore
1011
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'
12+
'arguments', 'await', 'break', 'case', 'catch', 'class', 'const', 'continue', 'debugger',
13+
'default', 'delete', 'do', 'else', 'enum', 'eval', 'export', 'extends', 'false', 'finally',
14+
'for', 'function', 'if', 'implements', 'import', 'in', 'instanceof', 'interface', 'let',
15+
'new', 'null', 'package', 'private', 'protected', 'public', 'return', 'static', 'super',
16+
'switch', 'this', 'throw', 'true', 'try', 'typeof', 'var', 'void', 'while', 'with', 'yield'
1617
]);
1718

1819
const getLocalName = (tokenName) => (RESERVED_WORDS.has(tokenName) ? `_${tokenName}` : tokenName);
1920

21+
// `export default` already declares the `default` export name, so an extra
22+
// `export { _default as default }` alias would be a duplicate export.
23+
const getAliasExport = (localName, tokenName) =>
24+
localName === tokenName || tokenName === 'default' ? '' : `\nexport { ${localName} as ${tokenName} };`;
25+
2026
const writeESMExport = (tokenName, tokenString) => {
2127
const localName = getLocalName(tokenName);
22-
const exportStatement =
28+
const declaration =
2329
localName === tokenName
2430
? `export const ${tokenName} = ${tokenString};`
25-
: `const ${localName} = ${tokenString};\nexport { ${localName} as ${tokenName} };`;
31+
: `const ${localName} = ${tokenString};`;
2632
outputFileSync(
2733
join(outDir, 'esm/', `${tokenName}.js`),
2834
`
29-
${exportStatement}
35+
${declaration}${getAliasExport(localName, tokenName)}
3036
export default ${localName};
3137
`.trim()
3238
);
@@ -45,12 +51,12 @@ exports["default"] = exports.${tokenName};
4551

4652
const writeDTSExport = (tokenName, tokenString) => {
4753
const localName = getLocalName(tokenName);
48-
const exportStatement =
54+
const declaration =
4955
localName === tokenName
5056
? `export const ${tokenName}: ${tokenString};`
51-
: `declare const ${localName}: ${tokenString};\nexport { ${localName} as ${tokenName} };`;
57+
: `declare const ${localName}: ${tokenString};`;
5258
const text = `
53-
${exportStatement}
59+
${declaration}${getAliasExport(localName, tokenName)}
5460
export default ${localName};
5561
`.trim();
5662
const filename = `${tokenName}.d.ts`;

0 commit comments

Comments
 (0)