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
fix: infer array type from outside of arrow function #12727
base: main
Are you sure you want to change the base?
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 20fad3c:
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/41225/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also add some tests at packages/babel-traverse/test/inference.js
?
This REPL doesn't seem to work, I get this error:
|
Yeah I know, it should be fixed by #12728 😅 |
I don't know why the CI failed and I cannot reproduce it locally. If I run // A possible fix?
((target.parent as t.VariableDeclarator).id as t.Identifier).name |
Since #12728 has been merged could you rebase this PR so the REPL works please |
8022e40
to
30ab283
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EDIT by @nicolo-ribaudo: spam
Tested and it does indeed fix the REPL provided in the issue, but when applied on the real code I extracted it from it doesn't as it returns the function that was created // Fixed in this PR
{
const keys = [];
const foo = () => {
for (const key of keys) {
}
};
}
// Same output as before
{
const foo = () => {
const keys = [];
const bar = () => {
for (const key of keys) {
}
};
return bar;
};
} |
The |
Can also be reproduced if the function is exported - REPL const keys = [];
export const foo = () => {
for (const key of keys) {
}
}; |
Exported function execution status is not guessed since #10417 and I think we should check it only when // Don't infer type
var keys = []
const foo = () => {
for (const key of keys) {}
}
// Infer type
const keys = []
export const foo = () => {
for (const key of keys) {}
}
// Infer type
export const bar = () => {
const keys = []
for (const key of keys) {}
}
// Don't infer type
var keys = []
export const baz = () => {
for (const key of keys) {}
} /cc @nicolo-ribaudo |
In that case |
cebc85b
to
20fad3c
Compare
If
ArrowFunctionExpression
is a child ofVariableDeclarator
we can try to guess the execution status relative to another expression. This allows to infer array type from outside of an arrow function.