Skip to content

Commit

Permalink
fix(referencesImport): do not be fooled by import name "*"
Browse files Browse the repository at this point in the history
at least when detecting accessing a named import via a '*' import
  • Loading branch information
jeysal committed Jan 16, 2021
1 parent ad2e2d1 commit 1b232a5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/babel-traverse/src/path/introspection.js
Expand Up @@ -164,7 +164,11 @@ export function referencesImport(moduleSource, importName) {
? t.isStringLiteral(this.node.property, { value: importName })
: this.node.property.name === importName)
) {
return this.get("object").referencesImport(moduleSource, "*");
const object = this.get("object");
return (
object.isReferencedIdentifier() &&
object.referencesImport(moduleSource, "*")
);
}

return false;
Expand Down
8 changes: 8 additions & 0 deletions packages/babel-traverse/test/introspection.js
Expand Up @@ -173,6 +173,14 @@ describe("path/introspection", function () {
const reference = program.get("body.1.expression");
expect(reference.referencesImport("source", "dep")).toBe(false);
});
it('rejects the "export called *" trick', function () {
const program = getPath(`import * as ns from "source"; ns["*"].nested;`, {
sourceType: "module",
plugins: ["moduleStringNames"],
});
const reference = program.get("body.1.expression");
expect(reference.referencesImport("source", "nested")).toBe(false);
});

it("accepts a namespace import", function () {
const program = getPath(`import * as dep from "source"; dep;`, {
Expand Down

0 comments on commit 1b232a5

Please sign in to comment.