Skip to content

Commit

Permalink
fix #32843 : evaluate right scope when checked if all type parameter …
Browse files Browse the repository at this point in the history
…are unused for jsdoc `@template` (#33320)

Co-authored-by: magierjones <simon.jaeger@magierjones.de>
  • Loading branch information
Simon Jaeger and magierjones authored Mar 31, 2020
1 parent 4dc827e commit 9c4cbd6
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30990,7 +30990,7 @@ namespace ts {
? rangeOfNode(parent)
// Include the `<>` in the error message
: rangeOfTypeParameters(parent.typeParameters!);
const only = typeParameters.length === 1;
const only = parent.typeParameters!.length === 1;
const message = only ? Diagnostics._0_is_declared_but_its_value_is_never_read : Diagnostics.All_type_parameters_are_unused;
const arg0 = only ? name : undefined;
addDiagnostic(typeParameter, UnusedKind.Parameter, createFileDiagnostic(getSourceFileOfNode(parent), range.pos, range.end - range.pos, message, arg0));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/a.js(3,4): error TS6133: 'V' is declared but its value is never read.
/a.js(13,4): error TS6205: All type parameters are unused
/a.js(20,16): error TS6133: 'V' is declared but its value is never read.
/a.js(20,18): error TS6133: 'X' is declared but its value is never read.


==== /a.js (4 errors) ====
/**
* @template T
* @template V
~~~~~~~~~~~
!!! error TS6133: 'V' is declared but its value is never read.
*/
class C1 {
constructor() {
/** @type {T} */
this.p;
}
}

/**
* @template T,V
~~~~~~~~~~~~~
!!! error TS6205: All type parameters are unused
*/
class C2 {
constructor() { }
}

/**
* @template T,V,X
~
!!! error TS6133: 'V' is declared but its value is never read.
~
!!! error TS6133: 'X' is declared but its value is never read.
*/
class C3 {
constructor() {
/** @type {T} */
this.p;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
=== /a.js ===
/**
* @template T
* @template V
*/
class C1 {
>C1 : Symbol(C1, Decl(a.js, 0, 0))

constructor() {
/** @type {T} */
this.p;
>this.p : Symbol(C1.p, Decl(a.js, 5, 19))
>this : Symbol(C1, Decl(a.js, 0, 0))
>p : Symbol(C1.p, Decl(a.js, 5, 19))
}
}

/**
* @template T,V
*/
class C2 {
>C2 : Symbol(C2, Decl(a.js, 9, 1))

constructor() { }
}

/**
* @template T,V,X
*/
class C3 {
>C3 : Symbol(C3, Decl(a.js, 16, 1))

constructor() {
/** @type {T} */
this.p;
>this.p : Symbol(C3.p, Decl(a.js, 22, 19))
>this : Symbol(C3, Decl(a.js, 16, 1))
>p : Symbol(C3.p, Decl(a.js, 22, 19))
}
}
40 changes: 40 additions & 0 deletions tests/baselines/reference/unusedTypeParameters_templateTag2.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
=== /a.js ===
/**
* @template T
* @template V
*/
class C1 {
>C1 : C1<T, V>

constructor() {
/** @type {T} */
this.p;
>this.p : T
>this : this
>p : T
}
}

/**
* @template T,V
*/
class C2 {
>C2 : C2<T, V>

constructor() { }
}

/**
* @template T,V,X
*/
class C3 {
>C3 : C3<T, V, X>

constructor() {
/** @type {T} */
this.p;
>this.p : T
>this : this
>p : T
}
}
34 changes: 34 additions & 0 deletions tests/cases/compiler/unusedTypeParameters_templateTag2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// @allowJs: true
// @checkJs: true
// @noEmit: true
// @noUnusedParameters:true

// @Filename: /a.js

/**
* @template T
* @template V
*/
class C1 {
constructor() {
/** @type {T} */
this.p;
}
}

/**
* @template T,V
*/
class C2 {
constructor() { }
}

/**
* @template T,V,X
*/
class C3 {
constructor() {
/** @type {T} */
this.p;
}
}

1 comment on commit 9c4cbd6

@ayruun
Copy link

@ayruun ayruun commented on 9c4cbd6 Apr 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great Job!

Please sign in to comment.