Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7d08f17

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

11 files changed

+64-10Lines changed: 64 additions & 10 deletions
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎…es/codefixes/addMissingConstInForLoop.ts‎ ‎src/services/codefixes/addMissingConst.ts‎src/services/codefixes/addMissingConstInForLoop.ts renamed to src/services/codefixes/addMissingConst.ts src/services/codefixes/addMissingConstInForLoop.ts renamed to src/services/codefixes/addMissingConst.ts

Copy file name to clipboardExpand all lines: src/services/codefixes/addMissingConst.ts
+23-5Lines 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

Collapse file

‎src/services/tsconfig.json‎

Copy file name to clipboardExpand all lines: src/services/tsconfig.json
+1-1Lines 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",
Collapse file

‎tests/cases/fourslash/codeFixAddMissingConstInForInLoop2.ts‎

Copy file name to clipboardExpand all lines: tests/cases/fourslash/codeFixAddMissingConstInForInLoop2.ts
+1-1Lines 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 []) {}
Collapse file

‎tests/cases/fourslash/codeFixAddMissingConstInForLoopWithArrayDestructuring2.ts‎

Copy file name to clipboardExpand all lines: tests/cases/fourslash/codeFixAddMissingConstInForLoopWithArrayDestructuring2.ts
+1-1Lines 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]]) {}
Collapse file

‎tests/cases/fourslash/codeFixAddMissingConstInForLoopWithObjectDestructuring2.ts‎

Copy file name to clipboardExpand all lines: tests/cases/fourslash/codeFixAddMissingConstInForLoopWithObjectDestructuring2.ts
+1-1Lines 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 }]) { }
Collapse file

‎tests/cases/fourslash/codeFixAddMissingConstInForOfLoop2.ts‎

Copy file name to clipboardExpand all lines: tests/cases/fourslash/codeFixAddMissingConstInForOfLoop2.ts
+1-1Lines 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 []) {}
Collapse file
+8Lines 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+
});
Collapse file
+9Lines 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+
});
Collapse file
+6Lines 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();
Collapse file
+8Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.