Skip to content

Commit ac50b7f

Browse files
fix(parser): keep block functions lexical in the full pre-walk too
A tap on the deprecated preStatement hook walks nested blocks, where the typed hooks declared a strict block function in the enclosing scope.
1 parent 6966d15 commit ac50b7f

2 files changed

Lines changed: 45 additions & 5 deletions

File tree

‎lib/javascript/JavascriptParser.js‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,7 @@ const hoistedDeclarationsOf = (node, trusted) => {
11271127
* Whether a statement is one of the list's own rather than nested in one of
11281128
* them, found by its start offset since the list is in source order.
11291129
* @param {(Statement | ModuleDeclaration)[]} statements the list
1130-
* @param {Statement | ModuleDeclaration} statement the statement to find
1130+
* @param {Statement | ModuleDeclaration | MaybeNamedFunctionDeclaration} statement the statement to find
11311131
* @returns {boolean} whether the list holds it directly
11321132
*/
11331133
const holdsStatement = (statements, statement) => {
@@ -1514,6 +1514,9 @@ class JavascriptParser extends Parser {
15141514
// webpack's own plugins drive ever asks for.
15151515
/** @type {boolean} */
15161516
this._needsFullPreWalk = false;
1517+
// the statements of the scope the full pre-walk is declaring into
1518+
/** @type {(Statement | ModuleDeclaration)[] | undefined} */
1519+
this._fullPreWalkStatements = undefined;
15171520
this.hooks.preStatementByType.intercept({
15181521
factory: (type, hook) => {
15191522
if (!RECORDED_STATEMENT_TYPES.has(type)) {
@@ -2753,8 +2756,11 @@ class JavascriptParser extends Parser {
27532756
_preWalkScope(node) {
27542757
// BACKWARD-COMPAT START
27552758
if (this._needsFullPreWalk || this.hooks.preStatement.taps.length !== 0) {
2759+
const outer = this._fullPreWalkStatements;
2760+
this._fullPreWalkStatements = node.body;
27562761
if (node.type === "Program") this.preWalkStatements(node.body);
27572762
else this.preWalkStatement(node);
2763+
this._fullPreWalkStatements = outer;
27582764
return;
27592765
}
27602766
// BACKWARD-COMPAT END
@@ -3076,8 +3082,21 @@ class JavascriptParser extends Parser {
30763082
let bail =
30773083
preStatement.taps.length !== 0 ? preStatement.call(statement) : undefined;
30783084
if (!bail) {
3079-
const typed = preStatementByType.get(statement.type);
3080-
if (typed !== undefined) bail = typed.call(statement);
3085+
const statements = this._fullPreWalkStatements;
3086+
// WHY: the full pre-walk also reaches nested blocks, where a function
3087+
// is lexical unless Annex B hoists it; as in the hoisting pass, its
3088+
// typed hooks and declaration are skipped, only `preStatement` sees it.
3089+
if (
3090+
statements !== undefined &&
3091+
statement.type === "FunctionDeclaration" &&
3092+
(this.scope.isStrict || statement.generator || statement.async) &&
3093+
!holdsStatement(statements, statement)
3094+
) {
3095+
bail = true;
3096+
} else {
3097+
const typed = preStatementByType.get(statement.type);
3098+
if (typed !== undefined) bail = typed.call(statement);
3099+
}
30813100
}
30823101
if (bail) {
30833102
this.prevStatement =
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
11
"use strict";
22

3-
/** @type {import("../../../../").Configuration} */
4-
module.exports = {};
3+
/** @type {import("../../../../").Configuration[]} */
4+
module.exports = [
5+
{},
6+
{
7+
plugins: [
8+
{
9+
apply(compiler) {
10+
compiler.hooks.compilation.tap(
11+
"Test",
12+
(compilation, { normalModuleFactory }) => {
13+
// a tap on the deprecated hook turns on the full pre-walk
14+
normalModuleFactory.hooks.parser
15+
.for("javascript/auto")
16+
.tap("Test", (parser) => {
17+
parser.hooks.preStatement.tap("Test", () => undefined);
18+
});
19+
}
20+
);
21+
}
22+
}
23+
]
24+
}
25+
];

0 commit comments

Comments
 (0)