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: register injected importDeclaration #10172
Conversation
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/11064/ |
eac1b1c
to
c301efb
Compare
c301efb
to
c092c1e
Compare
@@ -35,6 +35,11 @@ export default declare(api => { | |||
|
|||
path.replaceWithMultiple(nodes); | |||
}, | |||
ImportDefaultSpecifier(path) { | |||
if (!path.scope.hasOwnBinding(path.node.local.name)) { | |||
path.scope.registerDeclaration(path.parentPath); |
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.
I would prefer to do this as soon as the import declaration is created, in the ExportNamedDeclaration
visitor. This makes it clearer why we are doing it, and it prevents other plugin from running between the creating the declaration and registering it.
path.replaceWithMultiple(nodes);
returns an array containing the new inserted paths: the first one should be the import declaration, and you can get it's default specifier.
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.
path.replaceWithMultiple(nodes)
returns an array containing the new inserted paths.
Yes it is. It seems that @types/babel__traverse
declares out-of-dated signature here.
When
transform-typescript
combines withproposal-export-default-from
, theexport { _foo as foo };
will be erased as it seems that_foo
is not defined in its own scope. This PR register the injected importDeclaration so that it can work well with elideImports fromtransform-typescript
.Note that #7592 uses
scope.crawl()
to manually sync nodes to AST. Ascrawl
will incur great performance overkill, our approach check the injected import specifier only and register when necessary.