@@ -496,6 +496,25 @@ const EVALUATE_AST_CACHE_LIMIT = 4096;
496496
497497const 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+
499518class 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 ;
0 commit comments