Skip to content

Commit b7a7c65

Browse files
authored
Fix NodePath.referencesImport for JSXMemberExpression (#14403)
* add failing test * fix * add Expression alias for JSXMemberExpression & update condition * make this NodePath<Node> instead * remove useless type assertion
1 parent 9cb5f6c commit b7a7c65

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

packages/babel-traverse/src/path/introspection.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,18 @@ export function isStatementOrBlock(this: NodePath): boolean {
175175
*/
176176

177177
export function referencesImport(
178-
this: NodePath<t.Expression>,
178+
this: NodePath,
179179
moduleSource: string,
180180
importName: string,
181181
): boolean {
182182
if (!this.isReferencedIdentifier()) {
183183
if (
184-
(this.isMemberExpression() || this.isOptionalMemberExpression()) &&
185-
(this.node.computed
186-
? isStringLiteral(this.node.property, { value: importName })
187-
: (this.node.property as t.Identifier).name === importName)
184+
(this.isJSXMemberExpression() &&
185+
this.node.property.name === importName) ||
186+
((this.isMemberExpression() || this.isOptionalMemberExpression()) &&
187+
(this.node.computed
188+
? isStringLiteral(this.node.property, { value: importName })
189+
: (this.node.property as t.Identifier).name === importName))
188190
) {
189191
const object = (
190192
this as NodePath<t.MemberExpression | t.OptionalMemberExpression>

packages/babel-traverse/test/introspection.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ describe("path/introspection", function () {
161161
const reference = program.get("body.1.expression");
162162
expect(reference.referencesImport("source", "😅")).toBe(true);
163163
});
164+
it("accepts a named import via a namespace import jsx member expression", function () {
165+
const program = getPath(`import * as ns from "source"; <ns.dep />;`, {
166+
sourceType: "module",
167+
plugins: ["jsx"],
168+
});
169+
const reference = program.get("body.1.expression.openingElement.name");
170+
expect(reference.referencesImport("source", "dep")).toBe(true);
171+
});
164172
it("rejects a named import from the wrong module", function () {
165173
const program = getPath(`import { dep } from "wrong-source"; dep;`, {
166174
sourceType: "module",

0 commit comments

Comments
 (0)