Skip to content

Commit

Permalink
fix: add this to implicit global variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Zzzen committed Jan 3, 2022
1 parent 3caf3c5 commit 5b1ba66
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 44 deletions.
12 changes: 11 additions & 1 deletion packages/eslint-plugin/tests/eslint-rules/no-undef.test.ts
Expand Up @@ -249,13 +249,23 @@ 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;
},
};
class C {
foo() {
let self: typeof this;
}
bar = () => {
let self: typeof this;
}
}
`,
],
invalid: [
Expand Down
7 changes: 7 additions & 0 deletions packages/scope-manager/src/referencer/Referencer.ts
Expand Up @@ -98,6 +98,13 @@ class Referencer extends Visitor {
isTypeVariable: true,
isValueVariable: false,
});

// prevent `no-undef` from reporting `typeof this`
globalScope.defineImplicitVariable('this', {
eslintImplicitGlobalSetting: 'readonly',
isTypeVariable: false,
isValueVariable: true,
});
}

/**
Expand Down
11 changes: 1 addition & 10 deletions packages/scope-manager/src/referencer/TypeVisitor.ts
Expand Up @@ -258,20 +258,11 @@ class TypeVisitor extends Visitor {
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) {
while (expr.type !== AST_NODE_TYPES.Identifier) {
expr = expr.left;
}
if ((expr.type as unknown) === AST_NODE_TYPES.ThisExpression) {
return;
}
this.#referencer.currentScope().referenceValue(expr);
}
}
Expand Down
9 changes: 0 additions & 9 deletions packages/typescript-estree/src/convert.ts
Expand Up @@ -20,7 +20,6 @@ import {
isComputedProperty,
isESTreeClassMember,
isOptional,
isThisInTypeQuery,
TSError,
unescapeStringLiteralText,
} from './node-utils';
Expand Down Expand Up @@ -788,14 +787,6 @@ export class Converter {
}

case SyntaxKind.Identifier: {
if (isThisInTypeQuery(node)) {
return this.createNode<TSESTree.ThisExpression>(
node as unknown as ts.ThisExpression,
{
type: AST_NODE_TYPES.ThisExpression,
},
);
}
return this.createNode<TSESTree.Identifier>(node, {
type: AST_NODE_TYPES.Identifier,
name: node.text,
Expand Down
24 changes: 0 additions & 24 deletions packages/typescript-estree/src/node-utils.ts
Expand Up @@ -663,27 +663,3 @@ export function firstDefined<T, U>(
}
return undefined;
}

export function identifierIsThisKeyword(id: ts.Identifier): boolean {
return id.originalKeywordKind === SyntaxKind.ThisKeyword;
}

export function isThisIdentifier(node: ts.Node | undefined): boolean {
return (
!!node &&
node.kind === SyntaxKind.Identifier &&
identifierIsThisKeyword(node as ts.Identifier)
);
}

export function isThisInTypeQuery(node: ts.Node): boolean {
if (!isThisIdentifier(node)) {
return false;
}

while (ts.isQualifiedName(node.parent) && node.parent.left === node) {
node = node.parent;
}

return node.parent.kind === SyntaxKind.TypeQuery;
}

0 comments on commit 5b1ba66

Please sign in to comment.