Skip to content

Commit 6966d15

Browse files
fix(parser): scope strict-mode block function declarations to their block
Only sloppy-mode Annex B hoists a plain function declaration out of its block; strict, async and generator ones stay block-scoped. Fixes #22797
1 parent abb35e8 commit 6966d15

12 files changed

Lines changed: 203 additions & 3 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Scope strict-mode, async and generator block function declarations to their block.

‎lib/javascript/JavascriptParser.js‎

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ const EMPTY_MODULE_DECLARATIONS = [];
182182
// object pattern, so a tap on that type is not served from the list.
183183
const BLOCK_DECLARATION_TYPES = new Set([
184184
"VariableDeclaration",
185+
"FunctionDeclaration",
185186
"ClassDeclaration",
186187
"ExportNamedDeclaration",
187188
"ExportDefaultDeclaration"
@@ -1122,6 +1123,27 @@ const hoistedDeclarationsOf = (node, trusted) => {
11221123
return collectHoistedDeclarations(node.body, EMPTY_HOISTED_DECLARATIONS);
11231124
};
11241125

1126+
/**
1127+
* Whether a statement is one of the list's own rather than nested in one of
1128+
* them, found by its start offset since the list is in source order.
1129+
* @param {(Statement | ModuleDeclaration)[]} statements the list
1130+
* @param {Statement | ModuleDeclaration} statement the statement to find
1131+
* @returns {boolean} whether the list holds it directly
1132+
*/
1133+
const holdsStatement = (statements, statement) => {
1134+
const start = /** @type {Range} */ (statement.range)[0];
1135+
let low = 0;
1136+
let high = statements.length - 1;
1137+
while (low <= high) {
1138+
const middle = (low + high) >>> 1;
1139+
const candidate = statements[middle];
1140+
if (candidate === statement) return true;
1141+
if (/** @type {Range} */ (candidate.range)[0] < start) low = middle + 1;
1142+
else high = middle - 1;
1143+
}
1144+
return false;
1145+
};
1146+
11251147
/**
11261148
* Which statements of a list declare into its block scope, scanned for an AST
11271149
* some other parser built. The parser's own record is read instead when there
@@ -2737,22 +2759,35 @@ class JavascriptParser extends Parser {
27372759
}
27382760
// BACKWARD-COMPAT END
27392761
this._preWalkHoistedDeclarations(
2740-
hoistedDeclarationsOf(node, this._hasParserRecord)
2762+
hoistedDeclarationsOf(node, this._hasParserRecord),
2763+
node.body
27412764
);
27422765
}
27432766

27442767
/**
27452768
* Declare what hoists to the scope being entered, from what the parser
27462769
* recorded while reading it. `prevStatement` is not available here.
27472770
* @param {HoistedDeclarations} hoisted what the scope declares
2771+
* @param {(Statement | ModuleDeclaration)[]} statements the scope's own statements
27482772
*/
2749-
_preWalkHoistedDeclarations(hoisted) {
2773+
_preWalkHoistedDeclarations(hoisted, statements) {
27502774
for (let index = 0, len = hoisted.length; index < len; index++) {
2775+
const declaration = hoisted[index];
2776+
// WHY: only sloppy-mode Annex B hoists a function out of its block.
2777+
// The parser can't tell: `javascript/auto` reads a script as a strict
2778+
// module first. The block pass binds the function in its block.
2779+
if (
2780+
declaration.type === "FunctionDeclaration" &&
2781+
(this.scope.isStrict || declaration.generator || declaration.async) &&
2782+
!holdsStatement(statements, declaration)
2783+
) {
2784+
continue;
2785+
}
27512786
// WHY: the statements between two declarations are not read here, so
27522787
// the one before this declaration is unknown rather than the previous
27532788
// declaration. A tap needing it taps `preStatement`, which walks.
27542789
this.prevStatement = undefined;
2755-
this.preWalkStatement(hoisted[index]);
2790+
this.preWalkStatement(declaration);
27562791
}
27572792
}
27582793

@@ -3132,6 +3167,9 @@ class JavascriptParser extends Parser {
31323167
case "ClassDeclaration":
31333168
this.blockPreWalkClassDeclaration(statement);
31343169
break;
3170+
case "FunctionDeclaration":
3171+
this.blockPreWalkFunctionDeclaration(statement);
3172+
break;
31353173
case "ExportNamedDeclaration":
31363174
this.blockPreWalkExportNamedDeclaration(statement);
31373175
break;
@@ -3578,6 +3616,15 @@ class JavascriptParser extends Parser {
35783616
}
35793617
}
35803618

3619+
/**
3620+
* Bind a function declaration in the block holding it, where it is lexical
3621+
* even when sloppy-mode Annex B also hoists it.
3622+
* @param {FunctionDeclaration | MaybeNamedFunctionDeclaration} statement function declaration
3623+
*/
3624+
blockPreWalkFunctionDeclaration(statement) {
3625+
if (statement.id) this.defineVariable(statement.id.name);
3626+
}
3627+
35813628
/**
35823629
* Walk function declaration.
35833630
* @param {FunctionDeclaration | MaybeNamedFunctionDeclaration} statement function declaration

‎lib/javascript/syntax-parser.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7938,6 +7938,7 @@ const declaresIntoBlock = (statement) => {
79387938
const type = statement.type;
79397939
if (
79407940
type === "VariableDeclaration" ||
7941+
type === "FunctionDeclaration" ||
79417942
type === "ClassDeclaration" ||
79427943
type === "ExportNamedDeclaration" ||
79437944
type === "ExportDefaultDeclaration"
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
function strictOuter() {
2+
"use strict";
3+
{
4+
function require() {}
5+
}
6+
return require("./lib");
7+
}
8+
9+
function strictInner() {
10+
"use strict";
11+
{
12+
function require(request) {
13+
return "local " + request;
14+
}
15+
return require("./lib");
16+
}
17+
}
18+
19+
function sloppyAsync() {
20+
{
21+
async function require() {}
22+
}
23+
return require("./lib");
24+
}
25+
26+
function sloppyGenerator() {
27+
{
28+
function* require() {}
29+
}
30+
return require("./lib");
31+
}
32+
33+
function sloppyAnnexB() {
34+
{
35+
function require() {
36+
return "annex b";
37+
}
38+
}
39+
return require("./lib");
40+
}
41+
42+
module.exports = {
43+
strictOuter: strictOuter(),
44+
strictInner: strictInner(),
45+
sloppyAsync: sloppyAsync(),
46+
sloppyGenerator: sloppyGenerator(),
47+
sloppyAnnexB: sloppyAnnexB()
48+
};

‎test/configCases/parsing/custom-parser-hoisting/index.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { alias, reexported } from "./barrel";
22

33
const hoisting = require("./hoisting");
44
const blockDeclarations = require("./block-declarations");
5+
const blockFunctions = require("./block-functions");
56

67
it("should hoist from every position with a custom parser", () => {
78
expect(hoisting).toEqual({
@@ -47,6 +48,16 @@ it("should bind every block position with a custom parser", () => {
4748
});
4849
});
4950

51+
it("should scope a strict or async block function to its block with a custom parser", () => {
52+
expect(blockFunctions).toEqual({
53+
strictOuter: "lib",
54+
strictInner: "local ./lib",
55+
sloppyAsync: "lib",
56+
sloppyGenerator: "lib",
57+
sloppyAnnexB: "annex b"
58+
});
59+
});
60+
5061
it("should read imports and re-exports with a custom parser", () => {
5162
expect(alias).toBe("named");
5263
expect(reexported).toBe("reexported");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "lib";
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
function strictOuter() {
2+
"use strict";
3+
{
4+
function require() {}
5+
}
6+
return require("./lib");
7+
}
8+
9+
function strictInner() {
10+
"use strict";
11+
{
12+
function require(request) {
13+
return "local " + request;
14+
}
15+
return require("./lib");
16+
}
17+
}
18+
19+
function sloppyAsync() {
20+
{
21+
async function require() {}
22+
}
23+
return require("./lib");
24+
}
25+
26+
function sloppyGenerator() {
27+
{
28+
function* require() {}
29+
}
30+
return require("./lib");
31+
}
32+
33+
function sloppyAnnexB() {
34+
{
35+
function require() {
36+
return "annex b";
37+
}
38+
}
39+
return require("./lib");
40+
}
41+
42+
module.exports = {
43+
strictOuter: strictOuter(),
44+
strictInner: strictInner(),
45+
sloppyAsync: sloppyAsync(),
46+
sloppyGenerator: sloppyGenerator(),
47+
sloppyAnnexB: sloppyAnnexB()
48+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { viaImport, viaRequire } from "./module";
2+
3+
const blockFunctions = require("./block-functions");
4+
5+
it("should scope a strict or async block function to its block", () => {
6+
expect(blockFunctions).toEqual({
7+
strictOuter: "lib",
8+
strictInner: "local ./lib",
9+
sloppyAsync: "lib",
10+
sloppyGenerator: "lib",
11+
sloppyAnnexB: "annex b"
12+
});
13+
});
14+
15+
it("should scope a block function to its block in a module", () => {
16+
expect(viaImport).toBe("lib");
17+
expect(viaRequire).toBe("lib");
18+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "lib";
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import lib from "./lib";
2+
3+
{
4+
function require() {}
5+
}
6+
7+
export const viaImport = lib;
8+
export const viaRequire = require("./lib");

0 commit comments

Comments
 (0)