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
Copy link

@codesandbox codesandbox 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 babel-bot commented Jul 3, 2021

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

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

This comment has been minimized.

@fedeci

fedeci Jul 3, 2021
Member

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.

This comment has been minimized.

@colinaaa

colinaaa Jul 3, 2021
Author Contributor

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.

This comment has been minimized.

@fedeci

fedeci Jul 3, 2021
Member

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.

This comment has been minimized.

@JLHwung

JLHwung Jul 5, 2021
Contributor

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();

This comment has been minimized.

@colinaaa

colinaaa Jul 6, 2021
Author Contributor

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。

@colinaaa colinaaa requested review from fedeci and JLHwung Jul 6, 2021
@fedeci
fedeci approved these changes Jul 6, 2021
@JLHwung
JLHwung approved these changes Jul 6, 2021
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

Thanks!

@nicolo-ribaudo nicolo-ribaudo merged commit a5a63e3 into babel:main Jul 6, 2021
25 of 27 checks passed
25 of 27 checks passed
@github-actions
Prepare Cache
Details
@github-actions
Validate Yarn dependencies and constraints
Details
@github-actions
Test on Node.js Latest
Details
@github-actions
Build Babel Artifacts
Details
@github-actions
Test Babel 8 breaking changes
Details
@github-actions
Publish to local Verdaccio registry
Details
@github-actions
Lint
Details
@github-actions
Test on Node.js (14)
Details
@github-actions
Test on Node.js (12)
Details
@github-actions
Test on Node.js (10)
Details
@github-actions
Test on Node.js (8)
Details
@github-actions
Test on Node.js (6)
Details
@github-actions
Test on Windows
Details
@github-actions
Third-party Parser Tests
Details
@github-actions
Test @babel/runtime integrations
Details
@github-actions
E2E (babel)
Details
@github-actions
E2E (babel-old-version)
Details
@github-actions
E2E (create-react-app)
Details
@github-actions
E2E (vue-cli)
Details
@github-actions
E2E (jest)
Details
@circleci-checks
e2e-breaking-pr Workflow: e2e-breaking-pr
Details
@circleci-checks
test262-pr Workflow: test262-pr
Details
@gitpod-io
Gitpod Open an online workspace in Gitpod
Details
@babel-bot
babel/repl REPL preview is available
Details
@circleci-checks
build-standalone Workflow: build-standalone
Details
@codesandbox
ci/codesandbox Building packages succeeded.
Details
@codecov
codecov/project 92.04% (target 90.00%)
Details
nicolo-ribaudo added 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

5 participants