Skip to content
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

fix(scope-manager): correctly handle inferred types in nested type scopes #2497

Merged
merged 1 commit into from Sep 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;