Skip to content

Commit 7d08f17

Browse files
committed
Added fourslash tests for standalone and array initialization cases and started implementing them
1 parent 573b537 commit 7d08f17

11 files changed

+64
-10
lines changed

src/services/codefixes/addMissingConstInForLoop.ts renamed to src/services/codefixes/addMissingConst.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* @internal */
22
namespace ts.codefix {
3-
const fixId = "addMissingConstInForLoop";
3+
const fixId = "addMissingConst";
44
const errorCodes = [
55
Diagnostics.Cannot_find_name_0.code,
66
Diagnostics.No_value_exists_in_scope_for_the_shorthand_property_0_Either_declare_one_or_provide_an_initializer.code
@@ -22,12 +22,30 @@ namespace ts.codefix {
2222
});
2323

2424
function makeChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number, fixedNodes?: NodeSet<Node>) {
25-
const forInitializer = findAncestor(getTokenAtPosition(sourceFile, pos), node =>
25+
const token = getTokenAtPosition(sourceFile, pos);
26+
27+
const forInitializer = findAncestor(token, node =>
2628
isForInOrOfStatement(node.parent) ? node.parent.initializer === node
2729
: isPossiblyPartOfDestructuring(node) ? false : "quit");
28-
if (!forInitializer) return;
29-
if (!fixedNodes || fixedNodes.tryAdd(forInitializer)) {
30-
changeTracker.insertNodeBefore(sourceFile, forInitializer, createToken(SyntaxKind.ConstKeyword));
30+
if (forInitializer) return applyChange(changeTracker, forInitializer, sourceFile, fixedNodes);
31+
32+
const parent = token.parent;
33+
const standaloneInitializer = isExpressionStatement(parent.parent);
34+
if (standaloneInitializer) return applyChange(changeTracker, parent, sourceFile, fixedNodes);
35+
36+
const arrayLiteralInitializer = isArrayLiteralExpression(token.parent);
37+
if (arrayLiteralInitializer) {
38+
const availableIdentifiers: string[] = []; // TODO: where to get/gather this information from?
39+
const noIdentifiersDeclared = parent.forEachChild(node => availableIdentifiers.indexOf(node.getFullText()) < 0);
40+
if (!noIdentifiersDeclared) return;
41+
42+
return applyChange(changeTracker, parent, sourceFile, fixedNodes);
43+
}
44+
}
45+
46+
function applyChange(changeTracker: textChanges.ChangeTracker, initializer: Node, sourceFile: SourceFile, fixedNodes?: NodeSet<Node>) {
47+
if (!fixedNodes || fixedNodes.tryAdd(initializer)) {
48+
changeTracker.insertNodeBefore(sourceFile, initializer, createToken(SyntaxKind.ConstKeyword));
3149
}
3250
}
3351

src/services/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"codeFixProvider.ts",
4646
"refactorProvider.ts",
4747
"codefixes/addConvertToUnknownForNonOverlappingTypes.ts",
48-
"codefixes/addMissingConstInForLoop.ts",
48+
"codefixes/addMissingConst.ts",
4949
"codefixes/addMissingInvocationForDecorator.ts",
5050
"codefixes/addNameToNamelessParameter.ts",
5151
"codefixes/annotateWithTypeFromJSDoc.ts",

tests/cases/fourslash/codeFixAddMissingConstInForInLoop2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
////for (y in []) {}
55

66
verify.codeFixAll({
7-
fixId: "addMissingConstInForLoop",
7+
fixId: "addMissingConst",
88
fixAllDescription: "Add 'const' to all unresolved variables",
99
newFileContent:
1010
`for (const x in []) {}

tests/cases/fourslash/codeFixAddMissingConstInForLoopWithArrayDestructuring2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
////for ([x] of [[1,2]]) {}
55

66
verify.codeFixAll({
7-
fixId: "addMissingConstInForLoop",
7+
fixId: "addMissingConst",
88
fixAllDescription: "Add 'const' to all unresolved variables",
99
newFileContent:
1010
`for (const [x, y] of [[1,2]]) {}

tests/cases/fourslash/codeFixAddMissingConstInForLoopWithObjectDestructuring2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
////for ({ x } of [{ x: 0 }]) { }
55

66
verify.codeFixAll({
7-
fixId: "addMissingConstInForLoop",
7+
fixId: "addMissingConst",
88
fixAllDescription: "Add 'const' to all unresolved variables",
99
newFileContent:
1010
`for (const { x, y } of [{ x: 0, y: 1 }]) { }

tests/cases/fourslash/codeFixAddMissingConstInForOfLoop2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
////for (y of []) {}
55

66
verify.codeFixAll({
7-
fixId: "addMissingConstInForLoop",
7+
fixId: "addMissingConst",
88
fixAllDescription: "Add 'const' to all unresolved variables",
99
newFileContent:
1010
`for (const x of []) {}
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+
////[x] = [0];
4+
5+
verify.codeFix({
6+
description: "Add 'const' to unresolved variable",
7+
newFileContent: "const [x] = [0];"
8+
});
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+
////[x, y] = [0, 1];
4+
5+
verify.codeFixAll({
6+
fixId: "addMissingConst",
7+
fixAllDescription: "Add 'const' to all unresolved variables",
8+
newFileContent: "const [x, y] = [0, 1];"
9+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////let x: any;
4+
////[x, y] = [0, 1];
5+
6+
verify.not.codeFixAvailable();
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+
////x = 0;
4+
5+
verify.codeFix({
6+
description: "Add 'const' to unresolved variable",
7+
newFileContent: "const x = 0;"
8+
});

0 commit comments

Comments
 (0)