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 NodePath.referencesImport for JSXMemberExpression #14403

Merged
merged 5 commits into from Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions packages/babel-traverse/src/path/introspection.ts
Expand Up @@ -181,10 +181,12 @@ export function referencesImport(
): 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 as t.JSXIdentifier).name === importName) ||
((this.isMemberExpression() || this.isOptionalMemberExpression()) &&
(this.node.computed
? isStringLiteral(this.node.property, { value: importName })
: (this.node.property as t.Identifier).name === importName))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSXMemberExpression is never computed

) {
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
1 change: 1 addition & 0 deletions packages/babel-types/src/ast-types/generated/index.ts
Expand Up @@ -2156,6 +2156,7 @@ export type Expression =
| OptionalCallExpression
| TypeCastExpression
| JSXElement
| JSXMemberExpression
| JSXFragment
| BindExpression
| DoExpression
Expand Down
1 change: 1 addition & 0 deletions packages/babel-types/src/definitions/jsx.ts
Expand Up @@ -107,6 +107,7 @@ defineType("JSXIdentifier", {

defineType("JSXMemberExpression", {
visitor: ["object", "property"],
aliases: ["Expression"],
swandir marked this conversation as resolved.
Show resolved Hide resolved
fields: {
object: {
validate: assertNodeType("JSXMemberExpression", "JSXIdentifier"),
Expand Down
1 change: 1 addition & 0 deletions packages/babel-types/src/validators/generated/index.ts
Expand Up @@ -4357,6 +4357,7 @@ export function isExpression(
"OptionalCallExpression" === nodeType ||
"TypeCastExpression" === nodeType ||
"JSXElement" === nodeType ||
"JSXMemberExpression" === nodeType ||
"JSXFragment" === nodeType ||
"BindExpression" === nodeType ||
"DoExpression" === nodeType ||
Expand Down