Skip to content

Commit

Permalink
refactor: use set for reserved words (#4298)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalborczyk committed Dec 11, 2021
1 parent 49d41f7 commit e6d70d8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/utils/exportNames.ts
Expand Up @@ -18,7 +18,7 @@ export function assignExportsToMangledNames(
nameIndex += 9 * 64 ** (exportName.length - 1);
exportName = toBase64(nameIndex);
}
} while (RESERVED_NAMES[exportName] || exportsByName[exportName]);
} while (RESERVED_NAMES.has(exportName) || exportsByName[exportName]);
}
exportsByName[exportName] = variable;
exportNamesByVariable.set(variable, [exportName]);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/generateCodeSnippets.ts
Expand Up @@ -79,7 +79,7 @@ export function getGenerateCodeSnippets({

const isValidPropName = reservedNamesAsProps
? (name: string): boolean => validPropName.test(name)
: (name: string): boolean => !RESERVED_NAMES[name] && validPropName.test(name);
: (name: string): boolean => !RESERVED_NAMES.has(name) && validPropName.test(name);

return {
_,
Expand Down
106 changes: 50 additions & 56 deletions src/utils/reservedNames.ts
@@ -1,56 +1,50 @@
export interface NameCollection {
[name: string]: true | null;
__proto__: null;
}

export const RESERVED_NAMES: NameCollection = {
__proto__: null,
await: true,
break: true,
case: true,
catch: true,
class: true,
const: true,
continue: true,
debugger: true,
default: true,
delete: true,
do: true,
else: true,
enum: true,
eval: true,
export: true,
extends: true,
false: true,
finally: true,
for: true,
function: true,
if: true,
implements: true,
import: true,
in: true,
instanceof: true,
interface: true,
let: true,
new: true,
null: true,
package: true,
private: true,
protected: true,
public: true,
return: true,
static: true,
super: true,
switch: true,
this: true,
throw: true,
true: true,
try: true,
typeof: true,
undefined: true,
var: true,
void: true,
while: true,
with: true,
yield: true
};
export const RESERVED_NAMES = new Set([
'await',
'break',
'case',
'catch',
'class',
'const',
'continue',
'debugger',
'default',
'delete',
'do',
'else',
'enum',
'eval',
'export',
'extends',
'false',
'finally',
'for',
'function',
'if',
'implements',
'import',
'in',
'instanceof',
'interface',
'let',
'new',
'null',
'package',
'private',
'protected',
'public',
'return',
'static',
'super',
'switch',
'this',
'throw',
'true',
'try',
'typeof',
'undefined',
'var',
'void',
'while',
'with',
'yield'
]);
2 changes: 1 addition & 1 deletion src/utils/safeName.ts
Expand Up @@ -4,7 +4,7 @@ import { RESERVED_NAMES } from './reservedNames';
export function getSafeName(baseName: string, usedNames: Set<string>): string {
let safeName = baseName;
let count = 1;
while (usedNames.has(safeName) || RESERVED_NAMES[safeName]) {
while (usedNames.has(safeName) || RESERVED_NAMES.has(safeName)) {
safeName = `${baseName}$${toBase64(count++)}`;
}
usedNames.add(safeName);
Expand Down

0 comments on commit e6d70d8

Please sign in to comment.