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
feat: treat this
in typeof this
as a ThisExpression
#4382
Changes from 1 commit
3caf3c5
ae25168
1a6ef43
1680e54
92da63c
fdbc897
c924ee3
3aa666e
204c0bb
3d09e4d
b03c226
b699f0c
fcbaa5d
751fd94
df847c1
a60ceee
b9f12b9
d42ecff
042c6f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -663,3 +663,27 @@ 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 { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should use a type predicate so that TS can understand the refinement you're making within the function
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
export interface ThisExpression extends PrimaryExpression {
readonly kind: SyntaxKind.ThisKeyword;
} |
||||||
return ( | ||||||
!!node && | ||||||
node.kind === SyntaxKind.Identifier && | ||||||
identifierIsThisKeyword(node as ts.Identifier) | ||||||
); | ||||||
} | ||||||
|
||||||
export function isThisInTypeQuery(node: ts.Node): boolean { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto above - use a predicate return type |
||||||
if (!isThisIdentifier(node)) { | ||||||
return false; | ||||||
} | ||||||
|
||||||
while (ts.isQualifiedName(node.parent) && node.parent.left === node) { | ||||||
node = node.parent; | ||||||
} | ||||||
|
||||||
return node.parent.kind === SyntaxKind.TypeQuery; | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing the AST feels hacky and is gonna break some plugins. The
no-undef
rule usesglobalScope.through
to check undefined variables. Creating a variable calledthis
for functions should prevent eslint from reporting this.https://github.com/eslint/eslint/blob/main/lib/rules/no-undef.js#L59L75
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks like an issue in typescript, we should either get
SyntaxKind.ThisType
orSyntaxKind.ThisKeyword
playground 4.3.5
playground 4.5.4
const b: this = this
- is correctly reported asThisType
older versions of typescript (<4.4) reported
typeof this
as errorCannot find name 'this'. (2304)
we should add test cases for parser and visitor