mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Added addMissingConst codefix for comma separated initializers
This commit is contained in:
@@ -42,6 +42,19 @@ namespace ts.codefix {
|
||||
|
||||
return applyChange(changeTracker, parent, sourceFile, fixedNodes);
|
||||
}
|
||||
|
||||
const commaExpression = findAncestor(token, node =>
|
||||
isExpressionStatement(node.parent) ? true :
|
||||
isPossiblyPartOfCommaSeperatedInitializer(node) ? false : "quit"
|
||||
);
|
||||
if (commaExpression) {
|
||||
const checker = program.getTypeChecker();
|
||||
if (!expressionCouldBeVariableDeclaration(commaExpression, checker)) {
|
||||
return;
|
||||
}
|
||||
|
||||
return applyChange(changeTracker, commaExpression, sourceFile, fixedNodes);
|
||||
}
|
||||
}
|
||||
|
||||
function applyChange(changeTracker: textChanges.ChangeTracker, initializer: Node, sourceFile: SourceFile, fixedNodes?: NodeSet<Node>) {
|
||||
@@ -63,11 +76,34 @@ namespace ts.codefix {
|
||||
}
|
||||
}
|
||||
|
||||
function arrayElementCouldBeVariableDeclaration(expression: Expression, checker: TypeChecker) {
|
||||
function arrayElementCouldBeVariableDeclaration(expression: Expression, checker: TypeChecker): boolean {
|
||||
const identifier =
|
||||
isIdentifier(expression) ? expression :
|
||||
isAssignmentExpression(expression, /*excludeCompoundAssignment*/ true) && isIdentifier(expression.left) ? expression.left :
|
||||
undefined;
|
||||
return !!identifier && !checker.getSymbolAtLocation(identifier);
|
||||
}
|
||||
|
||||
function isPossiblyPartOfCommaSeperatedInitializer(node: Node): boolean {
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.Identifier:
|
||||
case SyntaxKind.BinaryExpression:
|
||||
case SyntaxKind.CommaToken:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function expressionCouldBeVariableDeclaration(expression: Node, checker: TypeChecker): boolean {
|
||||
if (!isBinaryExpression(expression)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (expression.operatorToken.kind === SyntaxKind.CommaToken) {
|
||||
return every([expression.left, expression.right], expression => expressionCouldBeVariableDeclaration(expression, checker));
|
||||
}
|
||||
|
||||
return isIdentifier(expression.left) && !checker.getSymbolAtLocation(expression.left);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
/// <reference path='fourslash.ts' />
|
||||
|
||||
////x = 0, y = 0;
|
||||
|
||||
verify.codeFixAll({
|
||||
fixId: "addMissingConst",
|
||||
fixAllDescription: "Add 'const' to all unresolved variables",
|
||||
newFileContent: "const x = 0, y = 0;"
|
||||
});
|
||||
@@ -0,0 +1,11 @@
|
||||
/// <reference path='fourslash.ts' />
|
||||
|
||||
////x = 0,
|
||||
////y = 0;
|
||||
|
||||
verify.codeFixAll({
|
||||
fixId: "addMissingConst",
|
||||
fixAllDescription: "Add 'const' to all unresolved variables",
|
||||
newFileContent: `const x = 0,
|
||||
y = 0;`
|
||||
});
|
||||
@@ -0,0 +1,15 @@
|
||||
/// <reference path='fourslash.ts' />
|
||||
|
||||
////function f() { return 42; }
|
||||
////x = 0, y = f()
|
||||
////
|
||||
////, z = 0;
|
||||
|
||||
verify.codeFixAll({
|
||||
fixId: "addMissingConst",
|
||||
fixAllDescription: "Add 'const' to all unresolved variables",
|
||||
newFileContent: `function f() { return 42; }
|
||||
const x = 0, y = f()
|
||||
|
||||
, z = 0;`
|
||||
});
|
||||
+1
@@ -1,5 +1,6 @@
|
||||
/// <reference path='fourslash.ts' />
|
||||
|
||||
////let y: any;
|
||||
////x = 0, y = 0;
|
||||
|
||||
verify.not.codeFixAvailable();
|
||||
Reference in New Issue
Block a user