Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(scope-manager): support type predicates (#2493)
Fixes #2462
- Loading branch information
1 parent
a2686c0
commit a40f54c
Showing
26 changed files
with
1,292 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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
1 change: 1 addition & 0 deletions
1
packages/scope-manager/tests/fixtures/functions/arrow/type-predicate-asserts1.ts
This file contains 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 @@ | ||
const foo = (arg: any): asserts arg => {}; |
84 changes: 84 additions & 0 deletions
84
packages/scope-manager/tests/fixtures/functions/arrow/type-predicate-asserts1.ts.shot
This file contains 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,84 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`functions arrow type-predicate-asserts1 1`] = ` | ||
ScopeManager { | ||
variables: Array [ | ||
Variable$1 { | ||
defs: Array [ | ||
VariableDefinition$1 { | ||
name: Identifier<"foo">, | ||
node: VariableDeclarator$1, | ||
}, | ||
], | ||
name: "foo", | ||
references: Array [ | ||
Reference$1 { | ||
identifier: Identifier<"foo">, | ||
init: true, | ||
isRead: false, | ||
isTypeReference: false, | ||
isValueReference: true, | ||
isWrite: true, | ||
resolved: Variable$1, | ||
writeExpr: ArrowFunctionExpression$2, | ||
}, | ||
], | ||
isValueVariable: true, | ||
isTypeVariable: false, | ||
}, | ||
Variable$2 { | ||
defs: Array [ | ||
ParameterDefinition$2 { | ||
name: Identifier<"arg">, | ||
node: ArrowFunctionExpression$2, | ||
}, | ||
], | ||
name: "arg", | ||
references: Array [ | ||
Reference$2 { | ||
identifier: Identifier<"arg">, | ||
isRead: true, | ||
isTypeReference: false, | ||
isValueReference: true, | ||
isWrite: false, | ||
resolved: Variable$2, | ||
}, | ||
], | ||
isValueVariable: true, | ||
isTypeVariable: false, | ||
}, | ||
], | ||
scopes: Array [ | ||
GlobalScope$1 { | ||
block: Program$3, | ||
isStrict: false, | ||
references: Array [ | ||
Reference$1, | ||
], | ||
set: Map { | ||
"foo" => Variable$1, | ||
}, | ||
type: "global", | ||
upper: null, | ||
variables: Array [ | ||
Variable$1, | ||
], | ||
}, | ||
FunctionScope$2 { | ||
block: ArrowFunctionExpression$2, | ||
isStrict: false, | ||
references: Array [ | ||
Reference$2, | ||
], | ||
set: Map { | ||
"arg" => Variable$2, | ||
}, | ||
type: "function", | ||
upper: GlobalScope$1, | ||
variables: Array [ | ||
Variable$2, | ||
], | ||
}, | ||
], | ||
} | ||
`; |
2 changes: 2 additions & 0 deletions
2
packages/scope-manager/tests/fixtures/functions/arrow/type-predicate-asserts2.ts
This file contains 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 @@ | ||
type T = string; | ||
const foo = (arg: any): asserts arg is T => {}; |
108 changes: 108 additions & 0 deletions
108
packages/scope-manager/tests/fixtures/functions/arrow/type-predicate-asserts2.ts.shot
This file contains 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,108 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`functions arrow type-predicate-asserts2 1`] = ` | ||
ScopeManager { | ||
variables: Array [ | ||
Variable$1 { | ||
defs: Array [ | ||
TypeDefinition$1 { | ||
name: Identifier<"T">, | ||
node: TSTypeAliasDeclaration$1, | ||
}, | ||
], | ||
name: "T", | ||
references: Array [ | ||
Reference$3 { | ||
identifier: Identifier<"T">, | ||
isRead: true, | ||
isTypeReference: true, | ||
isValueReference: false, | ||
isWrite: false, | ||
resolved: Variable$1, | ||
}, | ||
], | ||
isValueVariable: false, | ||
isTypeVariable: true, | ||
}, | ||
Variable$2 { | ||
defs: Array [ | ||
VariableDefinition$2 { | ||
name: Identifier<"foo">, | ||
node: VariableDeclarator$2, | ||
}, | ||
], | ||
name: "foo", | ||
references: Array [ | ||
Reference$1 { | ||
identifier: Identifier<"foo">, | ||
init: true, | ||
isRead: false, | ||
isTypeReference: false, | ||
isValueReference: true, | ||
isWrite: true, | ||
resolved: Variable$2, | ||
writeExpr: ArrowFunctionExpression$3, | ||
}, | ||
], | ||
isValueVariable: true, | ||
isTypeVariable: false, | ||
}, | ||
Variable$3 { | ||
defs: Array [ | ||
ParameterDefinition$3 { | ||
name: Identifier<"arg">, | ||
node: ArrowFunctionExpression$3, | ||
}, | ||
], | ||
name: "arg", | ||
references: Array [ | ||
Reference$2 { | ||
identifier: Identifier<"arg">, | ||
isRead: true, | ||
isTypeReference: false, | ||
isValueReference: true, | ||
isWrite: false, | ||
resolved: Variable$3, | ||
}, | ||
], | ||
isValueVariable: true, | ||
isTypeVariable: false, | ||
}, | ||
], | ||
scopes: Array [ | ||
GlobalScope$1 { | ||
block: Program$4, | ||
isStrict: false, | ||
references: Array [ | ||
Reference$1, | ||
], | ||
set: Map { | ||
"T" => Variable$1, | ||
"foo" => Variable$2, | ||
}, | ||
type: "global", | ||
upper: null, | ||
variables: Array [ | ||
Variable$1, | ||
Variable$2, | ||
], | ||
}, | ||
FunctionScope$2 { | ||
block: ArrowFunctionExpression$3, | ||
isStrict: false, | ||
references: Array [ | ||
Reference$2, | ||
Reference$3, | ||
], | ||
set: Map { | ||
"arg" => Variable$3, | ||
}, | ||
type: "function", | ||
upper: GlobalScope$1, | ||
variables: Array [ | ||
Variable$3, | ||
], | ||
}, | ||
], | ||
} | ||
`; |
3 changes: 3 additions & 0 deletions
3
packages/scope-manager/tests/fixtures/functions/arrow/type-predicate1.ts
This file contains 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,3 @@ | ||
const foo = (arg: any): arg is string => { | ||
return typeof arg === 'string'; | ||
}; |
93 changes: 93 additions & 0 deletions
93
packages/scope-manager/tests/fixtures/functions/arrow/type-predicate1.ts.shot
This file contains 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,93 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`functions arrow type-predicate1 1`] = ` | ||
ScopeManager { | ||
variables: Array [ | ||
Variable$1 { | ||
defs: Array [ | ||
VariableDefinition$1 { | ||
name: Identifier<"foo">, | ||
node: VariableDeclarator$1, | ||
}, | ||
], | ||
name: "foo", | ||
references: Array [ | ||
Reference$1 { | ||
identifier: Identifier<"foo">, | ||
init: true, | ||
isRead: false, | ||
isTypeReference: false, | ||
isValueReference: true, | ||
isWrite: true, | ||
resolved: Variable$1, | ||
writeExpr: ArrowFunctionExpression$2, | ||
}, | ||
], | ||
isValueVariable: true, | ||
isTypeVariable: false, | ||
}, | ||
Variable$2 { | ||
defs: Array [ | ||
ParameterDefinition$2 { | ||
name: Identifier<"arg">, | ||
node: ArrowFunctionExpression$2, | ||
}, | ||
], | ||
name: "arg", | ||
references: Array [ | ||
Reference$2 { | ||
identifier: Identifier<"arg">, | ||
isRead: true, | ||
isTypeReference: false, | ||
isValueReference: true, | ||
isWrite: false, | ||
resolved: Variable$2, | ||
}, | ||
Reference$3 { | ||
identifier: Identifier<"arg">, | ||
isRead: true, | ||
isTypeReference: false, | ||
isValueReference: true, | ||
isWrite: false, | ||
resolved: Variable$2, | ||
}, | ||
], | ||
isValueVariable: true, | ||
isTypeVariable: false, | ||
}, | ||
], | ||
scopes: Array [ | ||
GlobalScope$1 { | ||
block: Program$3, | ||
isStrict: false, | ||
references: Array [ | ||
Reference$1, | ||
], | ||
set: Map { | ||
"foo" => Variable$1, | ||
}, | ||
type: "global", | ||
upper: null, | ||
variables: Array [ | ||
Variable$1, | ||
], | ||
}, | ||
FunctionScope$2 { | ||
block: ArrowFunctionExpression$2, | ||
isStrict: false, | ||
references: Array [ | ||
Reference$2, | ||
Reference$3, | ||
], | ||
set: Map { | ||
"arg" => Variable$2, | ||
}, | ||
type: "function", | ||
upper: GlobalScope$1, | ||
variables: Array [ | ||
Variable$2, | ||
], | ||
}, | ||
], | ||
} | ||
`; |
4 changes: 4 additions & 0 deletions
4
packages/scope-manager/tests/fixtures/functions/arrow/type-predicate2.ts
This file contains 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,4 @@ | ||
type T = string; | ||
const foo = (arg: any): arg is T => { | ||
return typeof arg === 'string'; | ||
}; |
Oops, something went wrong.