Skip to content

Improve @template lookup and resilience #42851

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 3 commits into from
Feb 18, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9185,7 +9185,6 @@ namespace ts {
return undefined;
}
switch (node.kind) {
case SyntaxKind.VariableStatement:
case SyntaxKind.ClassDeclaration:
case SyntaxKind.ClassExpression:
case SyntaxKind.InterfaceDeclaration:
Expand All @@ -9205,28 +9204,32 @@ namespace ts {
case SyntaxKind.JSDocEnumTag:
case SyntaxKind.JSDocCallbackTag:
case SyntaxKind.MappedType:
case SyntaxKind.ConditionalType:
case SyntaxKind.ConditionalType: {
const outerTypeParameters = getOuterTypeParameters(node, includeThisTypes);
if (node.kind === SyntaxKind.MappedType) {
return append(outerTypeParameters, getDeclaredTypeOfTypeParameter(getSymbolOfNode((<MappedTypeNode>node).typeParameter)));
}
else if (node.kind === SyntaxKind.ConditionalType) {
return concatenate(outerTypeParameters, getInferTypeParameters(<ConditionalTypeNode>node));
}
else if (node.kind === SyntaxKind.VariableStatement && !isInJSFile(node)) {
break;
}
const outerAndOwnTypeParameters = appendTypeParameters(outerTypeParameters, getEffectiveTypeParameterDeclarations(<DeclarationWithTypeParameters>node));
const thisType = includeThisTypes &&
(node.kind === SyntaxKind.ClassDeclaration || node.kind === SyntaxKind.ClassExpression || node.kind === SyntaxKind.InterfaceDeclaration || isJSConstructor(node)) &&
getDeclaredTypeOfClassOrInterface(getSymbolOfNode(node as ClassLikeDeclaration | InterfaceDeclaration)).thisType;
return thisType ? append(outerAndOwnTypeParameters, thisType) : outerAndOwnTypeParameters;
}
case SyntaxKind.JSDocParameterTag:
const paramSymbol = getParameterSymbolFromJSDoc(node as JSDocParameterTag);
if (paramSymbol) {
node = paramSymbol.valueDeclaration;
}
break;
case SyntaxKind.JSDocComment: {
const outerTypeParameters = getOuterTypeParameters(node, includeThisTypes);
return (node as JSDoc).tags
? appendTypeParameters(outerTypeParameters, flatMap((node as JSDoc).tags, t => isJSDocTemplateTag(t) ? t.typeParameters : undefined))
: outerTypeParameters;
}
}
}
}
Expand Down
30 changes: 30 additions & 0 deletions tests/baselines/reference/jsdocOuterTypeParameters1.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
error TS5055: Cannot write file 'tests/cases/conformance/jsdoc/jsdocOuterTypeParameters1.js' because it would overwrite input file.
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.
tests/cases/conformance/jsdoc/jsdocOuterTypeParameters1.js(1,14): error TS2304: Cannot find name 'T'.
tests/cases/conformance/jsdoc/jsdocOuterTypeParameters1.js(4,17): error TS2304: Cannot find name 'T'.
tests/cases/conformance/jsdoc/jsdocOuterTypeParameters1.js(4,19): error TS1069: Unexpected token. A type parameter name was expected without curly braces.
tests/cases/conformance/jsdoc/jsdocOuterTypeParameters1.js(7,35): error TS2339: Property 'foo' does not exist on type 'Bar'.


!!! error TS5055: Cannot write file 'tests/cases/conformance/jsdoc/jsdocOuterTypeParameters1.js' because it would overwrite input file.
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.
==== tests/cases/conformance/jsdoc/jsdocOuterTypeParameters1.js (4 errors) ====
/** @return {T} */
~
!!! error TS2304: Cannot find name 'T'.
const dedupingMixin = function(mixin) {};

/** @template {T} */
~
!!! error TS2304: Cannot find name 'T'.
~
!!! error TS1069: Unexpected token. A type parameter name was expected without curly braces.
const PropertyAccessors = dedupingMixin(() => {
class Bar {
static bar() { this.prototype.foo(); }
~~~
!!! error TS2339: Property 'foo' does not exist on type 'Bar'.
}
});


23 changes: 23 additions & 0 deletions tests/baselines/reference/jsdocOuterTypeParameters1.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== tests/cases/conformance/jsdoc/jsdocOuterTypeParameters1.js ===
/** @return {T} */
const dedupingMixin = function(mixin) {};
>dedupingMixin : Symbol(dedupingMixin, Decl(jsdocOuterTypeParameters1.js, 1, 5))
>mixin : Symbol(mixin, Decl(jsdocOuterTypeParameters1.js, 1, 31))

