Skip to content

feat(referencesImport): support named exports accessed via namespace imports#12603

Merged
nicolo-ribaudo merged 1 commit intobabel:mainfrom
jeysal:references-import
Feb 21, 2021
Merged

feat(referencesImport): support named exports accessed via namespace imports#12603
nicolo-ribaudo merged 1 commit intobabel:mainfrom
jeysal:references-import

Conversation

@jeysal
Copy link
Copy Markdown
Contributor

@jeysal jeysal commented Jan 10, 2021

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

See referenced issue (or tests) for explanation/example.
Not really a bug fix because it was unclear how smart referencesImport is - this makes it a bit less limited.
We could of course go even further, support some cases of computed properties, etc., but I'd argue this is a pretty good line to draw because ns.dep is by far the most common way to access a named import via a namespace import object.
Don't think I have permission to request reviews here so cc @nicolo-ribaudo 😅

@codesandbox-ci
Copy link
Copy Markdown

codesandbox-ci Bot commented Jan 10, 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 191a654:

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

@jeysal
Copy link
Copy Markdown
Contributor Author

jeysal commented Jan 10, 2021

Only took me 3/4 of a year to follow up on the issue 🙈

@babel-bot
Copy link
Copy Markdown
Collaborator

babel-bot commented Jan 10, 2021

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

@JLHwung
Copy link
Copy Markdown
Contributor

JLHwung commented Jan 11, 2021

I don't think NodePath#referencesImport should extend to named imports because it is mutable, although the imported namespace binding is constant. For example

import * as ns from "some-module";
ns.foo = "foo";

// error: can not reassign "ns"
ns = anotherNs

Should NodePath("ns.foo").referencesImport("some-module", "foo") return true? While we are sure * is a referenced import from "some-module", we don't know if "foo" is exported from "some-module" without looking into the exports of "some-module".

@jeysal
Copy link
Copy Markdown
Contributor Author

jeysal commented Jan 11, 2021

@JLHwung as far as I know, this is not the case? Maybe I'm missing something.
At least in Node 14.15.1, which I've used to test, trying to override an existing namespace property fails with

TypeError: Cannot assign to read only property 'prop' of object '[object Module]'

and trying to add one fails with

TypeError: Cannot add property prop2, object is not extensible

@jeysal
Copy link
Copy Markdown
Contributor Author

jeysal commented Jan 11, 2021

I've checked the spec and these 'module namespace exotic objects' are indeed non-extensible, and their [[Set]] does nothing but Returns false. Phew, for a moment I was scared that the spec actually allowed mutating them, which would be terrible for anyone who wants to do some static analysis 😅

Comment thread packages/babel-traverse/src/path/introspection.js Outdated
Comment thread packages/babel-traverse/src/path/introspection.js Outdated
@nicolo-ribaudo nicolo-ribaudo added the PR: New Feature 🚀 A type of pull request used for our changelog categories label Jan 14, 2021
@jeysal
Copy link
Copy Markdown
Contributor Author

jeysal commented Jan 15, 2021

Both done - lmk if this needs anything else

Comment thread packages/babel-traverse/src/path/introspection.js Outdated
@nicolo-ribaudo nicolo-ribaudo added this to the v7.13.0 milestone Jan 15, 2021
@nicolo-ribaudo
Copy link
Copy Markdown
Member

The failing e2e tests are not related, and are probably caused by a slightly old base branch.

@jeysal
Copy link
Copy Markdown
Contributor Author

jeysal commented Jan 15, 2021

Would you like me to rebase?

@nicolo-ribaudo
Copy link
Copy Markdown
Member

Yes please!

@jeysal jeysal force-pushed the references-import branch from 828dfa0 to f22c8b5 Compare January 15, 2021 14:54
@jeysal
Copy link
Copy Markdown
Contributor Author

jeysal commented Jan 15, 2021

Done, it seems this was because it was based off of master, because my repo was from before the main branch name change

Comment thread packages/babel-traverse/test/introspection.js
Comment thread packages/babel-traverse/src/path/introspection.js Outdated
@existentialism existentialism added the PR: Ready to be Merged A pull request with already two approvals, but waiting for the next minor release label Jan 18, 2021
…imports

* test(traverse): referencesImport
* feat(referencesImport): support named exports accessed via namespace imports
* feat(referencesImport): support `namespaceObj?.importName` and `namespaceObj["importName"]`
* refactor(referencesImport): simplify member expression computed branches
* test(referencesImport): aliased named import case
* fix(referencesImport): do not be fooled by import name "*"
  at least when detecting accessing a named import via a '*' import

Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This was referenced Mar 14, 2021
@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 May 24, 2021
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators May 24, 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 PR: New Feature 🚀 A type of pull request used for our changelog categories PR: Ready to be Merged A pull request with already two approvals, but waiting for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

support simple namespace import cases in referencesImport helper

6 participants