Fixed codeFixAll behavior and adjusted changed fourslash tests

This commit is contained in:
Florian Regensburger
2018-11-03 00:00:12 +01:00
parent 94118b64c5
commit 1fd2371a37
5 changed files with 16 additions and 28 deletions

View File

@@ -2,6 +2,7 @@
namespace ts.codefix {
const fixId = "addMissingConstInForLoop";
const errorCodes = [Diagnostics.Cannot_find_name_0.code];
registerCodeFix({
errorCodes,
getCodeActions: (context) => {
@@ -11,16 +12,20 @@ namespace ts.codefix {
}
},
fixIds: [fixId],
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => makeChange(changes, diag.file, diag.start)),
getAllCodeActions: context => {
const fixedNodes = new NodeSet();
return codeFixAll(context, errorCodes, (changes, diag) => makeChange(changes, diag.file, diag.start, fixedNodes));
},
});
function makeChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number) {
function makeChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number, fixedNodes?: NodeSet<Node>) {
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));
if (!fixedNodes || fixedNodes.tryAdd(forInitializer)) {
changeTracker.insertNodeBefore(sourceFile, forInitializer, createToken(SyntaxKind.ConstKeyword));
}
}
function isPossiblyPartOfDestructuring(node: Node): boolean {
@@ -35,19 +40,4 @@ namespace ts.codefix {
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;
});
});
}
}

View File

@@ -4,7 +4,7 @@
// @noUnusedParameters: true
////export {};
////const { x, y } = [{}];
////const { x, y } = {};
verify.codeFix({
description: "Remove destructuring",

View File

@@ -3,8 +3,8 @@
// @noUnusedLocals: true
// @noUnusedParameters: true
////const { x, y } = [{}];
////const { a, b } = [{}];
////const { x, y } = {};
////const { a, b } = {};
////a;
////export function f({ a, b }, { x, y }) {
//// a;
@@ -14,7 +14,7 @@ verify.codeFixAll({
fixId: "unusedIdentifier_delete",
fixAllDescription: "Delete all unused declarations",
newFileContent:
`const { a } = [{}];
`const { a } = {};
a;
export function f({ a }) {
a;

View File

@@ -4,11 +4,11 @@
// @noUnusedParameters: true
////export {};
////const { x: { a, b } } = [{}];
////const { x: { a, b } } = {{}};
verify.codeFix({
description: "Remove destructuring",
newFileContent:
`export {};
const { } = [{}];`,
const { } = {{}};`,
});

View File

@@ -3,6 +3,4 @@
// @noImplicitAny: true
//// function ...q) {}} f(10);
verify.not.codeFixAvailable([
{ "description": "Infer parameter types from usage" }
]);
verify.not.codeFixAvailable();