Skip to content

fix(40257): Remove type parameters quick fix forgets the trailing > #40265

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 1 commit into from
Nov 2, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32531,15 +32531,16 @@ namespace ts {
const { parent } = typeParameter;
if (parent.kind !== SyntaxKind.InferType && parent.typeParameters!.every(isTypeParameterUnused)) {
if (tryAddToSet(seenParentsWithEveryUnused, parent)) {
const sourceFile = getSourceFileOfNode(parent);
const range = isJSDocTemplateTag(parent)
// Whole @template tag
? rangeOfNode(parent)
// Include the `<>` in the error message
: rangeOfTypeParameters(parent.typeParameters!);
: rangeOfTypeParameters(sourceFile, parent.typeParameters!);
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));
addDiagnostic(typeParameter, UnusedKind.Parameter, createFileDiagnostic(sourceFile, range.pos, range.end - range.pos, message, arg0));
}
}
else {
Expand Down
6 changes: 4 additions & 2 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6635,9 +6635,11 @@ namespace ts {
return { pos: getTokenPosOfNode(node), end: node.end };
}

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

export interface HostWithIsSourceOfProjectReferenceRedirect {
Expand Down
2 changes: 1 addition & 1 deletion src/services/textChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ namespace ts.textChanges {
for (const { sourceFile, node } of this.deletedNodes) {
if (!this.deletedNodes.some(d => d.sourceFile === sourceFile && rangeContainsRangeExclusive(d.node, node))) {
if (isArray(node)) {
this.deleteRange(sourceFile, rangeOfTypeParameters(node));
this.deleteRange(sourceFile, rangeOfTypeParameters(sourceFile, node));
}
else {
deleteDeclaration.deleteDeclaration(this, deletedNodesInLists, sourceFile, node);
Expand Down
12 changes: 12 additions & 0 deletions tests/cases/fourslash/unusedTypeParametersWithTrivia1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// <reference path="fourslash.ts" />

// @noUnusedParameters: true
////export type Foo<
//// T1 extends any,
//// T2 extends any
////> = () => void;

verify.codeFix({
description: ts.Diagnostics.Remove_type_parameters.message,
newFileContent: "export type Foo = () => void;"
});
9 changes: 9 additions & 0 deletions tests/cases/fourslash/unusedTypeParametersWithTrivia2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// <reference path="fourslash.ts" />

// @noUnusedParameters: true
////export type Foo< T1 extends any, T2 extends any > = () => void;

verify.codeFix({
description: ts.Diagnostics.Remove_type_parameters.message,
newFileContent: "export type Foo = () => void;"
});
9 changes: 9 additions & 0 deletions tests/cases/fourslash/unusedTypeParametersWithTrivia3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// <reference path="fourslash.ts" />

// @noUnusedParameters: true
////export type Foo</* comment */T1 extends any, T2 extends any/* comment */> = () => void;

verify.codeFix({
description: ts.Diagnostics.Remove_type_parameters.message,
newFileContent: "export type Foo = () => void;"
});
12 changes: 12 additions & 0 deletions tests/cases/fourslash/unusedTypeParametersWithTrivia4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// <reference path="fourslash.ts" />

// @noUnusedParameters: true
////export type Foo<
//// T1 extends any,
//// T2 extends any
//// /* comment */> = () => void;

verify.codeFix({
description: ts.Diagnostics.Remove_type_parameters.message,
newFileContent: "export type Foo = () => void;"
});