Skip to content

Commit f273448

Browse files
committed
Added addMissingConst codefix for comma separated initializers
1 parent 1de7881 commit f273448

5 files changed

+73
-1
lines changed

src/services/codefixes/addMissingConst.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ namespace ts.codefix {
4242

4343
return applyChange(changeTracker, parent, sourceFile, fixedNodes);
4444
}
45+
46+
const commaExpression = findAncestor(token, node =>
47+
isExpressionStatement(node.parent) ? true :
48+
isPossiblyPartOfCommaSeperatedInitializer(node) ? false : "quit"
49+
);
50+
if (commaExpression) {
51+
const checker = program.getTypeChecker();
52+
if (!expressionCouldBeVariableDeclaration(commaExpression, checker)) {
53+
return;
54+
}
55+
56+
return applyChange(changeTracker, commaExpression, sourceFile, fixedNodes);
57+
}
4558
}
4659

4760
function applyChange(changeTracker: textChanges.ChangeTracker, initializer: Node, sourceFile: SourceFile, fixedNodes?: NodeSet<Node>) {
@@ -63,11 +76,34 @@ namespace ts.codefix {
6376
}
6477
}
6578

66-
function arrayElementCouldBeVariableDeclaration(expression: Expression, checker: TypeChecker) {
79+
function arrayElementCouldBeVariableDeclaration(expression: Expression, checker: TypeChecker): boolean {
6780
const identifier =
6881
isIdentifier(expression) ? expression :
6982
isAssignmentExpression(expression, /*excludeCompoundAssignment*/ true) && isIdentifier(expression.left) ? expression.left :
7083
undefined;
7184
return !!identifier && !checker.getSymbolAtLocation(identifier);
7285
}
86+
87+
function isPossiblyPartOfCommaSeperatedInitializer(node: Node): boolean {
88+
switch (node.kind) {
89+
case SyntaxKind.Identifier:
90+
case SyntaxKind.BinaryExpression:
91+
case SyntaxKind.CommaToken:
92+
return true;
93+
default:
94+
return false;
95+
}
96+
}
97+
98+
function expressionCouldBeVariableDeclaration(expression: Node, checker: TypeChecker): boolean {
99+
if (!isBinaryExpression(expression)) {
100+
return false;
101+
}
102+
103+
if (expression.operatorToken.kind === SyntaxKind.CommaToken) {
104+
return every([expression.left, expression.right], expression => expressionCouldBeVariableDeclaration(expression, checker));
105+
}
106+
107+
return isIdentifier(expression.left) && !checker.getSymbolAtLocation(expression.left);
108+
}
73109
}
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 = 0, y = 0;
4+
5+
verify.codeFixAll({
6+
fixId: "addMissingConst",
7+
fixAllDescription: "Add 'const' to all unresolved variables",
8+
newFileContent: "const x = 0, y = 0;"
9+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////x = 0,
4+
////y = 0;
5+
6+
verify.codeFixAll({
7+
fixId: "addMissingConst",
8+
fixAllDescription: "Add 'const' to all unresolved variables",
9+
newFileContent: `const x = 0,
10+
y = 0;`
11+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////function f() { return 42; }
4+
////x = 0, y = f()
5+
////
6+
////, z = 0;
7+
8+
verify.codeFixAll({
9+
fixId: "addMissingConst",
10+
fixAllDescription: "Add 'const' to all unresolved variables",
11+
newFileContent: `function f() { return 42; }
12+
const x = 0, y = f()
13+
14+
, z = 0;`
15+
});
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/// <reference path='fourslash.ts' />
22

3+
////let y: any;
34
////x = 0, y = 0;
45

56
verify.not.codeFixAvailable();

0 commit comments

Comments
 (0)