Skip to content

Commit

Permalink
refactor: re-use reserved names set (#4304)
Browse files Browse the repository at this point in the history
* refactor: re-use reserved names set

* refactor: export set as default, rename to reserved words, make set readonly

* refactor: extract builtins, make readonly set

* refactor: order set

* Unify reserved names

Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
Co-authored-by: Lukas Taegert-Atkinson <lukas.taegert-atkinson@tngtech.com>
  • Loading branch information
3 people committed Dec 24, 2021
1 parent a993426 commit edb7982
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 17 deletions.
5 changes: 4 additions & 1 deletion src/utils/reservedNames.ts → src/utils/RESERVED_NAMES.ts
@@ -1,4 +1,4 @@
export const RESERVED_NAMES = new Set([
const RESERVED_NAMES: ReadonlySet<string> = new Set([
'await',
'break',
'case',
Expand Down Expand Up @@ -26,6 +26,7 @@ export const RESERVED_NAMES = new Set([
'instanceof',
'interface',
'let',
'NaN',
'new',
'null',
'package',
Expand All @@ -48,3 +49,5 @@ export const RESERVED_NAMES = new Set([
'with',
'yield'
]);

export default RESERVED_NAMES;
4 changes: 2 additions & 2 deletions src/utils/exportNames.ts
@@ -1,6 +1,6 @@
import Variable from '../ast/variables/Variable';
import RESERVED_NAMES from './RESERVED_NAMES';
import { toBase64 } from './base64';
import { RESERVED_NAMES } from './reservedNames';

export function assignExportsToMangledNames(
exports: Set<Variable>,
Expand All @@ -9,7 +9,7 @@ export function assignExportsToMangledNames(
): void {
let nameIndex = 0;
for (const variable of exports) {
let exportName = variable.name[0];
let [exportName] = variable.name;
if (exportsByName[exportName]) {
do {
exportName = toBase64(++nameIndex);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/generateCodeSnippets.ts
@@ -1,5 +1,5 @@
import { NormalizedOutputOptions } from '../rollup/types';
import { RESERVED_NAMES } from './reservedNames';
import RESERVED_NAMES from './RESERVED_NAMES';

export interface GenerateCodeSnippets {
_: string;
Expand Down
15 changes: 3 additions & 12 deletions src/utils/identifierHelpers.ts
@@ -1,20 +1,11 @@
export const reservedWords =
'break case class catch const continue debugger default delete do else export extends finally for function if import in instanceof let new return super switch this throw try typeof var void while with yield enum await implements package protected static interface private public'.split(
' '
);
const builtins =
'Infinity NaN undefined null true false eval uneval isFinite isNaN parseFloat parseInt decodeURI decodeURIComponent encodeURI encodeURIComponent escape unescape Object Function Boolean Symbol Error EvalError InternalError RangeError ReferenceError SyntaxError TypeError URIError Number Math Date String RegExp Array Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float32Array Float64Array Map Set WeakMap WeakSet SIMD ArrayBuffer DataView JSON Promise Generator GeneratorFunction Reflect Proxy Intl'.split(
' '
);

const blacklisted = new Set(reservedWords.concat(builtins));
import RESERVED_NAMES from './RESERVED_NAMES';

const illegalCharacters = /[^$_a-zA-Z0-9]/g;

const startsWithDigit = (str: string) => /\d/.test(str[0]);

export function isLegal(str: string): boolean {
if (startsWithDigit(str) || blacklisted.has(str)) {
if (startsWithDigit(str) || RESERVED_NAMES.has(str)) {
return false;
}
return !illegalCharacters.test(str);
Expand All @@ -23,7 +14,7 @@ export function isLegal(str: string): boolean {
export function makeLegal(str: string): string {
str = str.replace(/-(\w)/g, (_, letter) => letter.toUpperCase()).replace(illegalCharacters, '_');

if (startsWithDigit(str) || blacklisted.has(str)) str = `_${str}`;
if (startsWithDigit(str) || RESERVED_NAMES.has(str)) str = `_${str}`;

return str || '_';
}
2 changes: 1 addition & 1 deletion src/utils/safeName.ts
@@ -1,5 +1,5 @@
import RESERVED_NAMES from './RESERVED_NAMES';
import { toBase64 } from './base64';
import { RESERVED_NAMES } from './reservedNames';

export function getSafeName(baseName: string, usedNames: Set<string>): string {
let safeName = baseName;
Expand Down

0 comments on commit edb7982

Please sign in to comment.