/** @template {T} */
const PropertyAccessors = dedupingMixin(() => {
>PropertyAccessors : Symbol(PropertyAccessors, Decl(jsdocOuterTypeParameters1.js, 4, 5))
>dedupingMixin : Symbol(dedupingMixin, Decl(jsdocOuterTypeParameters1.js, 1, 5))

class Bar {
>Bar : Symbol(Bar, Decl(jsdocOuterTypeParameters1.js, 4, 47))

static bar() { this.prototype.foo(); }
>bar : Symbol(Bar.bar, Decl(jsdocOuterTypeParameters1.js, 5, 13))
>this.prototype : Symbol(Bar.prototype)
>this : Symbol(Bar, Decl(jsdocOuterTypeParameters1.js, 4, 47))
>prototype : Symbol(Bar.prototype)
}
});


29 changes: 29 additions & 0 deletions tests/baselines/reference/jsdocOuterTypeParameters1.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
=== tests/cases/conformance/jsdoc/jsdocOuterTypeParameters1.js ===
/** @return {T} */
const dedupingMixin = function(mixin) {};
>dedupingMixin : (mixin: any) => any
>function(mixin) {} : (mixin: any) => any
>mixin : any

/** @template {T} */
const PropertyAccessors = dedupingMixin(() => {
>PropertyAccessors : any
>dedupingMixin(() => { class Bar { static bar() { this.prototype.foo(); } }}) : any
>dedupingMixin : (mixin: any) => any
>() => { class Bar { static bar() { this.prototype.foo(); } }} : () => void

class Bar {
>Bar : Bar

static bar() { this.prototype.foo(); }
>bar : () => void
>this.prototype.foo() : any
>this.prototype.foo : any
>this.prototype : Bar
>this : typeof Bar
>prototype : Bar
>foo : any
}
});


24 changes: 24 additions & 0 deletions tests/baselines/reference/jsdocOuterTypeParameters2.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
error TS5055: Cannot write file 'tests/cases/conformance/jsdoc/jsdocOuterTypeParameters1.js' because it would overwrite input file.
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.
tests/cases/conformance/jsdoc/jsdocOuterTypeParameters1.js(1,14): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
tests/cases/conformance/jsdoc/jsdocOuterTypeParameters1.js(7,35): error TS2339: Property 'foo' does not exist on type 'Bar'.


!!! error TS5055: Cannot write file 'tests/cases/conformance/jsdoc/jsdocOuterTypeParameters1.js' because it would overwrite input file.
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.
==== tests/cases/conformance/jsdoc/jsdocOuterTypeParameters1.js (2 errors) ====
/** @return {T} */
~
!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
const dedupingMixin = function(mixin) {};

/** @template T */
const PropertyAccessors = dedupingMixin(() => {
class Bar {
static bar() { this.prototype.foo(); }
~~~
!!! error TS2339: Property 'foo' does not exist on type 'Bar'.
}
});


23 changes: 23 additions & 0 deletions tests/baselines/reference/jsdocOuterTypeParameters2.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== tests/cases/conformance/jsdoc/jsdocOuterTypeParameters1.js ===
/** @return {T} */
const dedupingMixin = function(mixin) {};
>dedupingMixin : Symbol(dedupingMixin, Decl(jsdocOuterTypeParameters1.js, 1, 5))
>mixin : Symbol(mixin, Decl(jsdocOuterTypeParameters1.js, 1, 31))

/** @template T */
const PropertyAccessors = dedupingMixin(() => {
>PropertyAccessors : Symbol(PropertyAccessors, Decl(jsdocOuterTypeParameters1.js, 4, 5))
>dedupingMixin : Symbol(dedupingMixin, Decl(jsdocOuterTypeParameters1.js, 1, 5))

class Bar {
>Bar : Symbol(Bar, Decl(jsdocOuterTypeParameters1.js, 4, 47))

static bar() { this.prototype.foo(); }
>bar : Symbol(Bar.bar, Decl(jsdocOuterTypeParameters1.js, 5, 13))
>this.prototype : Symbol(Bar.prototype)
>this : Symbol(Bar, Decl(jsdocOuterTypeParameters1.js, 4, 47))
>prototype : Symbol(Bar.prototype)
}
});


29 changes: 29 additions & 0 deletions tests/baselines/reference/jsdocOuterTypeParameters2.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
=== tests/cases/conformance/jsdoc/jsdocOuterTypeParameters1.js ===
/** @return {T} */
const dedupingMixin = function(mixin) {};
>dedupingMixin : (mixin: any) => T
>function(mixin) {} : (mixin: any) => T
>mixin : any

