Skip to content

fix(scope-manager): add a reference for JSX closing element if it exists #10614

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions packages/scope-manager/src/referencer/Referencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,26 @@ class Referencer extends Visitor {
}
}

protected visitJSXElement(
node: TSESTree.JSXClosingElement | TSESTree.JSXOpeningElement,
): void {
if (node.name.type === AST_NODE_TYPES.JSXIdentifier) {
if (
node.name.name[0].toUpperCase() === node.name.name[0] ||
node.name.name === 'this'
) {
// lower cased component names are always treated as "intrinsic" names, and are converted to a string,
// not a variable by JSX transforms:
// <div /> => React.createElement("div", null)

// the only case we want to visit a lower-cased component has its name as "this",
this.visit(node.name);
}
} else {
this.visit(node.name);
}
}

protected visitProperty(node: TSESTree.Property): void {
if (node.computed) {
this.visit(node.key);
Expand Down Expand Up @@ -497,8 +517,8 @@ class Referencer extends Visitor {
this.visit(node.value);
}

protected JSXClosingElement(): void {
// should not be counted as a reference
protected JSXClosingElement(node: TSESTree.JSXClosingElement): void {
this.visitJSXElement(node);
}

protected JSXFragment(node: TSESTree.JSXFragment): void {
Expand All @@ -522,21 +542,7 @@ class Referencer extends Visitor {
}
protected JSXOpeningElement(node: TSESTree.JSXOpeningElement): void {
this.referenceJsxPragma();
if (node.name.type === AST_NODE_TYPES.JSXIdentifier) {
if (
node.name.name[0].toUpperCase() === node.name.name[0] ||
node.name.name === 'this'
) {
// lower cased component names are always treated as "intrinsic" names, and are converted to a string,
// not a variable by JSX transforms:
// <div /> => React.createElement("div", null)

// the only case we want to visit a lower-cased component has its name as "this",
this.visit(node.name);
}
} else {
this.visit(node.name);
}
this.visitJSXElement(node);
this.visitType(node.typeArguments);
for (const attr of node.attributes) {
this.visit(attr);
Expand Down
8 changes: 8 additions & 0 deletions packages/scope-manager/tests/fixtures/jsx/children.tsx.shot

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading