Review changes and adjustment of fourslash tests

This commit is contained in:
Florian Regensburger
2018-11-02 01:32:12 +01:00
parent 2dae10f6ba
commit 70fee3fb36
23 changed files with 129 additions and 49 deletions
+2 -2
View File
@@ -4800,11 +4800,11 @@
"category": "Message",
"code": 95073
},
"Add const modifier to unresolved variable": {
"Add 'const' to unresolved variable": {
"category": "Message",
"code": 95074
},
"Add const modifiers to all unresolved variables": {
"Add 'const' to all unresolved variables": {
"category": "Message",
"code": 95075
}
@@ -6,18 +6,48 @@ namespace ts.codefix {
errorCodes,
getCodeActions: (context) => {
const changes = textChanges.ChangeTracker.with(context, t => makeChange(t, context.sourceFile, context.span.start));
return [createCodeFixAction(fixId, changes, Diagnostics.Add_const_modifier_to_unresolved_variable, fixId, Diagnostics.Add_const_modifiers_to_all_unresolved_variables)];
if (changes) {
return [createCodeFixAction(fixId, changes, Diagnostics.Add_const_to_unresolved_variable, fixId, Diagnostics.Add_const_to_all_unresolved_variables)];
}
},
fixIds: [fixId],
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => makeChange(changes, diag.file, diag.start)),
});
function makeChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number) {
const token = getTokenAtPosition(sourceFile, pos);
// fails on 'for ([x, y] of [[1,2]]) {}' when called for y
// since findPrecedingMatchingToken does not return the open paren here after iterating over another identifier (x)
const openParenToken = <Node>findPrecedingMatchingToken(token, SyntaxKind.OpenParenToken, sourceFile);
Debug.assert(!!openParenToken, "openParenToken must be defined");
changeTracker.insertNodeAt(sourceFile, openParenToken.getEnd(), createToken(SyntaxKind.ConstKeyword), { suffix: " " });
const forInitializer = findAncestor(getTokenAtPosition(sourceFile, pos), node =>
isForInOrOfStatement(node.parent) ? node.parent.initializer === node
: isPossiblyPartOfDestructuring(node) ? false : "quit");
if (!forInitializer) return;
if (alreadyContainsConstCodeFixForInitializer(changeTracker, forInitializer, sourceFile)) return;
changeTracker.insertNodeBefore(sourceFile, forInitializer, createToken(SyntaxKind.ConstKeyword));
}
function isPossiblyPartOfDestructuring(node: Node): boolean {
switch (node.kind) {
case SyntaxKind.Identifier:
case SyntaxKind.ArrayLiteralExpression:
case SyntaxKind.ObjectLiteralExpression:
case SyntaxKind.PropertyAssignment:
case SyntaxKind.ShorthandPropertyAssignment:
return true;
default:
return false;
}
}
function alreadyContainsConstCodeFixForInitializer(changeTracker: textChanges.ChangeTracker, forInitializer: Node, sourceFile: SourceFile): boolean {
return changeTracker.getChanges().some(change => {
const textChanges = change.textChanges;
if (!textChanges) return false;
return textChanges.some(textChange => {
if (textChange.newText !== "const ") return false;
const changeStart = textChange.span.start;
const changeEnd = changeStart + textChange.span.length;
const initStart = forInitializer.getStart(sourceFile);
const initEnd = forInitializer.getEnd();
return initStart <= changeEnd && changeStart <= initEnd;
});
});
}
}
+3
View File
@@ -403,6 +403,9 @@ namespace ts.textChanges {
else if (isStringLiteral(before) && isImportDeclaration(before.parent) || isNamedImports(before)) {
return { suffix: ", " };
}
else if (isForInitializer(before)) {
return { suffix: " " };
}
return Debug.failBadSyntaxKind(before); // We haven't handled this kind of node yet -- add it
}