fix(40257): fix type parameters range (#40265)
This commit is contained in:
parent
ce8d702586
commit
3f92a6498f
|
@ -33329,15 +33329,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 {
|
||||
|
|
|
@ -6709,9 +6709,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 {
|
||||
|
|
|
@ -843,7 +843,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);
|
||||
|
|
12
tests/cases/fourslash/unusedTypeParametersWithTrivia1.ts
Normal file
12
tests/cases/fourslash/unusedTypeParametersWithTrivia1.ts
Normal file
|
@ -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
tests/cases/fourslash/unusedTypeParametersWithTrivia2.ts
Normal file
9
tests/cases/fourslash/unusedTypeParametersWithTrivia2.ts
Normal file
|
@ -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
tests/cases/fourslash/unusedTypeParametersWithTrivia3.ts
Normal file
9
tests/cases/fourslash/unusedTypeParametersWithTrivia3.ts
Normal file
|
@ -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
tests/cases/fourslash/unusedTypeParametersWithTrivia4.ts
Normal file
12
tests/cases/fourslash/unusedTypeParametersWithTrivia4.ts
Normal file
|
@ -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;"
|
||||
});
|
Loading…
Reference in a new issue