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(scope-manager): incorrect reference for this within a jsx identifier #4535

Merged
merged 3 commits into from Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion packages/scope-manager/src/referencer/Referencer.ts
Expand Up @@ -521,10 +521,15 @@ 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]) {
if (
node.name.name[0].toUpperCase() === node.name.name[0] ||
node.name.name === 'this'
) {
bradzacher marked this conversation as resolved.
Show resolved Hide resolved
// 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 {
Expand Down
@@ -0,0 +1,7 @@
import React from 'react';

class Hello extends React.Component<{ tag: () => JSX.Element }> {
inline() {
return [<this.props.tag />, <this />];
}
}
145 changes: 145 additions & 0 deletions packages/scope-manager/tests/fixtures/jsx/this-jsxidentifier.tsx.shot
@@ -0,0 +1,145 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`jsx this-jsxidentifier 1`] = `
ScopeManager {
variables: Array [
ImplicitGlobalConstTypeVariable,
Variable$2 {
defs: Array [
ImportBindingDefinition$1 {
name: Identifier<"React">,
node: ImportDefaultSpecifier$1,
},
],
name: "React",
references: Array [
Reference$3 {
identifier: Identifier<"React">,
isRead: true,
isTypeReference: false,
isValueReference: true,
isWrite: false,
resolved: Variable$2,
},
Reference$1 {
identifier: Identifier<"React">,
isRead: true,
isTypeReference: false,
isValueReference: true,
isWrite: false,
resolved: Variable$2,
},
],
isValueVariable: true,
isTypeVariable: true,
},
Variable$3 {
defs: Array [
ClassNameDefinition$2 {
name: Identifier<"Hello">,
node: ClassDeclaration$2,
},
],
name: "Hello",
references: Array [],
isValueVariable: true,
isTypeVariable: true,
},
Variable$4 {
defs: Array [
ClassNameDefinition$3 {
name: Identifier<"Hello">,
node: ClassDeclaration$2,
},
],
name: "Hello",
references: Array [],
isValueVariable: true,
isTypeVariable: true,
},
Variable$5 {
defs: Array [],
name: "arguments",
references: Array [],
isValueVariable: true,
isTypeVariable: true,
},
],
scopes: Array [
GlobalScope$1 {
block: Program$3,
isStrict: false,
references: Array [
Reference$3,
],
set: Map {
"const" => ImplicitGlobalConstTypeVariable,
"React" => Variable$2,
"Hello" => Variable$3,
},
type: "global",
upper: null,
variables: Array [
ImplicitGlobalConstTypeVariable,
Variable$2,
Variable$3,
],
},
ClassScope$2 {
block: ClassDeclaration$2,
isStrict: true,
references: Array [
Reference$1,
],
set: Map {
"Hello" => Variable$4,
},
type: "class",
upper: GlobalScope$1,
variables: Array [
Variable$4,
],
},
FunctionTypeScope$3 {
block: TSFunctionType$4,
isStrict: true,
references: Array [
Reference$2 {
identifier: Identifier<"JSX">,
isRead: true,
isTypeReference: true,
isValueReference: false,
isWrite: false,
resolved: null,
},
],
set: Map {},
type: "functionType",
upper: ClassScope$2,
variables: Array [],
},
FunctionScope$4 {
block: FunctionExpression$5,
isStrict: true,
references: Array [
Reference$4 {
identifier: JSXIdentifier$6,
isRead: true,
isTypeReference: false,
isValueReference: true,
isWrite: false,
resolved: null,
},
],
set: Map {
"arguments" => Variable$5,
},
type: "function",
upper: ClassScope$2,
variables: Array [
Variable$5,
],
},
],
}
`;