Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: re-use reserved names set #4304

Merged
merged 6 commits into from Dec 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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