Skip to content

Commit 6a45b88

Browse files
Improve relative execution tracking in fn exprs (#15275)
1 parent 382d284 commit 6a45b88

4 files changed

Lines changed: 36 additions & 5 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
f(() => a);
2+
let a;
3+
4+
let b;
5+
f(() => b);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var a = babelHelpers.temporalUndefined;
2+
f(() => babelHelpers.temporalRef(a, "a"));
3+
a = void 0;
4+
var b;
5+
f(() => b);

packages/babel-traverse/src/path/introspection.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,10 @@ export function willIMaybeExecuteBefore(
267267
}
268268

269269
function getOuterFunction(path: NodePath) {
270-
return (path.scope.getFunctionParent() || path.scope.getProgramParent()).path;
270+
return (
271+
path.parentPath.scope.getFunctionParent() ||
272+
path.parentPath.scope.getProgramParent()
273+
).path;
271274
}
272275

273276
function isExecutionUncertain(type: t.Node["type"], key: string) {
@@ -445,10 +448,12 @@ function _guessExecutionStatusRelativeToDifferentFunctionsInternal(
445448
target: NodePath,
446449
cache: ExecutionStatusCache,
447450
): RelativeExecutionStatus {
448-
if (
449-
!target.isFunctionDeclaration() ||
450-
target.parentPath.isExportDeclaration()
451-
) {
451+
if (!target.isFunctionDeclaration()) {
452+
if (base._guessExecutionStatusRelativeTo(target) === "before") {
453+
return "before";
454+
}
455+
return "unknown";
456+
} else if (target.parentPath.isExportDeclaration()) {
452457
return "unknown";
453458
}
454459

packages/babel-traverse/test/introspection.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,20 @@ describe("path/introspection", function () {
213213
expect(reference.referencesImport("source", "*")).toBe(false);
214214
});
215215
});
216+
217+
describe("_guessExecutionStatusRelativeTo", function () {
218+
it("works withs paths in function expressions", () => {
219+
const program = getPath(`
220+
a;
221+
f(() => b);
222+
c;
223+
`);
224+
const a = program.get("body.0.expression");
225+
const b = program.get("body.1.expression.arguments.0.body");
226+
const c = program.get("body.2.expression");
227+
228+
expect(a._guessExecutionStatusRelativeTo(b)).toBe("before");
229+
expect(c._guessExecutionStatusRelativeTo(b)).toBe("unknown");
230+
});
231+
});
216232
});

0 commit comments

Comments
 (0)