fix(40257): fix type parameters range (#40265)

This commit is contained in:
Oleksandr T 2020-11-03 01:20:13 +02:00 committed by GitHub
parent ce8d702586
commit 3f92a6498f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 50 additions and 5 deletions

View file

@ -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 {

View file

@ -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 {

View file

@ -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);

View 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;"
});

View 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;"
});

View 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;"
});

View 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;"
});