Skip to content

Commit

Permalink
Fix NodePath.referencesImport for JSXMemberExpression (#14403)
Browse files Browse the repository at this point in the history
* add failing test

* fix

* add Expression alias for JSXMemberExpression & update condition

* make this NodePath<Node> instead

* remove useless type assertion
  • Loading branch information
swandir committed Mar 31, 2022
1 parent 9cb5f6c commit b7a7c65
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
12 changes: 7 additions & 5 deletions packages/babel-traverse/src/path/introspection.ts
Expand Up @@ -175,16 +175,18 @@ export function isStatementOrBlock(this: NodePath): boolean {
*/

export function referencesImport(
this: NodePath<t.Expression>,
this: NodePath,
moduleSource: string,
importName: string,
): boolean {
if (!this.isReferencedIdentifier()) {
if (
(this.isMemberExpression() || this.isOptionalMemberExpression()) &&
(this.node.computed
? isStringLiteral(this.node.property, { value: importName })
: (this.node.property as t.Identifier).name === importName)
(this.isJSXMemberExpression() &&
this.node.property.name === importName) ||
((this.isMemberExpression() || this.isOptionalMemberExpression()) &&
(this.node.computed
? isStringLiteral(this.node.property, { value: importName })
: (this.node.property as t.Identifier).name === importName))
) {
const object = (
this as NodePath<t.MemberExpression | t.OptionalMemberExpression>
Expand Down
8 changes: 8 additions & 0 deletions packages/babel-traverse/test/introspection.js
Expand Up @@ -161,6 +161,14 @@ describe("path/introspection", function () {
const reference = program.get("body.1.expression");
expect(reference.referencesImport("source", "馃槄")).toBe(true);
});
it("accepts a named import via a namespace import jsx member expression", function () {
const program = getPath(`import * as ns from "source"; <ns.dep />;`, {
sourceType: "module",
plugins: ["jsx"],
});
const reference = program.get("body.1.expression.openingElement.name");
expect(reference.referencesImport("source", "dep")).toBe(true);
});
it("rejects a named import from the wrong module", function () {
const program = getPath(`import { dep } from "wrong-source"; dep;`, {
sourceType: "module",
Expand Down

0 comments on commit b7a7c65

Please sign in to comment.