Skip to content

Commit a85327c

Browse files
authored
Prevent using arguments for generated variable names (#4613)
1 parent a8647da commit a85327c

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

src/utils/identifierHelpers.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ const illegalCharacters = /[^$_a-zA-Z0-9]/g;
44

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

7+
const needsEscape = (str: string) =>
8+
startsWithDigit(str) || RESERVED_NAMES.has(str) || str === 'arguments';
9+
710
export function isLegal(str: string): boolean {
8-
if (startsWithDigit(str) || RESERVED_NAMES.has(str)) {
11+
if (needsEscape(str)) {
912
return false;
1013
}
1114
return !illegalCharacters.test(str);
@@ -14,7 +17,7 @@ export function isLegal(str: string): boolean {
1417
export function makeLegal(str: string): string {
1518
str = str.replace(/-(\w)/g, (_, letter) => letter.toUpperCase()).replace(illegalCharacters, '_');
1619

17-
if (startsWithDigit(str) || RESERVED_NAMES.has(str)) str = `_${str}`;
20+
if (needsEscape(str)) str = `_${str}`;
1821

1922
return str || '_';
2023
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const assert = require('assert');
2+
3+
module.exports = {
4+
description: 'does not use "arguments" as a placeholder variable for a default export',
5+
exports(exports) {
6+
assert.deepStrictEqual(exports, { foo: { __proto__: null, default: 42 } });
7+
}
8+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 42;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * as foo from './arguments.js';

0 commit comments

Comments
 (0)