-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Migrate some duplicate binding tests to traverse #9532
Conversation
@@ -349,9 +349,6 @@ export default class Scope { | |||
// class expression | |||
if (local.kind === "local") return; | |||
|
|||
// ignore hoisted functions if there's also a local let | |||
if (kind === "hoisted" && local.kind === "let") return; | |||
|
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.
For some reason we have this exception in the code for the case mentioned in the PR. I couldn't come up with a reason why we do this and the commit that introduced it doesn't help either 8a4296a
Simply removing it also doesn't break any of the tests.
Do you think it is safe to remove? Anyone has an idea why this code was there and which case it covers?
To avoid confusion: kind === "hoisted"
are function declarations. Should be renamed I think, but that would be definitely breaking.
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.
Maybe it was this? It is an Annex B thing:
let a;
{ function a() {} }
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.
right but even with removing these lines, this case still works. will add a test.
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/10156/ |
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/10154/ |
Maybe it's worth using |
Oh I have never seen this. makes sense. |
Nevermind this does not work obviously with loops and generated testcases. |
}); | ||
|
||
describe("global", () => { | ||
["let", "const"].forEach(name => { |
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.
We should also test class/function
and class/var
(and maybe function/var
to be sure that it doesn't throw)
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.
Okay I added a more generic way to define tests.
Turns out we have some problems, unless I made a mistake in the tests.
var foo = 1;
class foo {};
and
class foo {};
var foo = 1;
Class and var never throw.
also does not throw:
function foo() {}
const foo = 1;
var foo = 1;
const foo = 1;
Thats why I disabled the reverse matching in the test, because swapping the two statements does throw.
It seems the scope tracking in traverse has some flaws. I guess we should have a look separately from this. I can maybe work on this once done with the scope tracking in the parser. |
Yeah let's just update the tests for now.
traverse's scope tracker needs to be kept updated while parser's scope describes a static AST; I don't know how feasible it would be. |
But in the end both implementations (parser and traverse) work on nodes, have a stack of scopes, add/remove bindings and check for duplicates. But yes there might be more stuff going on in traverse which I'm not yet aware of. |
Fixes #1, Fixes #2
The main goal here was to transfer all the tests which test duplicate bindings. They were in different transforms, although the code was in
@babel/traverse
and the transforms have no impact on that behavior, and imho also shouldn't.While doing this I discovered, that on case wasn't throwing an error:
see inline comment.