diff --git a/src/utils/identifierHelpers.ts b/src/utils/identifierHelpers.ts index 0dbbde65c74..6d1fceaca68 100644 --- a/src/utils/identifierHelpers.ts +++ b/src/utils/identifierHelpers.ts @@ -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); @@ -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 || '_'; } diff --git a/test/function/samples/escape-arguments/_config.js b/test/function/samples/escape-arguments/_config.js new file mode 100644 index 00000000000..8208e4edde9 --- /dev/null +++ b/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 } }); + } +}; diff --git a/test/function/samples/escape-arguments/arguments.js b/test/function/samples/escape-arguments/arguments.js new file mode 100644 index 00000000000..7a4e8a723a4 --- /dev/null +++ b/test/function/samples/escape-arguments/arguments.js @@ -0,0 +1 @@ +export default 42; diff --git a/test/function/samples/escape-arguments/main.js b/test/function/samples/escape-arguments/main.js new file mode 100644 index 00000000000..93a8f661807 --- /dev/null +++ b/test/function/samples/escape-arguments/main.js @@ -0,0 +1 @@ +export * as foo from './arguments.js';