Skip to content

Commit

Permalink
fix: add ThisExpression to EntityName
Browse files Browse the repository at this point in the history
  • Loading branch information
Zzzen committed Jan 4, 2022
1 parent 3caf3c5 commit ae25168
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
3 changes: 2 additions & 1 deletion packages/ast-spec/src/unions/EntityName.ts
@@ -1,4 +1,5 @@
import type { Identifier } from '../expression/Identifier/spec';
import type { ThisExpression } from '../expression/ThisExpression/spec';
import type { TSQualifiedName } from '../type/TSQualifiedName/spec';

export type EntityName = Identifier | TSQualifiedName;
export type EntityName = Identifier | ThisExpression | TSQualifiedName;
3 changes: 2 additions & 1 deletion packages/eslint-plugin/tests/eslint-rules/no-undef.test.ts
Expand Up @@ -249,11 +249,12 @@ class Foo {}
export type AppState = typeof import('./src/store/reducers').default;
`,
`
let self: typeof this;
let foo: typeof this.foo;
const obj = {
foo: '',
bar() {
let self: typeof this;
let foo: typeof this.foo;
},
};
`,
Expand Down
6 changes: 5 additions & 1 deletion packages/scope-manager/src/referencer/ClassVisitor.ts
Expand Up @@ -293,7 +293,7 @@ class ClassVisitor extends Visitor {
node.typeAnnotation.type === AST_NODE_TYPES.TSTypeReference &&
this.#emitDecoratorMetadata
) {
let identifier: TSESTree.Identifier;
let identifier: TSESTree.Identifier | TSESTree.ThisExpression;
if (
node.typeAnnotation.typeName.type === AST_NODE_TYPES.TSQualifiedName
) {
Expand All @@ -306,6 +306,10 @@ class ClassVisitor extends Visitor {
identifier = node.typeAnnotation.typeName;
}

if (identifier.type === AST_NODE_TYPES.ThisExpression) {
return;
}

if (withDecorators) {
this.#referencer.currentScope().referenceDualValueType(identifier);

Expand Down
30 changes: 14 additions & 16 deletions packages/scope-manager/src/referencer/TypeVisitor.ts
Expand Up @@ -256,24 +256,22 @@ class TypeVisitor extends Visitor {

// a type query `typeof foo` is a special case that references a _non-type_ variable,
protected TSTypeQuery(node: TSESTree.TSTypeQuery): void {
if (node.exprName.type === AST_NODE_TYPES.Identifier) {
this.#referencer.currentScope().referenceValue(node.exprName);
} else if (
(node.exprName.type as unknown) === AST_NODE_TYPES.ThisExpression
) {
// technically exprName is either Identifier or QualifiedName, but eslint does not recognize `typeof this`,
// so we have translated it to ThisExpression.
return;
} else {
let expr = node.exprName.left;
while (expr.type === TSESTree.AST_NODE_TYPES.TSQualifiedName) {
expr = expr.left;
let identifier: TSESTree.Identifier | TSESTree.ThisExpression;
if (node.exprName.type === AST_NODE_TYPES.TSQualifiedName) {
let iter = node.exprName;
while (iter.left.type === AST_NODE_TYPES.TSQualifiedName) {
iter = iter.left;
}
if ((expr.type as unknown) === AST_NODE_TYPES.ThisExpression) {
return;
}
this.#referencer.currentScope().referenceValue(expr);
identifier = iter.left;
} else {
identifier = node.exprName;
}

if (identifier.type === AST_NODE_TYPES.ThisExpression) {
return;
}

this.#referencer.currentScope().referenceValue(identifier);
}

protected TSTypeAnnotation(node: TSESTree.TSTypeAnnotation): void {
Expand Down

0 comments on commit ae25168

Please sign in to comment.