Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve relative execution tracking in fn exprs #15275

Merged
merged 1 commit into from Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On main we would also inject the TDZ checks for b, because our tracking would return "unknown" at function expression boundaries.

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");
});
});
});