Skip to content
This repository was archived by the owner on Jan 13, 2024. It is now read-only.

Commit fe82a61

Browse files
authored
fix(55014): Quick fix for ts7051 introduces incorrect type (microsoft#55020)
1 parent f424a6b commit fe82a61

File tree

4 files changed

+59
-5
lines changed

4 files changed

+59
-5
lines changed

src/services/codefixes/addNameToNamelessParameter.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import {
2+
createRange,
23
Debug,
34
Diagnostics,
45
factory,
6+
findNextToken,
57
getTokenAtPosition,
68
Identifier,
9+
isArrayBindingPattern,
10+
isArrayTypeNode,
711
isParameter,
12+
ParameterDeclaration,
813
SourceFile,
14+
SyntaxKind,
915
textChanges,
16+
TypeNode,
1017
} from "../_namespaces/ts";
1118
import {
1219
codeFixAll,
@@ -26,8 +33,8 @@ registerCodeFix({
2633
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => makeChange(changes, diag.file, diag.start)),
2734
});
2835

29-
function makeChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number) {
30-
const token = getTokenAtPosition(sourceFile, pos);
36+
function makeChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, start: number) {
37+
const token = getTokenAtPosition(sourceFile, start);
3138
const param = token.parent;
3239
if (!isParameter(param)) {
3340
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
3744
Debug.assert(!param.type, "Tried to add a parameter name to a parameter that already had one.");
3845
Debug.assert(i > -1, "Parameter not found in parent parameter list.");
3946

40-
const typeNode = factory.createTypeReferenceNode(param.name as Identifier, /*typeArguments*/ undefined);
47+
let end = param.name.getEnd();
48+
let typeNode: TypeNode = factory.createTypeReferenceNode(param.name as Identifier, /*typeArguments*/ undefined);
49+
let nextParam = tryGetNextParam(sourceFile, param);
50+
while (nextParam) {
51+
typeNode = factory.createArrayTypeNode(typeNode);
52+
end = nextParam.getEnd();
53+
nextParam = tryGetNextParam(sourceFile, nextParam);
54+
}
55+
4156
const replacement = factory.createParameterDeclaration(
4257
param.modifiers,
4358
param.dotDotDotToken,
4459
"arg" + i,
4560
param.questionToken,
46-
param.dotDotDotToken ? factory.createArrayTypeNode(typeNode) : typeNode,
61+
param.dotDotDotToken && !isArrayTypeNode(typeNode) ? factory.createArrayTypeNode(typeNode) : typeNode,
4762
param.initializer,
4863
);
49-
changeTracker.replaceNode(sourceFile, param, replacement);
64+
changeTracker.replaceRange(sourceFile, createRange(param.getStart(sourceFile), end), replacement);
65+
}
66+
67+
function tryGetNextParam(sourceFile: SourceFile, param: ParameterDeclaration) {
68+
const nextToken = findNextToken(param.name, param.parent, sourceFile);
69+
if (
70+
nextToken && nextToken.kind === SyntaxKind.OpenBracketToken
71+
&& isArrayBindingPattern(nextToken.parent) && isParameter(nextToken.parent.parent)
72+
) {
73+
return nextToken.parent.parent;
74+
}
75+
return undefined;
5076
}
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+
// @noImplicitAny: true
4+
////function fn(f: (...number[]) => unknown) {}
5+
6+
verify.codeFix({
7+
description: ts.Diagnostics.Add_parameter_name.message,
8+
newFileContent: `function fn(f: (...arg0: number[]) => unknown) {}`,
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+
// @noImplicitAny: true
4+
////function fn(f: (...number[][][]) => unknown) {}
5+
6+
verify.codeFix({
7+
description: ts.Diagnostics.Add_parameter_name.message,
8+
newFileContent: `function fn(f: (...arg0: number[][][]) => unknown) {}`,
9+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @noImplicitAny: true
4+
////function fn(f: (...number) => unknown) {}
5+
6+
verify.codeFix({
7+
index: 0,
8+
description: ts.Diagnostics.Add_parameter_name.message,
9+
newFileContent: `function fn(f: (...arg0: number[]) => unknown) {}`,
10+
});

0 commit comments

Comments
 (0)