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

Prevent using "arguments" for generated variable names #4613

Merged
merged 1 commit into from Aug 19, 2022
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
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';