Skip to content

Commit

Permalink
Improve relative execution tracking in fn exprs (#15275)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Dec 15, 2022
1 parent 382d284 commit 6a45b88
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
@@ -0,0 +1,5 @@
f(() => a);
let a;

let b;
f(() => b);
@@ -0,0 +1,5 @@
var a = babelHelpers.temporalUndefined;
f(() => babelHelpers.temporalRef(a, "a"));
a = void 0;
var b;
f(() => b);
15 changes: 10 additions & 5 deletions packages/babel-traverse/src/path/introspection.ts
Expand Up @@ -267,7 +267,10 @@ export function willIMaybeExecuteBefore(
}

function getOuterFunction(path: NodePath) {
return (path.scope.getFunctionParent() || path.scope.getProgramParent()).path;
return (
path.parentPath.scope.getFunctionParent() ||
path.parentPath.scope.getProgramParent()
).path;
}

function isExecutionUncertain(type: t.Node["type"], key: string) {
Expand Down Expand Up @@ -445,10 +448,12 @@ function _guessExecutionStatusRelativeToDifferentFunctionsInternal(
target: NodePath,
cache: ExecutionStatusCache,
): RelativeExecutionStatus {
if (
!target.isFunctionDeclaration() ||
target.parentPath.isExportDeclaration()
) {
if (!target.isFunctionDeclaration()) {
if (base._guessExecutionStatusRelativeTo(target) === "before") {
return "before";
}
return "unknown";
} else if (target.parentPath.isExportDeclaration()) {
return "unknown";
}

Expand Down
16 changes: 16 additions & 0 deletions packages/babel-traverse/test/introspection.js
Expand Up @@ -213,4 +213,20 @@ describe("path/introspection", function () {
expect(reference.referencesImport("source", "*")).toBe(false);
});
});

describe("_guessExecutionStatusRelativeTo", function () {
it("works withs paths in function expressions", () => {
const program = getPath(`
a;
f(() => b);
c;
`);
const a = program.get("body.0.expression");
const b = program.get("body.1.expression.arguments.0.body");
const c = program.get("body.2.expression");

expect(a._guessExecutionStatusRelativeTo(b)).toBe("before");
expect(c._guessExecutionStatusRelativeTo(b)).toBe("unknown");
});
});
});

0 comments on commit 6a45b88

Please sign in to comment.