Skip to content

Commit 2d18aa6

Browse files
fix(parser): report strict-mode-invalid binding names in ES module output (#21567)
* fix(parser): report strict-mode-invalid binding names in ES module output A loose script keeps binding names that strict mode rejects, so acorn accepts them, but emitting the module as ES module output makes the whole bundle fail to parse — previously with no diagnostic at all. Extend strictModeViolations to bindings named eval/arguments, the strict-mode reserved words, and await (reserved by the module goal), covering variable declarations, function/class declaration names, named function expressions and catch parameters. * fix(parser): check parameters and class expression names for reserved words Parameters and named class expressions are bindings too, so the same names break there: a loose script accepts `function f(static)` and `var C = class await {}`, but ES module output rejects both. * fix(parser): check called function expressions for reserved binding names
1 parent fe7177e commit 2d18aa6

8 files changed

Lines changed: 250 additions & 5 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": minor
3+
---
4+
5+
Report bindings named `eval`, `arguments` or reserved words in ES module output.

‎lib/javascript/JavascriptParser.js‎

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,25 @@ const EVALUATE_AST_CACHE_LIMIT = 4096;
496496

497497
const CLASS_NAME = "JavascriptParser";
498498

499+
// Reserved words in strict mode, so unusable as binding names there (12.7.2).
500+
const STRICT_MODE_RESERVED_WORDS = new Set([
501+
"implements",
502+
"interface",
503+
"let",
504+
"package",
505+
"private",
506+
"protected",
507+
"public",
508+
"static",
509+
"yield"
510+
]);
511+
512+
const STRICT_MODE_OUTPUT_REASON =
513+
"The output is an ES module, which runs in strict mode.";
514+
// `await` is reserved by the module goal rather than by strict mode itself.
515+
const MODULE_GOAL_OUTPUT_REASON =
516+
'The output is an ES module, where "await" is a reserved word.';
517+
499518
class JavascriptParser extends Parser {
500519
/**
501520
* Creates an instance of JavascriptParser.
@@ -2872,6 +2891,9 @@ class JavascriptParser extends Parser {
28722891
*/
28732892
preWalkFunctionDeclaration(statement) {
28742893
if (statement.id) {
2894+
if (this._strictInModuleOutput) {
2895+
this._checkStrictModeBinding(statement.id.name, statement.id);
2896+
}
28752897
this.defineVariable(statement.id.name);
28762898
}
28772899
}
@@ -3263,21 +3285,25 @@ class JavascriptParser extends Parser {
32633285
_preWalkVariableDeclaration(statement, hookMap) {
32643286
/** @type {OnIdent | undefined} */
32653287
let onIdent;
3288+
const checkStrictBindings = this._strictInModuleOutput;
32663289
for (const declarator of statement.declarations) {
32673290
if (declarator.type !== "VariableDeclarator") continue;
32683291
this.preWalkVariableDeclarator(declarator);
32693292
if (this.hooks.preDeclarator.call(declarator, statement)) continue;
32703293
const id = declarator.id;
32713294
if (id.type === "Identifier") {
3295+
if (checkStrictBindings) this._checkStrictModeBinding(id.name, id);
32723296
// fast path: plain `const x =` skips the enterPattern dispatch and
32733297
// the per-declaration onIdent closure (only patterns need it)
32743298
if (!this.callHooksForName(this.hooks.pattern, id.name, id)) {
32753299
this._defineVariableForDeclaration(id.name, id, hookMap);
32763300
}
32773301
} else {
32783302
if (onIdent === undefined) {
3279-
onIdent = (name, ident) =>
3303+
onIdent = (name, ident) => {
3304+
if (checkStrictBindings) this._checkStrictModeBinding(name, ident);
32803305
this._defineVariableForDeclaration(name, ident, hookMap);
3306+
};
32813307
}
32823308
this.enterPattern(id, onIdent);
32833309
}
@@ -3447,6 +3473,11 @@ class JavascriptParser extends Parser {
34473473
*/
34483474
blockPreWalkClassDeclaration(statement) {
34493475
if (statement.id) {
3476+
// only `await` reaches here: acorn rejects the other names itself,
3477+
// since a class body is always strict even in a loose script
3478+
if (this._strictInModuleOutput) {
3479+
this._checkStrictModeBinding(statement.id.name, statement.id);
3480+
}
34503481
this.defineVariable(statement.id.name);
34513482
}
34523483
}
@@ -3527,7 +3558,10 @@ class JavascriptParser extends Parser {
35273558
this.inBlockScope(() => {
35283559
// Error binding is optional in catch clause since ECMAScript 2019
35293560
if (catchClause.param !== null) {
3530-
this.enterPattern(catchClause.param, (ident) => {
3561+
this.enterPattern(catchClause.param, (ident, identifier) => {
3562+
if (this._strictInModuleOutput) {
3563+
this._checkStrictModeBinding(ident, identifier);
3564+
}
35313565
this.defineVariable(ident);
35323566
});
35333567
this.walkPattern(catchClause.param);
@@ -3830,6 +3864,9 @@ class JavascriptParser extends Parser {
38303864

38313865
if (this._strictInModuleOutput) {
38323866
this._checkStrictModeParams(expression.params);
3867+
if (expression.id) {
3868+
this._checkStrictModeBinding(expression.id.name, expression.id);
3869+
}
38333870
}
38343871
this.inFunctionScope(true, scopeParams, () => {
38353872
for (const param of expression.params) {
@@ -3967,6 +4004,32 @@ class JavascriptParser extends Parser {
39674004
}
39684005
}
39694006

4007+
/**
4008+
* Reports a binding name a loose script allows but strict-mode ESM output
4009+
* rejects — the whole bundle fails to parse, so this is never merely latent.
4010+
* @param {string} name binding name
4011+
* @param {Statement | Expression | Identifier} node node to report the violation at
4012+
*/
4013+
_checkStrictModeBinding(name, node) {
4014+
if (name === "eval" || name === "arguments") {
4015+
this._reportStrictModeViolation(
4016+
`"${name}" is not allowed as a binding name`,
4017+
node
4018+
);
4019+
} else if (STRICT_MODE_RESERVED_WORDS.has(name)) {
4020+
this._reportStrictModeViolation(
4021+
`"${name}" is a reserved word in strict mode and is not allowed as a binding name`,
4022+
node
4023+
);
4024+
} else if (name === "await") {
4025+
this._reportStrictModeViolation(
4026+
'"await" is not allowed as a binding name',
4027+
node,
4028+
MODULE_GOAL_OUTPUT_REASON
4029+
);
4030+
}
4031+
}
4032+
39704033
/**
39714034
* Reports strict-mode-only errors in a function's parameter list: a duplicate
39724035
* simple parameter, or a parameter named `eval` / `arguments`.
@@ -3983,6 +4046,17 @@ class JavascriptParser extends Parser {
39834046
`"${name}" is not allowed as a parameter name`,
39844047
param
39854048
);
4049+
} else if (STRICT_MODE_RESERVED_WORDS.has(name)) {
4050+
this._reportStrictModeViolation(
4051+
`"${name}" is a reserved word in strict mode and is not allowed as a parameter name`,
4052+
param
4053+
);
4054+
} else if (name === "await") {
4055+
this._reportStrictModeViolation(
4056+
'"await" is not allowed as a parameter name',
4057+
param,
4058+
MODULE_GOAL_OUTPUT_REASON
4059+
);
39864060
}
39874061
if (seen === undefined) seen = new Set();
39884062
if (seen.has(name)) {
@@ -4036,10 +4110,11 @@ class JavascriptParser extends Parser {
40364110
* The location is computed here, on the rare violation path only.
40374111
* @param {string} reason what is not allowed
40384112
* @param {Statement | Expression | Identifier} node node to report the violation at
4113+
* @param {string=} outputReason why the output rejects it, defaults to strict mode
40394114
*/
4040-
_reportStrictModeViolation(reason, node) {
4115+
_reportStrictModeViolation(reason, node, outputReason) {
40414116
const diagnostic = new WebpackError(
4042-
`${reason}. The output is an ES module, which runs in strict mode.`
4117+
`${reason}. ${outputReason === undefined ? STRICT_MODE_OUTPUT_REASON : outputReason}`
40434118
);
40444119
diagnostic.loc = this.getLocation(node);
40454120
if (this.options.strictModeViolations === "error") {
@@ -4243,6 +4318,10 @@ class JavascriptParser extends Parser {
42434318
* @param {ClassExpression} expression the class expression
42444319
*/
42454320
walkClassExpression(expression) {
4321+
// only `await` reaches here, as for class declarations
4322+
if (this._strictInModuleOutput && expression.id) {
4323+
this._checkStrictModeBinding(expression.id.name, expression.id);
4324+
}
42464325
this.walkClass(expression);
42474326
}
42484327

@@ -4300,6 +4379,16 @@ class JavascriptParser extends Parser {
43004379
};
43014380
const { params, type } = functionExpression;
43024381
const arrow = type === "ArrowFunctionExpression";
4382+
// the walk skips walkFunctionExpression for a called function expression
4383+
if (this._strictInModuleOutput) {
4384+
this._checkStrictModeParams(params);
4385+
if (type === "FunctionExpression" && functionExpression.id) {
4386+
this._checkStrictModeBinding(
4387+
functionExpression.id.name,
4388+
functionExpression.id
4389+
);
4390+
}
4391+
}
43034392
const renameThis = currentThis ? getVarInfo(currentThis) : null;
43044393
const varInfoForArgs = options.map(getVarInfo);
43054394
const wasTopLevel = this.scope.topLevelScope;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "./mod.js";
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Sloppy CommonJS module: every binding below is legal in a loose script, so
2+
// acorn accepts it, but each one is a SyntaxError once the module is emitted
3+
// into strict-mode ESM output — the bundle would not parse at all.
4+
var eval = 1;
5+
function static() {}
6+
var namedExpression = function yield() {};
7+
var { package } = { package: 1 };
8+
var [private] = [1];
9+
var interface = 2;
10+
11+
function reservedLocals() {
12+
var protected = 1;
13+
var public = 2;
14+
return protected + public;
15+
}
16+
17+
let arguments = 3;
18+
19+
try {
20+
null.x;
21+
} catch (implements) {}
22+
23+
// `await` is not strict-mode-reserved, but it is reserved in module code, so
24+
// these break in ESM output for the same reason.
25+
function awaitLocal() {
26+
var await = 4;
27+
return await;
28+
}
29+
class await {}
30+
31+
// Parameters are bindings too, including on arrow functions.
32+
function reservedParam(static) {
33+
return static;
34+
}
35+
var arrowParam = (package) => package;
36+
function awaitParam(await) {
37+
return await;
38+
}
39+
40+
// A named class expression binds its name, same as a declaration.
41+
var namedClass = class await {};
42+
43+
// A called function expression is walked as an IIFE, not through
44+
// walkFunctionExpression.
45+
var iifeName = (function yield() {
46+
return 1;
47+
})();
48+
var iifeParam = (function (static) {
49+
return static;
50+
})(1);
51+
52+
// Unaffected: reserved words are valid as property and method names.
53+
var properties = { static: 1, public: 2, await: 3 };
54+
var readsProperty = properties.static + properties.public;
55+
var normalBinding = 5;
56+
57+
module.exports = {
58+
eval,
59+
static,
60+
namedExpression,
61+
package,
62+
private,
63+
interface,
64+
reservedLocals,
65+
arguments,
66+
awaitLocal,
67+
await,
68+
reservedParam,
69+
arrowParam,
70+
awaitParam,
71+
namedClass,
72+
iifeName,
73+
iifeParam,
74+
readsProperty,
75+
normalBinding
76+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"use strict";
2+
3+
// The bundle intentionally keeps strict-mode-reserved bindings, so it must not run.
4+
module.exports.noTests = true;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"use strict";
2+
3+
module.exports = [
4+
[
5+
{
6+
message: /"eval" is not allowed as a binding name/,
7+
moduleName: /mod\.js/
8+
}
9+
],
10+
[{ message: /"static" is a reserved word in strict mode/ }],
11+
[{ message: /"yield" is a reserved word in strict mode/ }],
12+
[{ message: /"package" is a reserved word in strict mode/ }],
13+
[{ message: /"private" is a reserved word in strict mode/ }],
14+
[{ message: /"interface" is a reserved word in strict mode/ }],
15+
[{ message: /"protected" is a reserved word in strict mode/ }],
16+
[{ message: /"public" is a reserved word in strict mode/ }],
17+
[{ message: /"arguments" is not allowed as a binding name/ }],
18+
[{ message: /"implements" is a reserved word in strict mode/ }],
19+
[
20+
{
21+
message:
22+
/"await" is not allowed as a binding name\. The output is an ES module, where "await" is a reserved word\./
23+
}
24+
],
25+
[{ message: /"await" is not allowed as a binding name/ }],
26+
[
27+
{
28+
message:
29+
/"static" is a reserved word in strict mode and is not allowed as a parameter name/
30+
}
31+
],
32+
[
33+
{
34+
message:
35+
/"package" is a reserved word in strict mode and is not allowed as a parameter name/
36+
}
37+
],
38+
[{ message: /"await" is not allowed as a parameter name/ }],
39+
[{ message: /"await" is not allowed as a binding name/ }],
40+
[
41+
{
42+
message:
43+
/"yield" is a reserved word in strict mode and is not allowed as a binding name/
44+
}
45+
],
46+
[
47+
{
48+
message:
49+
/"static" is a reserved word in strict mode and is not allowed as a parameter name/
50+
}
51+
]
52+
];
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
mode: "development",
6+
target: "node",
7+
output: {
8+
module: true,
9+
chunkFormat: "module",
10+
library: { type: "module" }
11+
},
12+
experiments: {
13+
outputModule: true
14+
}
15+
};

‎test/configCases/parsing/strict-mode-module-output-semantics-future-defaults/errors.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@ module.exports = [
1818
],
1919
[{ message: /Assigning to the read-only global "NaN" is not allowed/ }],
2020
[{ message: /Assigning to the read-only global "Infinity" is not allowed/ }],
21-
[{ message: /Assigning to "eval" is not allowed/ }]
21+
[{ message: /Assigning to "eval" is not allowed/ }],
22+
// shadowing `arguments` suppresses the member checks, but the binding itself
23+
// is a strict-mode SyntaxError
24+
[{ message: /"arguments" is not allowed as a binding name/ }]
2225
];

0 commit comments

Comments
 (0)