Skip to content

Commit

Permalink
fix(scope-manager): correctly handle inferred types in nested type sc…
Browse files Browse the repository at this point in the history
…opes (#2497)
  • Loading branch information
bradzacher committed Sep 6, 2020
1 parent 4d3ce5f commit 95f6bf4
Show file tree
Hide file tree
Showing 8 changed files with 451 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/eslint-plugin/tests/rules/no-unused-vars.test.ts
Expand Up @@ -789,6 +789,20 @@ declare const [v6];
`,
filename: 'foo.d.ts',
},
// https://github.com/typescript-eslint/typescript-eslint/issues/2459
`
export type Test<U> = U extends (k: infer I) => void ? I : never;
`,
`
export type Test<U> = U extends { [k: string]: infer I } ? I : never;
`,
`
export type Test<U> = U extends (arg: {
[k: string]: (arg2: infer I) => void;
}) => void
? I
: never;
`,
],

invalid: [
Expand Down
39 changes: 39 additions & 0 deletions packages/scope-manager/src/referencer/TypeVisitor.ts
Expand Up @@ -2,6 +2,7 @@ import { TSESTree, AST_NODE_TYPES } from '@typescript-eslint/types';
import { Referencer } from './Referencer';
import { Visitor } from './Visitor';
import { ParameterDefinition, TypeDefinition } from '../definition';
import { ScopeType } from '../scope';

class TypeVisitor extends Visitor {
readonly #referencer: Referencer;
Expand Down Expand Up @@ -121,6 +122,44 @@ class TypeVisitor extends Visitor {
this.visit(node.typeAnnotation);
}

protected TSInferType(node: TSESTree.TSInferType): void {
const typeParameter = node.typeParameter;
let scope = this.#referencer.currentScope();

/*
In cases where there is a sub-type scope created within a conditional type, then the generic should be defined in the
conditional type's scope, not the child type scope.
If we define it within the child type's scope then it won't be able to be referenced outside the child type
*/
if (
scope.type === ScopeType.functionType ||
scope.type === ScopeType.mappedType
) {
// search up the scope tree to figure out if we're in a nested type scope
let currentScope = scope.upper;
while (currentScope) {
if (
currentScope.type === ScopeType.functionType ||
currentScope.type === ScopeType.mappedType
) {
// ensure valid type parents only
currentScope = currentScope.upper;
continue;
}
if (currentScope.type === ScopeType.conditionalType) {
scope = currentScope;
break;
}
break;
}
}

scope.defineIdentifier(
typeParameter.name,
new TypeDefinition(typeParameter.name, typeParameter),
);
}

protected TSInterfaceDeclaration(
node: TSESTree.TSInterfaceDeclaration,
): void {
Expand Down
@@ -0,0 +1 @@
type Test<U> = U extends (k: infer I) => void ? I : never;
@@ -0,0 +1,131 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`type-declaration conditional3 1`] = `
ScopeManager {
variables: Array [
Variable$1 {
defs: Array [
TypeDefinition$1 {
name: Identifier<"Test">,
node: TSTypeAliasDeclaration$1,
},
],
name: "Test",
references: Array [],
isValueVariable: false,
isTypeVariable: true,
},
Variable$2 {
defs: Array [
TypeDefinition$2 {
name: Identifier<"U">,
node: TSTypeParameter$2,
},
],
name: "U",
references: Array [
Reference$1 {
identifier: Identifier<"U">,
isRead: true,
isTypeReference: true,
isValueReference: false,
isWrite: false,
resolved: Variable$2,
},
],
isValueVariable: false,
isTypeVariable: true,
},
Variable$3 {
defs: Array [
ParameterDefinition$3 {
name: Identifier<"k">,
node: TSFunctionType$3,
},
],
name: "k",
references: Array [],
isValueVariable: true,
isTypeVariable: false,
},
Variable$4 {
defs: Array [
TypeDefinition$4 {
name: Identifier<"I">,
node: TSTypeParameter$4,
},
],
name: "I",
references: Array [
Reference$2 {
identifier: Identifier<"I">,
isRead: true,
isTypeReference: true,
isValueReference: false,
isWrite: false,
resolved: Variable$4,
},
],
isValueVariable: false,
isTypeVariable: true,
},
],
scopes: Array [
GlobalScope$1 {
block: Program$5,
isStrict: false,
references: Array [],
set: Map {
"Test" => Variable$1,
},
type: "global",
upper: null,
variables: Array [
Variable$1,
],
},
TypeScope$2 {
block: TSTypeAliasDeclaration$1,
isStrict: true,
references: Array [],
set: Map {
"U" => Variable$2,
},
type: "type",
upper: GlobalScope$1,
variables: Array [
Variable$2,
],
},
ConditionalTypeScope$3 {
block: TSConditionalType$6,
isStrict: true,
references: Array [
Reference$1,
Reference$2,
],
set: Map {
"I" => Variable$4,
},
type: "conditionalType",
upper: TypeScope$2,
variables: Array [
Variable$4,
],
},
FunctionTypeScope$4 {
block: TSFunctionType$3,
isStrict: true,
references: Array [],
set: Map {
"k" => Variable$3,
},
type: "functionType",
upper: ConditionalTypeScope$3,
variables: Array [
Variable$3,
],
},
],
}
`;
@@ -0,0 +1 @@
type Test<U> = U extends { [k: string]: infer I } ? I : never;
@@ -0,0 +1,106 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`type-declaration conditional4 1`] = `
ScopeManager {
variables: Array [
Variable$1 {
defs: Array [
TypeDefinition$1 {
name: Identifier<"Test">,
node: TSTypeAliasDeclaration$1,
},
],
name: "Test",
references: Array [],
isValueVariable: false,
isTypeVariable: true,
},
Variable$2 {
defs: Array [
TypeDefinition$2 {
name: Identifier<"U">,
node: TSTypeParameter$2,
},
],
name: "U",
references: Array [
Reference$1 {
identifier: Identifier<"U">,
isRead: true,
isTypeReference: true,
isValueReference: false,
isWrite: false,
resolved: Variable$2,
},
],
isValueVariable: false,
isTypeVariable: true,
},
Variable$3 {
defs: Array [
TypeDefinition$3 {
name: Identifier<"I">,
node: TSTypeParameter$3,
},
],
name: "I",
references: Array [
Reference$2 {
identifier: Identifier<"I">,
isRead: true,
isTypeReference: true,
isValueReference: false,
isWrite: false,
resolved: Variable$3,
},
],
isValueVariable: false,
isTypeVariable: true,
},
],
scopes: Array [
GlobalScope$1 {
block: Program$4,
isStrict: false,
references: Array [],
set: Map {
"Test" => Variable$1,
},
type: "global",
upper: null,
variables: Array [
Variable$1,
],
},
TypeScope$2 {
block: TSTypeAliasDeclaration$1,
isStrict: true,
references: Array [],
set: Map {
"U" => Variable$2,
},
type: "type",
upper: GlobalScope$1,
variables: Array [
Variable$2,
],
},
ConditionalTypeScope$3 {
block: TSConditionalType$5,
isStrict: true,
references: Array [
Reference$1,
Reference$2,
],
set: Map {
"I" => Variable$3,
},
type: "conditionalType",
upper: TypeScope$2,
variables: Array [
Variable$3,
],
},
],
}
`;
@@ -0,0 +1,3 @@
type Test<U> = U extends (arg: { [k: string]: (arg2: infer I) => void }) => void
? I
: never;

0 comments on commit 95f6bf4

Please sign in to comment.