@@ -4,29 +4,35 @@ import { generateTokens } from './generateTokens.mjs';
44
55const 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
1011const 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
1819const 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+
2026const 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 ) }
3036export default ${ localName } ;
3137` . trim ( )
3238 ) ;
@@ -45,12 +51,12 @@ exports["default"] = exports.${tokenName};
4551
4652const 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 ) }
5460export default ${ localName } ;
5561` . trim ( ) ;
5662 const filename = `${ tokenName } .d.ts` ;
0 commit comments