diff --git a/packages/eslint-plugin/tests/eslint-rules/no-undef.test.ts b/packages/eslint-plugin/tests/eslint-rules/no-undef.test.ts index fea74c8aae3..c87e5b4433e 100644 --- a/packages/eslint-plugin/tests/eslint-rules/no-undef.test.ts +++ b/packages/eslint-plugin/tests/eslint-rules/no-undef.test.ts @@ -111,6 +111,35 @@ function eachr(subject: Map): typeof subject; var a = { b: () => {} }; a?.b(); `, + // https://github.com/typescript-eslint/typescript-eslint/issues/2462 + ` +export default class Column { + isColumnString(column: unknown): column is string { + return typeof this.column === 'string'; + } +} + `, + ` +type T = string; +function predicate(arg: any): arg is T { + return typeof arg === 'string'; +} + `, + ` +function predicate(arg: any): asserts arg { + if (arg == null) { + throw 'oops'; + } +} + `, + ` +type T = string; +function predicate(arg: any): asserts arg is T { + if (typeof arg !== 'string') { + throw 'oops'; + } +} + `, ], invalid: [ { diff --git a/packages/scope-manager/src/referencer/TypeVisitor.ts b/packages/scope-manager/src/referencer/TypeVisitor.ts index a43d0948659..d921529ac82 100644 --- a/packages/scope-manager/src/referencer/TypeVisitor.ts +++ b/packages/scope-manager/src/referencer/TypeVisitor.ts @@ -199,6 +199,13 @@ class TypeVisitor extends Visitor { this.visit(node.default); } + protected TSTypePredicate(node: TSESTree.TSTypePredicate): void { + if (node.parameterName.type !== AST_NODE_TYPES.TSThisType) { + this.#referencer.currentScope().referenceValue(node.parameterName); + } + this.visit(node.typeAnnotation); + } + // 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) { diff --git a/packages/scope-manager/tests/fixtures/functions/arrow/type-predicate-asserts1.ts b/packages/scope-manager/tests/fixtures/functions/arrow/type-predicate-asserts1.ts new file mode 100644 index 00000000000..3b0688ece76 --- /dev/null +++ b/packages/scope-manager/tests/fixtures/functions/arrow/type-predicate-asserts1.ts @@ -0,0 +1 @@ +const foo = (arg: any): asserts arg => {}; diff --git a/packages/scope-manager/tests/fixtures/functions/arrow/type-predicate-asserts1.ts.shot b/packages/scope-manager/tests/fixtures/functions/arrow/type-predicate-asserts1.ts.shot new file mode 100644 index 00000000000..0c9383d2c10 --- /dev/null +++ b/packages/scope-manager/tests/fixtures/functions/arrow/type-predicate-asserts1.ts.shot @@ -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, + ], + }, + ], +} +`; diff --git a/packages/scope-manager/tests/fixtures/functions/arrow/type-predicate-asserts2.ts b/packages/scope-manager/tests/fixtures/functions/arrow/type-predicate-asserts2.ts new file mode 100644 index 00000000000..93d7055b7c4 --- /dev/null +++ b/packages/scope-manager/tests/fixtures/functions/arrow/type-predicate-asserts2.ts @@ -0,0 +1,2 @@ +type T = string; +const foo = (arg: any): asserts arg is T => {}; diff --git a/packages/scope-manager/tests/fixtures/functions/arrow/type-predicate-asserts2.ts.shot b/packages/scope-manager/tests/fixtures/functions/arrow/type-predicate-asserts2.ts.shot new file mode 100644 index 00000000000..6ef8fae784d --- /dev/null +++ b/packages/scope-manager/tests/fixtures/functions/arrow/type-predicate-asserts2.ts.shot @@ -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, + ], + }, + ], +} +`; diff --git a/packages/scope-manager/tests/fixtures/functions/arrow/type-predicate1.ts b/packages/scope-manager/tests/fixtures/functions/arrow/type-predicate1.ts new file mode 100644 index 00000000000..61f672ab3db --- /dev/null +++ b/packages/scope-manager/tests/fixtures/functions/arrow/type-predicate1.ts @@ -0,0 +1,3 @@ +const foo = (arg: any): arg is string => { + return typeof arg === 'string'; +}; diff --git a/packages/scope-manager/tests/fixtures/functions/arrow/type-predicate1.ts.shot b/packages/scope-manager/tests/fixtures/functions/arrow/type-predicate1.ts.shot new file mode 100644 index 00000000000..88e387ae251 --- /dev/null +++ b/packages/scope-manager/tests/fixtures/functions/arrow/type-predicate1.ts.shot @@ -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, + ], + }, + ], +} +`; diff --git a/packages/scope-manager/tests/fixtures/functions/arrow/type-predicate2.ts b/packages/scope-manager/tests/fixtures/functions/arrow/type-predicate2.ts new file mode 100644 index 00000000000..3a5c16435b0 --- /dev/null +++ b/packages/scope-manager/tests/fixtures/functions/arrow/type-predicate2.ts @@ -0,0 +1,4 @@ +type T = string; +const foo = (arg: any): arg is T => { + return typeof arg === 'string'; +}; diff --git a/packages/scope-manager/tests/fixtures/functions/arrow/type-predicate2.ts.shot b/packages/scope-manager/tests/fixtures/functions/arrow/type-predicate2.ts.shot new file mode 100644 index 00000000000..9795a6b29f7 --- /dev/null +++ b/packages/scope-manager/tests/fixtures/functions/arrow/type-predicate2.ts.shot @@ -0,0 +1,117 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`functions arrow type-predicate2 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, + }, + Reference$4 { + 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, + Reference$4, + ], + set: Map { + "arg" => Variable$3, + }, + type: "function", + upper: GlobalScope$1, + variables: Array [ + Variable$3, + ], + }, + ], +} +`; diff --git a/packages/scope-manager/tests/fixtures/functions/function-declaration/type-predicate-asserts1.ts b/packages/scope-manager/tests/fixtures/functions/function-declaration/type-predicate-asserts1.ts new file mode 100644 index 00000000000..f571c9fb2cb --- /dev/null +++ b/packages/scope-manager/tests/fixtures/functions/function-declaration/type-predicate-asserts1.ts @@ -0,0 +1 @@ +function foo(arg: any): asserts arg {} diff --git a/packages/scope-manager/tests/fixtures/functions/function-declaration/type-predicate-asserts1.ts.shot b/packages/scope-manager/tests/fixtures/functions/function-declaration/type-predicate-asserts1.ts.shot new file mode 100644 index 00000000000..79f248a2044 --- /dev/null +++ b/packages/scope-manager/tests/fixtures/functions/function-declaration/type-predicate-asserts1.ts.shot @@ -0,0 +1,80 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`functions function-declaration type-predicate-asserts1 1`] = ` +ScopeManager { + variables: Array [ + Variable$1 { + defs: Array [ + FunctionNameDefinition$1 { + name: Identifier<"foo">, + node: FunctionDeclaration$1, + }, + ], + name: "foo", + references: Array [], + isValueVariable: true, + isTypeVariable: false, + }, + Variable$2 { + defs: Array [], + name: "arguments", + references: Array [], + isValueVariable: true, + isTypeVariable: true, + }, + Variable$3 { + defs: Array [ + ParameterDefinition$2 { + name: Identifier<"arg">, + node: FunctionDeclaration$1, + }, + ], + name: "arg", + references: Array [ + Reference$1 { + identifier: Identifier<"arg">, + isRead: true, + isTypeReference: false, + isValueReference: true, + isWrite: false, + resolved: Variable$3, + }, + ], + isValueVariable: true, + isTypeVariable: false, + }, + ], + scopes: Array [ + GlobalScope$1 { + block: Program$2, + isStrict: false, + references: Array [], + set: Map { + "foo" => Variable$1, + }, + type: "global", + upper: null, + variables: Array [ + Variable$1, + ], + }, + FunctionScope$2 { + block: FunctionDeclaration$1, + isStrict: false, + references: Array [ + Reference$1, + ], + set: Map { + "arguments" => Variable$2, + "arg" => Variable$3, + }, + type: "function", + upper: GlobalScope$1, + variables: Array [ + Variable$2, + Variable$3, + ], + }, + ], +} +`; diff --git a/packages/scope-manager/tests/fixtures/functions/function-declaration/type-predicate-asserts2.ts b/packages/scope-manager/tests/fixtures/functions/function-declaration/type-predicate-asserts2.ts new file mode 100644 index 00000000000..64c8afd613f --- /dev/null +++ b/packages/scope-manager/tests/fixtures/functions/function-declaration/type-predicate-asserts2.ts @@ -0,0 +1,2 @@ +type T = string; +function foo(arg: any): asserts arg is T {} diff --git a/packages/scope-manager/tests/fixtures/functions/function-declaration/type-predicate-asserts2.ts.shot b/packages/scope-manager/tests/fixtures/functions/function-declaration/type-predicate-asserts2.ts.shot new file mode 100644 index 00000000000..4df9c6d09eb --- /dev/null +++ b/packages/scope-manager/tests/fixtures/functions/function-declaration/type-predicate-asserts2.ts.shot @@ -0,0 +1,104 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`functions function-declaration type-predicate-asserts2 1`] = ` +ScopeManager { + variables: Array [ + Variable$1 { + defs: Array [ + TypeDefinition$1 { + name: Identifier<"T">, + node: TSTypeAliasDeclaration$1, + }, + ], + name: "T", + references: Array [ + Reference$2 { + identifier: Identifier<"T">, + isRead: true, + isTypeReference: true, + isValueReference: false, + isWrite: false, + resolved: Variable$1, + }, + ], + isValueVariable: false, + isTypeVariable: true, + }, + Variable$2 { + defs: Array [ + FunctionNameDefinition$2 { + name: Identifier<"foo">, + node: FunctionDeclaration$2, + }, + ], + name: "foo", + references: Array [], + isValueVariable: true, + isTypeVariable: false, + }, + Variable$3 { + defs: Array [], + name: "arguments", + references: Array [], + isValueVariable: true, + isTypeVariable: true, + }, + Variable$4 { + defs: Array [ + ParameterDefinition$3 { + name: Identifier<"arg">, + node: FunctionDeclaration$2, + }, + ], + name: "arg", + references: Array [ + Reference$1 { + identifier: Identifier<"arg">, + isRead: true, + isTypeReference: false, + isValueReference: true, + isWrite: false, + resolved: Variable$4, + }, + ], + isValueVariable: true, + isTypeVariable: false, + }, + ], + scopes: Array [ + GlobalScope$1 { + block: Program$3, + isStrict: false, + references: Array [], + set: Map { + "T" => Variable$1, + "foo" => Variable$2, + }, + type: "global", + upper: null, + variables: Array [ + Variable$1, + Variable$2, + ], + }, + FunctionScope$2 { + block: FunctionDeclaration$2, + isStrict: false, + references: Array [ + Reference$1, + Reference$2, + ], + set: Map { + "arguments" => Variable$3, + "arg" => Variable$4, + }, + type: "function", + upper: GlobalScope$1, + variables: Array [ + Variable$3, + Variable$4, + ], + }, + ], +} +`; diff --git a/packages/scope-manager/tests/fixtures/functions/function-declaration/type-predicate1.ts b/packages/scope-manager/tests/fixtures/functions/function-declaration/type-predicate1.ts new file mode 100644 index 00000000000..ed938ce7bfa --- /dev/null +++ b/packages/scope-manager/tests/fixtures/functions/function-declaration/type-predicate1.ts @@ -0,0 +1,3 @@ +function foo(arg: any): arg is string { + return typeof arg === 'string'; +} diff --git a/packages/scope-manager/tests/fixtures/functions/function-declaration/type-predicate1.ts.shot b/packages/scope-manager/tests/fixtures/functions/function-declaration/type-predicate1.ts.shot new file mode 100644 index 00000000000..315b74fe467 --- /dev/null +++ b/packages/scope-manager/tests/fixtures/functions/function-declaration/type-predicate1.ts.shot @@ -0,0 +1,89 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`functions function-declaration type-predicate1 1`] = ` +ScopeManager { + variables: Array [ + Variable$1 { + defs: Array [ + FunctionNameDefinition$1 { + name: Identifier<"foo">, + node: FunctionDeclaration$1, + }, + ], + name: "foo", + references: Array [], + isValueVariable: true, + isTypeVariable: false, + }, + Variable$2 { + defs: Array [], + name: "arguments", + references: Array [], + isValueVariable: true, + isTypeVariable: true, + }, + Variable$3 { + defs: Array [ + ParameterDefinition$2 { + name: Identifier<"arg">, + node: FunctionDeclaration$1, + }, + ], + name: "arg", + references: Array [ + Reference$1 { + identifier: Identifier<"arg">, + isRead: true, + isTypeReference: false, + isValueReference: true, + isWrite: false, + resolved: Variable$3, + }, + 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$2, + isStrict: false, + references: Array [], + set: Map { + "foo" => Variable$1, + }, + type: "global", + upper: null, + variables: Array [ + Variable$1, + ], + }, + FunctionScope$2 { + block: FunctionDeclaration$1, + isStrict: false, + references: Array [ + Reference$1, + Reference$2, + ], + set: Map { + "arguments" => Variable$2, + "arg" => Variable$3, + }, + type: "function", + upper: GlobalScope$1, + variables: Array [ + Variable$2, + Variable$3, + ], + }, + ], +} +`; diff --git a/packages/scope-manager/tests/fixtures/functions/function-declaration/type-predicate2.ts b/packages/scope-manager/tests/fixtures/functions/function-declaration/type-predicate2.ts new file mode 100644 index 00000000000..ecf9003b234 --- /dev/null +++ b/packages/scope-manager/tests/fixtures/functions/function-declaration/type-predicate2.ts @@ -0,0 +1,4 @@ +type T = string; +function foo(arg: any): arg is T { + return typeof arg === 'string'; +} diff --git a/packages/scope-manager/tests/fixtures/functions/function-declaration/type-predicate2.ts.shot b/packages/scope-manager/tests/fixtures/functions/function-declaration/type-predicate2.ts.shot new file mode 100644 index 00000000000..95f298e5eea --- /dev/null +++ b/packages/scope-manager/tests/fixtures/functions/function-declaration/type-predicate2.ts.shot @@ -0,0 +1,113 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`functions function-declaration type-predicate2 1`] = ` +ScopeManager { + variables: Array [ + Variable$1 { + defs: Array [ + TypeDefinition$1 { + name: Identifier<"T">, + node: TSTypeAliasDeclaration$1, + }, + ], + name: "T", + references: Array [ + Reference$2 { + identifier: Identifier<"T">, + isRead: true, + isTypeReference: true, + isValueReference: false, + isWrite: false, + resolved: Variable$1, + }, + ], + isValueVariable: false, + isTypeVariable: true, + }, + Variable$2 { + defs: Array [ + FunctionNameDefinition$2 { + name: Identifier<"foo">, + node: FunctionDeclaration$2, + }, + ], + name: "foo", + references: Array [], + isValueVariable: true, + isTypeVariable: false, + }, + Variable$3 { + defs: Array [], + name: "arguments", + references: Array [], + isValueVariable: true, + isTypeVariable: true, + }, + Variable$4 { + defs: Array [ + ParameterDefinition$3 { + name: Identifier<"arg">, + node: FunctionDeclaration$2, + }, + ], + name: "arg", + references: Array [ + Reference$1 { + identifier: Identifier<"arg">, + isRead: true, + isTypeReference: false, + isValueReference: true, + isWrite: false, + resolved: Variable$4, + }, + Reference$3 { + identifier: Identifier<"arg">, + isRead: true, + isTypeReference: false, + isValueReference: true, + isWrite: false, + resolved: Variable$4, + }, + ], + isValueVariable: true, + isTypeVariable: false, + }, + ], + scopes: Array [ + GlobalScope$1 { + block: Program$3, + isStrict: false, + references: Array [], + set: Map { + "T" => Variable$1, + "foo" => Variable$2, + }, + type: "global", + upper: null, + variables: Array [ + Variable$1, + Variable$2, + ], + }, + FunctionScope$2 { + block: FunctionDeclaration$2, + isStrict: false, + references: Array [ + Reference$1, + Reference$2, + Reference$3, + ], + set: Map { + "arguments" => Variable$3, + "arg" => Variable$4, + }, + type: "function", + upper: GlobalScope$1, + variables: Array [ + Variable$3, + Variable$4, + ], + }, + ], +} +`; diff --git a/packages/scope-manager/tests/fixtures/functions/function-expression/type-predicate-asserts1.ts b/packages/scope-manager/tests/fixtures/functions/function-expression/type-predicate-asserts1.ts new file mode 100644 index 00000000000..3bf323ef9e0 --- /dev/null +++ b/packages/scope-manager/tests/fixtures/functions/function-expression/type-predicate-asserts1.ts @@ -0,0 +1 @@ +const foo = function (arg: any): asserts arg {}; diff --git a/packages/scope-manager/tests/fixtures/functions/function-expression/type-predicate-asserts1.ts.shot b/packages/scope-manager/tests/fixtures/functions/function-expression/type-predicate-asserts1.ts.shot new file mode 100644 index 00000000000..53dbd1612ee --- /dev/null +++ b/packages/scope-manager/tests/fixtures/functions/function-expression/type-predicate-asserts1.ts.shot @@ -0,0 +1,93 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`functions function-expression 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: FunctionExpression$2, + }, + ], + isValueVariable: true, + isTypeVariable: false, + }, + Variable$2 { + defs: Array [], + name: "arguments", + references: Array [], + isValueVariable: true, + isTypeVariable: true, + }, + Variable$3 { + defs: Array [ + ParameterDefinition$2 { + name: Identifier<"arg">, + node: FunctionExpression$2, + }, + ], + 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$3, + isStrict: false, + references: Array [ + Reference$1, + ], + set: Map { + "foo" => Variable$1, + }, + type: "global", + upper: null, + variables: Array [ + Variable$1, + ], + }, + FunctionScope$2 { + block: FunctionExpression$2, + isStrict: false, + references: Array [ + Reference$2, + ], + set: Map { + "arguments" => Variable$2, + "arg" => Variable$3, + }, + type: "function", + upper: GlobalScope$1, + variables: Array [ + Variable$2, + Variable$3, + ], + }, + ], +} +`; diff --git a/packages/scope-manager/tests/fixtures/functions/function-expression/type-predicate-asserts2.ts b/packages/scope-manager/tests/fixtures/functions/function-expression/type-predicate-asserts2.ts new file mode 100644 index 00000000000..9e9be635d08 --- /dev/null +++ b/packages/scope-manager/tests/fixtures/functions/function-expression/type-predicate-asserts2.ts @@ -0,0 +1,2 @@ +type T = string; +const foo = function (arg: any): asserts arg is T {}; diff --git a/packages/scope-manager/tests/fixtures/functions/function-expression/type-predicate-asserts2.ts.shot b/packages/scope-manager/tests/fixtures/functions/function-expression/type-predicate-asserts2.ts.shot new file mode 100644 index 00000000000..12b912cf820 --- /dev/null +++ b/packages/scope-manager/tests/fixtures/functions/function-expression/type-predicate-asserts2.ts.shot @@ -0,0 +1,117 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`functions function-expression 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: FunctionExpression$3, + }, + ], + isValueVariable: true, + isTypeVariable: false, + }, + Variable$3 { + defs: Array [], + name: "arguments", + references: Array [], + isValueVariable: true, + isTypeVariable: true, + }, + Variable$4 { + defs: Array [ + ParameterDefinition$3 { + name: Identifier<"arg">, + node: FunctionExpression$3, + }, + ], + name: "arg", + references: Array [ + Reference$2 { + identifier: Identifier<"arg">, + isRead: true, + isTypeReference: false, + isValueReference: true, + isWrite: false, + resolved: Variable$4, + }, + ], + 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: FunctionExpression$3, + isStrict: false, + references: Array [ + Reference$2, + Reference$3, + ], + set: Map { + "arguments" => Variable$3, + "arg" => Variable$4, + }, + type: "function", + upper: GlobalScope$1, + variables: Array [ + Variable$3, + Variable$4, + ], + }, + ], +} +`; diff --git a/packages/scope-manager/tests/fixtures/functions/function-expression/type-predicate1.ts b/packages/scope-manager/tests/fixtures/functions/function-expression/type-predicate1.ts new file mode 100644 index 00000000000..a9a8b925784 --- /dev/null +++ b/packages/scope-manager/tests/fixtures/functions/function-expression/type-predicate1.ts @@ -0,0 +1,3 @@ +const foo = function (arg: any): arg is string { + return typeof arg === 'string'; +}; diff --git a/packages/scope-manager/tests/fixtures/functions/function-expression/type-predicate1.ts.shot b/packages/scope-manager/tests/fixtures/functions/function-expression/type-predicate1.ts.shot new file mode 100644 index 00000000000..9aea3d721d8 --- /dev/null +++ b/packages/scope-manager/tests/fixtures/functions/function-expression/type-predicate1.ts.shot @@ -0,0 +1,102 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`functions function-expression 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: FunctionExpression$2, + }, + ], + isValueVariable: true, + isTypeVariable: false, + }, + Variable$2 { + defs: Array [], + name: "arguments", + references: Array [], + isValueVariable: true, + isTypeVariable: true, + }, + Variable$3 { + defs: Array [ + ParameterDefinition$2 { + name: Identifier<"arg">, + node: FunctionExpression$2, + }, + ], + name: "arg", + references: Array [ + Reference$2 { + identifier: Identifier<"arg">, + isRead: true, + isTypeReference: false, + isValueReference: true, + isWrite: false, + resolved: Variable$3, + }, + Reference$3 { + identifier: Identifier<"arg">, + isRead: true, + isTypeReference: false, + isValueReference: true, + isWrite: false, + resolved: Variable$3, + }, + ], + 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: FunctionExpression$2, + isStrict: false, + references: Array [ + Reference$2, + Reference$3, + ], + set: Map { + "arguments" => Variable$2, + "arg" => Variable$3, + }, + type: "function", + upper: GlobalScope$1, + variables: Array [ + Variable$2, + Variable$3, + ], + }, + ], +} +`; diff --git a/packages/scope-manager/tests/fixtures/functions/function-expression/type-predicate2.ts b/packages/scope-manager/tests/fixtures/functions/function-expression/type-predicate2.ts new file mode 100644 index 00000000000..3e83e548b3b --- /dev/null +++ b/packages/scope-manager/tests/fixtures/functions/function-expression/type-predicate2.ts @@ -0,0 +1,4 @@ +type T = string; +const foo = function (arg: any): arg is T { + return typeof arg === 'string'; +}; diff --git a/packages/scope-manager/tests/fixtures/functions/function-expression/type-predicate2.ts.shot b/packages/scope-manager/tests/fixtures/functions/function-expression/type-predicate2.ts.shot new file mode 100644 index 00000000000..fc5dd9a6e0e --- /dev/null +++ b/packages/scope-manager/tests/fixtures/functions/function-expression/type-predicate2.ts.shot @@ -0,0 +1,126 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`functions function-expression type-predicate2 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: FunctionExpression$3, + }, + ], + isValueVariable: true, + isTypeVariable: false, + }, + Variable$3 { + defs: Array [], + name: "arguments", + references: Array [], + isValueVariable: true, + isTypeVariable: true, + }, + Variable$4 { + defs: Array [ + ParameterDefinition$3 { + name: Identifier<"arg">, + node: FunctionExpression$3, + }, + ], + name: "arg", + references: Array [ + Reference$2 { + identifier: Identifier<"arg">, + isRead: true, + isTypeReference: false, + isValueReference: true, + isWrite: false, + resolved: Variable$4, + }, + Reference$4 { + identifier: Identifier<"arg">, + isRead: true, + isTypeReference: false, + isValueReference: true, + isWrite: false, + resolved: Variable$4, + }, + ], + 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: FunctionExpression$3, + isStrict: false, + references: Array [ + Reference$2, + Reference$3, + Reference$4, + ], + set: Map { + "arguments" => Variable$3, + "arg" => Variable$4, + }, + type: "function", + upper: GlobalScope$1, + variables: Array [ + Variable$3, + Variable$4, + ], + }, + ], +} +`;