@@ -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 */
11331133const 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 =
0 commit comments