Skip to content

Commit 70fee3f

Browse files
committed
Review changes and adjustment of fourslash tests
1 parent 2dae10f commit 70fee3f

23 files changed

+129
-49
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4800,11 +4800,11 @@
48004800
"category": "Message",
48014801
"code": 95073
48024802
},
4803-
"Add const modifier to unresolved variable": {
4803+
"Add 'const' to unresolved variable": {
48044804
"category": "Message",
48054805
"code": 95074
48064806
},
4807-
"Add const modifiers to all unresolved variables": {
4807+
"Add 'const' to all unresolved variables": {
48084808
"category": "Message",
48094809
"code": 95075
48104810
}

src/services/codefixes/addMissingConstInForLoop.ts

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,48 @@ namespace ts.codefix {
66
errorCodes,
77
getCodeActions: (context) => {
88
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)];
9+
if (changes) {
10+
return [createCodeFixAction(fixId, changes, Diagnostics.Add_const_to_unresolved_variable, fixId, Diagnostics.Add_const_to_all_unresolved_variables)];
11+
}
1012
},
1113
fixIds: [fixId],
1214
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => makeChange(changes, diag.file, diag.start)),
1315
});
1416

1517
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: " " });
18+
const forInitializer = findAncestor(getTokenAtPosition(sourceFile, pos), node =>
19+
isForInOrOfStatement(node.parent) ? node.parent.initializer === node
20+
: isPossiblyPartOfDestructuring(node) ? false : "quit");
21+
if (!forInitializer) return;
22+
if (alreadyContainsConstCodeFixForInitializer(changeTracker, forInitializer, sourceFile)) return;
23+
changeTracker.insertNodeBefore(sourceFile, forInitializer, createToken(SyntaxKind.ConstKeyword));
24+
}
25+
26+
function isPossiblyPartOfDestructuring(node: Node): boolean {
27+
switch (node.kind) {
28+
case SyntaxKind.Identifier:
29+
case SyntaxKind.ArrayLiteralExpression:
30+
case SyntaxKind.ObjectLiteralExpression:
31+
case SyntaxKind.PropertyAssignment:
32+
case SyntaxKind.ShorthandPropertyAssignment:
33+
return true;
34+
default:
35+
return false;
36+
}
37+
}
38+
39+
function alreadyContainsConstCodeFixForInitializer(changeTracker: textChanges.ChangeTracker, forInitializer: Node, sourceFile: SourceFile): boolean {
40+
return changeTracker.getChanges().some(change => {
41+
const textChanges = change.textChanges;
42+
if (!textChanges) return false;
43+
return textChanges.some(textChange => {
44+
if (textChange.newText !== "const ") return false;
45+
const changeStart = textChange.span.start;
46+
const changeEnd = changeStart + textChange.span.length;
47+
const initStart = forInitializer.getStart(sourceFile);
48+
const initEnd = forInitializer.getEnd();
49+
return initStart <= changeEnd && changeStart <= initEnd;
50+
});
51+
});
2252
}
2353
}

src/services/textChanges.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,9 @@ namespace ts.textChanges {
403403
else if (isStringLiteral(before) && isImportDeclaration(before.parent) || isNamedImports(before)) {
404404
return { suffix: ", " };
405405
}
406+
else if (isForInitializer(before)) {
407+
return { suffix: " " };
408+
}
406409
return Debug.failBadSyntaxKind(before); // We haven't handled this kind of node yet -- add it
407410
}
408411

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 in []) {}
4+
5+
verify.codeFix({
6+
description: "Add 'const' to unresolved variable",
7+
newFileContent: "for (const x in []) {}"
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 in []) {}
4+
////for (y in []) {}
5+
6+
verify.codeFixAll({
7+
fixId: "addMissingConstInForLoop",
8+
fixAllDescription: "Add 'const' to all unresolved variables",
9+
newFileContent:
10+
`for (const x in []) {}
11+
for (const y in []) {}`
12+
});

tests/cases/fourslash/codeFixAddMissingConstInForLoop1.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/cases/fourslash/codeFixAddMissingConstInForLoop3.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/cases/fourslash/codeFixAddMissingConstInForLoop4.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.
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' to unresolved variable",
7+
newFileContent: "for (const [x] of [[1,2]]) {}"
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, y] of [[1,2]]) {}
4+
////for ([x] of [[1,2]]) {}
5+
6+
verify.codeFixAll({
7+
fixId: "addMissingConstInForLoop",
8+
fixAllDescription: "Add 'const' to all unresolved variables",
9+
newFileContent:
10+
`for (const [x, y] of [[1,2]]) {}
11+
for (const [x] of [[1,2]]) {}`
12+
});

0 commit comments

Comments
 (0)