Skip to content

Commit 3f92a64

Browse files
authored
fix(40257): fix type parameters range (#40265)
1 parent ce8d702 commit 3f92a64

File tree

7 files changed

+50
-5
lines changed

7 files changed

+50
-5
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33329,15 +33329,16 @@ namespace ts {
3332933329
const { parent } = typeParameter;
3333033330
if (parent.kind !== SyntaxKind.InferType && parent.typeParameters!.every(isTypeParameterUnused)) {
3333133331
if (tryAddToSet(seenParentsWithEveryUnused, parent)) {
33332+
const sourceFile = getSourceFileOfNode(parent);
3333233333
const range = isJSDocTemplateTag(parent)
3333333334
// Whole @template tag
3333433335
? rangeOfNode(parent)
3333533336
// Include the `<>` in the error message
33336-
: rangeOfTypeParameters(parent.typeParameters!);
33337+
: rangeOfTypeParameters(sourceFile, parent.typeParameters!);
3333733338
const only = parent.typeParameters!.length === 1;
3333833339
const message = only ? Diagnostics._0_is_declared_but_its_value_is_never_read : Diagnostics.All_type_parameters_are_unused;
3333933340
const arg0 = only ? name : undefined;
33340-
addDiagnostic(typeParameter, UnusedKind.Parameter, createFileDiagnostic(getSourceFileOfNode(parent), range.pos, range.end - range.pos, message, arg0));
33341+
addDiagnostic(typeParameter, UnusedKind.Parameter, createFileDiagnostic(sourceFile, range.pos, range.end - range.pos, message, arg0));
3334133342
}
3334233343
}
3334333344
else {

src/compiler/utilities.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6709,9 +6709,11 @@ namespace ts {
67096709
return { pos: getTokenPosOfNode(node), end: node.end };
67106710
}
67116711

6712-
export function rangeOfTypeParameters(typeParameters: NodeArray<TypeParameterDeclaration>): TextRange {
6712+
export function rangeOfTypeParameters(sourceFile: SourceFile, typeParameters: NodeArray<TypeParameterDeclaration>): TextRange {
67136713
// Include the `<>`
6714-
return { pos: typeParameters.pos - 1, end: typeParameters.end + 1 };
6714+
const pos = typeParameters.pos - 1;
6715+
const end = skipTrivia(sourceFile.text, typeParameters.end) + 1;
6716+
return { pos, end };
67156717
}
67166718

67176719
export interface HostWithIsSourceOfProjectReferenceRedirect {

src/services/textChanges.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ namespace ts.textChanges {
843843
for (const { sourceFile, node } of this.deletedNodes) {
844844
if (!this.deletedNodes.some(d => d.sourceFile === sourceFile && rangeContainsRangeExclusive(d.node, node))) {
845845
if (isArray(node)) {
846-
this.deleteRange(sourceFile, rangeOfTypeParameters(node));
846+
this.deleteRange(sourceFile, rangeOfTypeParameters(sourceFile, node));
847847
}
848848
else {
849849
deleteDeclaration.deleteDeclaration(this, deletedNodesInLists, sourceFile, node);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @noUnusedParameters: true
4+
////export type Foo<
5+
//// T1 extends any,
6+
//// T2 extends any
7+
////> = () => void;
8+
9+
verify.codeFix({
10+
description: ts.Diagnostics.Remove_type_parameters.message,
11+
newFileContent: "export type Foo = () => void;"
12+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @noUnusedParameters: true
4+
////export type Foo< T1 extends any, T2 extends any > = () => void;
5+
6+
verify.codeFix({
7+
description: ts.Diagnostics.Remove_type_parameters.message,
8+
newFileContent: "export type Foo = () => void;"
9+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @noUnusedParameters: true
4+
////export type Foo</* comment */T1 extends any, T2 extends any/* comment */> = () => void;
5+
6+
verify.codeFix({
7+
description: ts.Diagnostics.Remove_type_parameters.message,
8+
newFileContent: "export type Foo = () => void;"
9+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @noUnusedParameters: true
4+
////export type Foo<
5+
//// T1 extends any,
6+
//// T2 extends any
7+
//// /* comment */> = () => void;
8+
9+
verify.codeFix({
10+
description: ts.Diagnostics.Remove_type_parameters.message,
11+
newFileContent: "export type Foo = () => void;"
12+
});

0 commit comments

Comments
 (0)