-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat: treat this
in typeof this
as a ThisExpression
#4382
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
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
3caf3c5
fix: support `typeof this`
Zzzen ae25168
fix: add ThisExpression to EntityName
Zzzen 1a6ef43
fix: add parser tests
Zzzen 1680e54
fix: prefer type predicate
Zzzen 92da63c
Merge remote-tracking branch 'origin/main' into typeof-this
Zzzen fdbc897
fix: update snapshot
Zzzen c924ee3
fix: update baseline
Zzzen 3aa666e
Merge branch 'main' into typeof-this
bradzacher 204c0bb
Merge remote-tracking branch 'origin/main' into typeof-this
Zzzen 3d09e4d
test: transform babel ast
Zzzen b03c226
test: fix CI error
Zzzen b699f0c
Merge branch 'main' into typeof-this
Zzzen fcbaa5d
test: refactor
Zzzen 751fd94
Merge branch 'main' into typeof-this
Zzzen df847c1
fix merge error
Zzzen a60ceee
refactor
Zzzen b9f12b9
refactor
Zzzen d42ecff
apply armano's suggested node type change
bradzacher 042c6f2
Merge branch 'main' into typeof-this
bradzacher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
packages/scope-manager/tests/fixtures/decorators/typeof-this.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
function decorator() {} | ||
@decorator | ||
class Foo { | ||
bar(baz: typeof this) {} | ||
} |
142 changes: 142 additions & 0 deletions
142
packages/scope-manager/tests/fixtures/decorators/typeof-this.ts.shot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`decorators typeof-this 1`] = ` | ||
ScopeManager { | ||
variables: Array [ | ||
ImplicitGlobalConstTypeVariable, | ||
Variable$2 { | ||
defs: Array [ | ||
FunctionNameDefinition$1 { | ||
name: Identifier<"decorator">, | ||
node: FunctionDeclaration$1, | ||
}, | ||
], | ||
name: "decorator", | ||
references: Array [ | ||
Reference$1 { | ||
identifier: Identifier<"decorator">, | ||
isRead: true, | ||
isTypeReference: false, | ||
isValueReference: true, | ||
isWrite: false, | ||
resolved: Variable$2, | ||
}, | ||
], | ||
isValueVariable: true, | ||
isTypeVariable: false, | ||
}, | ||
Variable$3 { | ||
defs: Array [], | ||
name: "arguments", | ||
references: Array [], | ||
isValueVariable: true, | ||
isTypeVariable: true, | ||
}, | ||
Variable$4 { | ||
defs: Array [ | ||
ClassNameDefinition$2 { | ||
name: Identifier<"Foo">, | ||
node: ClassDeclaration$2, | ||
}, | ||
], | ||
name: "Foo", | ||
references: Array [], | ||
isValueVariable: true, | ||
isTypeVariable: true, | ||
}, | ||
Variable$5 { | ||
defs: Array [ | ||
ClassNameDefinition$3 { | ||
name: Identifier<"Foo">, | ||
node: ClassDeclaration$2, | ||
}, | ||
], | ||
name: "Foo", | ||
references: Array [], | ||
isValueVariable: true, | ||
isTypeVariable: true, | ||
}, | ||
Variable$6 { | ||
defs: Array [], | ||
name: "arguments", | ||
references: Array [], | ||
isValueVariable: true, | ||
isTypeVariable: true, | ||
}, | ||
Variable$7 { | ||
defs: Array [ | ||
ParameterDefinition$4 { | ||
name: Identifier<"baz">, | ||
node: FunctionExpression$3, | ||
}, | ||
], | ||
name: "baz", | ||
references: Array [], | ||
isValueVariable: true, | ||
isTypeVariable: false, | ||
}, | ||
], | ||
scopes: Array [ | ||
GlobalScope$1 { | ||
block: Program$4, | ||
isStrict: false, | ||
references: Array [ | ||
Reference$1, | ||
], | ||
set: Map { | ||
"const" => ImplicitGlobalConstTypeVariable, | ||
"decorator" => Variable$2, | ||
"Foo" => Variable$4, | ||
}, | ||
type: "global", | ||
upper: null, | ||
variables: Array [ | ||
ImplicitGlobalConstTypeVariable, | ||
Variable$2, | ||
Variable$4, | ||
], | ||
}, | ||
FunctionScope$2 { | ||
block: FunctionDeclaration$1, | ||
isStrict: false, | ||
references: Array [], | ||
set: Map { | ||
"arguments" => Variable$3, | ||
}, | ||
type: "function", | ||
upper: GlobalScope$1, | ||
variables: Array [ | ||
Variable$3, | ||
], | ||
}, | ||
ClassScope$3 { | ||
block: ClassDeclaration$2, | ||
isStrict: true, | ||
references: Array [], | ||
set: Map { | ||
"Foo" => Variable$5, | ||
}, | ||
type: "class", | ||
upper: GlobalScope$1, | ||
variables: Array [ | ||
Variable$5, | ||
], | ||
}, | ||
FunctionScope$4 { | ||
block: FunctionExpression$3, | ||
isStrict: true, | ||
references: Array [], | ||
set: Map { | ||
"arguments" => Variable$6, | ||
"baz" => Variable$7, | ||
}, | ||
type: "function", | ||
upper: ClassScope$3, | ||
variables: Array [ | ||
Variable$6, | ||
Variable$7, | ||
], | ||
}, | ||
], | ||
} | ||
`; |
2 changes: 2 additions & 0 deletions
2
packages/shared-fixtures/fixtures/typescript/types/typeof-this.src.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
let self: typeof this; | ||
let foo: typeof this.foo; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -662,3 +662,29 @@ 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, | ||
): node is ts.Identifier { | ||
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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