Skip to content

Commit 1e6a5f3

Browse files
committed
Add codefix for 'Cannot find name' diagnostic
1 parent b90d291 commit 1e6a5f3

8 files changed

+69
-1
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4799,5 +4799,13 @@
47994799
"Add names to all parameters without names": {
48004800
"category": "Message",
48014801
"code": 95073
4802+
},
4803+
"Add const modifier to unresolved variable": {
4804+
"category": "Message",
4805+
"code": 95074
4806+
},
4807+
"Add const modifiers to all unresolved variables": {
4808+
"category": "Message",
4809+
"code": 95075
48024810
}
48034811
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* @internal */
2+
namespace ts.codefix {
3+
const fixId = "addMissingConstInForLoop";
4+
const errorCodes = [Diagnostics.Cannot_find_name_0.code];
5+
registerCodeFix({
6+
errorCodes,
7+
getCodeActions: (context) => {
8+
const changes = textChanges.ChangeTracker.with(context, t => makeChange(t, context.sourceFile, context.span.start));
9+
return [createCodeFixAction(fixId, changes, Diagnostics.Add_const_modifier_to_unresolved_variable, fixId, Diagnostics.Add_const_modifiers_to_all_unresolved_variables)];
10+
},
11+
fixIds: [fixId],
12+
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => makeChange(changes, diag.file, diag.start)),
13+
});
14+
15+
function makeChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number) {
16+
const token = getTokenAtPosition(sourceFile, pos);
17+
// fails on 'for ([x, y] of [[1,2]]) {}' when called for y
18+
// since findPrecedingMatchingToken does not return the open paren here after iterating over another identifier (x)
19+
const openParenToken = <Node>findPrecedingMatchingToken(token, SyntaxKind.OpenParenToken, sourceFile);
20+
Debug.assert(!!openParenToken, "openParenToken must be defined");
21+
changeTracker.insertNodeAt(sourceFile, openParenToken.getEnd(), createToken(SyntaxKind.ConstKeyword), { suffix: " " });
22+
}
23+
}

src/services/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"codeFixProvider.ts",
4545
"refactorProvider.ts",
4646
"codefixes/addConvertToUnknownForNonOverlappingTypes.ts",
47+
"codefixes/addMissingConstInForLoop.ts",
4748
"codefixes/addMissingInvocationForDecorator.ts",
4849
"codefixes/addNameToNamelessParameter.ts",
4950
"codefixes/annotateWithTypeFromJSDoc.ts",

src/services/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ namespace ts {
936936
return undefined;
937937
}
938938
token = preceding;
939-
939+
940940
if (token.kind === matchingTokenKind) {
941941
if (remainingMatchingTokens === 0) {
942942
return token;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////[|for (x of []) {}|]
4+
5+
verify.codeFix({
6+
description: "Add const modifier to unresolved variable",
7+
newRangeContent: "for (const x of []) {}"
8+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////[|for (x of []) {}|]
4+
////[|for (y of []) {}|]
5+
6+
verify.codeFixAll({
7+
fixId: "addMissingConstInForLoop",
8+
fixAllDescription: "Add const modifiers to all unresolved variables",
9+
newFileContent:
10+
`for (const x of []) {}
11+
for (const y of []) {}`
12+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////[|for ([x] of [[1,2]]) {}|]
4+
5+
verify.codeFix({
6+
description: "Add const modifier to unresolved variable",
7+
newRangeContent: "for (const [x] of [[1,2]]) {}"
8+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////[|for ([x, y] of [[1,2]]) {}|]
4+
5+
verify.codeFix({
6+
description: "Add const modifier to unresolved variable",
7+
newRangeContent: "for (const [x, y] of [[1,2]]) {}"
8+
});

0 commit comments

Comments
 (0)