Skip to content

Commit

Permalink
Prevent using arguments for generated variable names (#4613)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Aug 19, 2022
1 parent a8647da commit a85327c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/utils/identifierHelpers.ts
Expand Up @@ -4,8 +4,11 @@ const illegalCharacters = /[^$_a-zA-Z0-9]/g;

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

const needsEscape = (str: string) =>
startsWithDigit(str) || RESERVED_NAMES.has(str) || str === 'arguments';

export function isLegal(str: string): boolean {
if (startsWithDigit(str) || RESERVED_NAMES.has(str)) {
if (needsEscape(str)) {
return false;
}
return !illegalCharacters.test(str);
Expand All @@ -14,7 +17,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) || RESERVED_NAMES.has(str)) str = `_${str}`;
if (needsEscape(str)) str = `_${str}`;

return str || '_';
}
8 changes: 8 additions & 0 deletions test/function/samples/escape-arguments/_config.js
@@ -0,0 +1,8 @@
const assert = require('assert');

module.exports = {
description: 'does not use "arguments" as a placeholder variable for a default export',
exports(exports) {
assert.deepStrictEqual(exports, { foo: { __proto__: null, default: 42 } });
}
};
1 change: 1 addition & 0 deletions test/function/samples/escape-arguments/arguments.js
@@ -0,0 +1 @@
export default 42;
1 change: 1 addition & 0 deletions test/function/samples/escape-arguments/main.js
@@ -0,0 +1 @@
export * as foo from './arguments.js';

0 comments on commit a85327c

Please sign in to comment.