/** @template T */
const PropertyAccessors = dedupingMixin(() => {
>PropertyAccessors : T
>dedupingMixin(() => { class Bar { static bar() { this.prototype.foo(); } }}) : T
>dedupingMixin : (mixin: any) => T
>() => { class Bar { static bar() { this.prototype.foo(); } }} : () => void

class Bar {
>Bar : Bar

static bar() { this.prototype.foo(); }
>bar : () => void
>this.prototype.foo() : any
>this.prototype.foo : any
>this.prototype : Bar
>this : typeof Bar
>prototype : Bar
>foo : any
}
});


25 changes: 25 additions & 0 deletions tests/baselines/reference/jsdocOuterTypeParameters3.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
error TS5055: Cannot write file 'tests/cases/conformance/jsdoc/jsdocOuterTypeParameters3.js' because it would overwrite input file.
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.
tests/cases/conformance/jsdoc/jsdocOuterTypeParameters3.js(1,16): error TS2304: Cannot find name 'T'.
tests/cases/conformance/jsdoc/jsdocOuterTypeParameters3.js(1,18): error TS1069: Unexpected token. A type parameter name was expected without curly braces.
tests/cases/conformance/jsdoc/jsdocOuterTypeParameters3.js(5,43): error TS2339: Property 'foo' does not exist on type 'Bar'.


!!! error TS5055: Cannot write file 'tests/cases/conformance/jsdoc/jsdocOuterTypeParameters3.js' because it would overwrite input file.
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.
==== tests/cases/conformance/jsdoc/jsdocOuterTypeParameters3.js (3 errors) ====
/** @template {T} */
~
!!! error TS2304: Cannot find name 'T'.
~
!!! error TS1069: Unexpected token. A type parameter name was expected without curly braces.
class Baz {
m() {
class Bar {
static bar() { this.prototype.foo(); }
~~~
!!! error TS2339: Property 'foo' does not exist on type 'Bar'.
}
}
}

20 changes: 20 additions & 0 deletions tests/baselines/reference/jsdocOuterTypeParameters3.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
=== tests/cases/conformance/jsdoc/jsdocOuterTypeParameters3.js ===
/** @template {T} */
class Baz {
>Baz : Symbol(Baz, Decl(jsdocOuterTypeParameters3.js, 0, 0))

m() {
>m : Symbol(Baz.m, Decl(jsdocOuterTypeParameters3.js, 1, 11))

class Bar {
>Bar : Symbol(Bar, Decl(jsdocOuterTypeParameters3.js, 2, 9))

static bar() { this.prototype.foo(); }
>bar : Symbol(Bar.bar, Decl(jsdocOuterTypeParameters3.js, 3, 19))
>this.prototype : Symbol(Bar.prototype)
>this : Symbol(Bar, Decl(jsdocOuterTypeParameters3.js, 2, 9))
>prototype : Symbol(Bar.prototype)
}
}
}

23 changes: 23 additions & 0 deletions tests/baselines/reference/jsdocOuterTypeParameters3.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== tests/cases/conformance/jsdoc/jsdocOuterTypeParameters3.js ===
/** @template {T} */
class Baz {
>Baz : Baz

m() {
>m : () => void

class Bar {
>Bar : Bar

static bar() { this.prototype.foo(); }
>bar : () => void
>this.prototype.foo() : any
>this.prototype.foo : any
>this.prototype : Bar
>this : typeof Bar
>prototype : Bar
>foo : any
}
}
}

12 changes: 12 additions & 0 deletions tests/cases/conformance/jsdoc/jsdocOuterTypeParameters1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @checkjs: true
// @filename: jsdocOuterTypeParameters1.js
/** @return {T} */
const dedupingMixin = function(mixin) {};

/** @template {T} */
const PropertyAccessors = dedupingMixin(() => {
class Bar {
static bar() { this.prototype.foo(); }
}
});

12 changes: 12 additions & 0 deletions tests/cases/conformance/jsdoc/jsdocOuterTypeParameters2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @checkjs: true
// @filename: jsdocOuterTypeParameters1.js
/** @return {T} */
const dedupingMixin = function(mixin) {};

/** @template T */
const PropertyAccessors = dedupingMixin(() => {
class Bar {
static bar() { this.prototype.foo(); }
}
});

11 changes: 11 additions & 0 deletions tests/cases/conformance/jsdoc/jsdocOuterTypeParameters3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @checkjs: true
// @filename: jsdocOuterTypeParameters3.js

/** @template {T} */
class Baz {
m() {
class Bar {
static bar() { this.prototype.foo(); }
}
}
}