fix(55014): Quick fix for ts7051 introduces incorrect type (#55020)

This commit is contained in:
Oleksandr T
2023-08-31 01:17:43 +03:00
committed by GitHub
parent f424a6be05
commit fe82a610cd
4 changed files with 59 additions and 5 deletions

View File

@@ -1,12 +1,19 @@
import {
createRange,
Debug,
Diagnostics,
factory,
findNextToken,
getTokenAtPosition,
Identifier,
isArrayBindingPattern,
isArrayTypeNode,
isParameter,
ParameterDeclaration,
SourceFile,
SyntaxKind,
textChanges,
TypeNode,
} from "../_namespaces/ts";
import {
codeFixAll,
@@ -26,8 +33,8 @@ registerCodeFix({
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => makeChange(changes, diag.file, diag.start)),
});
function makeChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number) {
const token = getTokenAtPosition(sourceFile, pos);
function makeChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, start: number) {
const token = getTokenAtPosition(sourceFile, start);
const param = token.parent;
if (!isParameter(param)) {
return Debug.fail("Tried to add a parameter name to a non-parameter: " + Debug.formatSyntaxKind(token.kind));
@@ -37,14 +44,33 @@ function makeChange(changeTracker: textChanges.ChangeTracker, sourceFile: Source
Debug.assert(!param.type, "Tried to add a parameter name to a parameter that already had one.");
Debug.assert(i > -1, "Parameter not found in parent parameter list.");
const typeNode = factory.createTypeReferenceNode(param.name as Identifier, /*typeArguments*/ undefined);
let end = param.name.getEnd();
let typeNode: TypeNode = factory.createTypeReferenceNode(param.name as Identifier, /*typeArguments*/ undefined);
let nextParam = tryGetNextParam(sourceFile, param);
while (nextParam) {
typeNode = factory.createArrayTypeNode(typeNode);
end = nextParam.getEnd();
nextParam = tryGetNextParam(sourceFile, nextParam);
}
const replacement = factory.createParameterDeclaration(
param.modifiers,
param.dotDotDotToken,
"arg" + i,
param.questionToken,
param.dotDotDotToken ? factory.createArrayTypeNode(typeNode) : typeNode,
param.dotDotDotToken && !isArrayTypeNode(typeNode) ? factory.createArrayTypeNode(typeNode) : typeNode,
param.initializer,
);
changeTracker.replaceNode(sourceFile, param, replacement);
changeTracker.replaceRange(sourceFile, createRange(param.getStart(sourceFile), end), replacement);
}
function tryGetNextParam(sourceFile: SourceFile, param: ParameterDeclaration) {
const nextToken = findNextToken(param.name, param.parent, sourceFile);
if (
nextToken && nextToken.kind === SyntaxKind.OpenBracketToken
&& isArrayBindingPattern(nextToken.parent) && isParameter(nextToken.parent.parent)
) {
return nextToken.parent.parent;
}
return undefined;
}

View File

@@ -0,0 +1,9 @@
/// <reference path='fourslash.ts' />
// @noImplicitAny: true
////function fn(f: (...number[]) => unknown) {}
verify.codeFix({
description: ts.Diagnostics.Add_parameter_name.message,
newFileContent: `function fn(f: (...arg0: number[]) => unknown) {}`,
});

View File

@@ -0,0 +1,9 @@
/// <reference path='fourslash.ts' />
// @noImplicitAny: true
////function fn(f: (...number[][][]) => unknown) {}
verify.codeFix({
description: ts.Diagnostics.Add_parameter_name.message,
newFileContent: `function fn(f: (...arg0: number[][][]) => unknown) {}`,
});

View File

@@ -0,0 +1,10 @@
/// <reference path='fourslash.ts' />
// @noImplicitAny: true
////function fn(f: (...number) => unknown) {}
verify.codeFix({
index: 0,
description: ts.Diagnostics.Add_parameter_name.message,
newFileContent: `function fn(f: (...arg0: number[]) => unknown) {}`,
});