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

fix: accept duplicated import/variable in different module #13527

Merged
merged 3 commits into from Jul 6, 2021

Conversation

colinaaa
Copy link
Contributor

@colinaaa colinaaa commented Jul 3, 2021

Q                       A
Fixed Issues? Fixes #13510
Patch: Bug Fix? 👍
Major: Breaking Change? Not sure
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

Declaration(path) {
// delegate block scope handling to the `BlockScoped` method
if (path.isBlockScoped()) return;
// this will be hit again once we traverse into it after this iteration
if (path.isExportDeclaration()) return;
// we've ran into a declaration!
const parent =
path.scope.getFunctionParent() || path.scope.getProgramParent();
parent.registerDeclaration(path);
},

I make all ImportDeclaration isBlockScoped, so that it will getBlockParent instead of getProgramParent (which leads to conflicts).

I'm not sure this is the best way to do so, but since Declaration and BlockScoped visitor of collectorVisitor are doing similar things, it sounds like pretty good to me.

According to the test, this change did not bring any breaking change. However, I not so familiar with babel codebase. So there are possibilities that this PR is breaking something.

Let me know if this approach is not correct / breaking, I will try to find another way out.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jul 3, 2021

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 4f1c802:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

@babel-bot
Copy link
Collaborator

babel-bot commented Jul 3, 2021

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/47208/

Comment on lines 17 to 18
// import declaration can be block scoped when it's in TS/flow module declaration
isImportDeclaration(node)
Copy link
Member

Choose a reason for hiding this comment

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

Actually it is not block scoped as ImportDeclarations may only appear in the top level or inside a module/namespace (for TS). Adding it here may be a breaking change for someone using the validator.
Furthermore excluding ImportDeclarations from

Declaration(path) {
// delegate block scope handling to the `BlockScoped` method
if (path.isBlockScoped()) return;
// this will be hit again once we traverse into it after this iteration
if (path.isExportDeclaration()) return;
// we've ran into a declaration!
const parent =
path.scope.getFunctionParent() || path.scope.getProgramParent();
parent.registerDeclaration(path);
},

results in excluding those nodes from the traversal and not running registerDeclaration on them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think excluding ImportDeclarations is well enough, since it will also cause breaking changes.

The main problem here is that the scope of declared module should be isolated with other modules' scopes and the program's scope. But unfortunately it's not now.

So maybe we could change the implementation of getProgramParent to make it returns not only on Program, but also ModuleDeclaration. This will make scopes of modules isolated with each other and the program.

Copy link
Member

Choose a reason for hiding this comment

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

Yep, excluding ImportDeclaration is definitely not the right way here.
You will be happy to know that there is already a method called getBlockParent that gets the upper block. However I would suggest you to give a look at how the BlockScoped visitor is handling block scoped declarations, because we may have to replicate a similar behavior for ImportDeclaration.

Copy link
Contributor

Choose a reason for hiding this comment

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

make it returns not only on Program, but also ModuleDeclaration.

I don't think we can mark ModuleDeclaration as program because variables within module declaration can reference bindings defined in program:

type T = number;
declare module 'test' {
  declare var a: T;
}

Since ambient declarations are always top level. We can check if node.parent is ModuleDeclaration and then register declarations there, otherwise we fallback to

const parent =
path.scope.getFunctionParent() || path.scope.getProgramParent();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I took a different approach.

Take all ImportDeclaration into a separated visitor, and use getBlockParent to find parent scope.

I think it is more conducive to maintenance。

@fedeci fedeci added pkg: traverse (scope) PR: Bug Fix 🐛 A type of pull request used for our changelog categories labels Jul 3, 2021
@colinaaa colinaaa requested review from fedeci and JLHwung July 6, 2021 08:35
packages/babel-traverse/src/scope/index.ts Outdated Show resolved Hide resolved
Co-authored-by: Huáng Jùnliàng <jlhwung@gmail.com>
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

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

Thanks!

@nicolo-ribaudo nicolo-ribaudo merged commit a5a63e3 into babel:main Jul 6, 2021
nicolo-ribaudo pushed a commit to nicolo-ribaudo/babel that referenced this pull request Jul 30, 2021
Co-authored-by: Huáng Jùnliàng <jlhwung@gmail.com>
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 6, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: traverse (scope) PR: Bug Fix 🐛 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: babel fails on flow definition file, if modules in it have imports with the same name
5 participants