@@ -182,6 +182,7 @@ const EMPTY_MODULE_DECLARATIONS = [];
182182// object pattern, so a tap on that type is not served from the list.
183183const 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
0 commit comments