Skip to content

Commit 1fd2371

Browse files
committed
Fixed codeFixAll behavior and adjusted changed fourslash tests
1 parent 94118b6 commit 1fd2371

5 files changed

+16
-28
lines changed

src/services/codefixes/addMissingConstInForLoop.ts

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace ts.codefix {
33
const fixId = "addMissingConstInForLoop";
44
const errorCodes = [Diagnostics.Cannot_find_name_0.code];
5+
56
registerCodeFix({
67
errorCodes,
78
getCodeActions: (context) => {
@@ -11,16 +12,20 @@ namespace ts.codefix {
1112
}
1213
},
1314
fixIds: [fixId],
14-
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => makeChange(changes, diag.file, diag.start)),
15+
getAllCodeActions: context => {
16+
const fixedNodes = new NodeSet();
17+
return codeFixAll(context, errorCodes, (changes, diag) => makeChange(changes, diag.file, diag.start, fixedNodes));
18+
},
1519
});
1620

17-
function makeChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number) {
21+
function makeChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number, fixedNodes?: NodeSet<Node>) {
1822
const forInitializer = findAncestor(getTokenAtPosition(sourceFile, pos), node =>
1923
isForInOrOfStatement(node.parent) ? node.parent.initializer === node
2024
: isPossiblyPartOfDestructuring(node) ? false : "quit");
2125
if (!forInitializer) return;
22-
if (alreadyContainsConstCodeFixForInitializer(changeTracker, forInitializer, sourceFile)) return;
23-
changeTracker.insertNodeBefore(sourceFile, forInitializer, createToken(SyntaxKind.ConstKeyword));
26+
if (!fixedNodes || fixedNodes.tryAdd(forInitializer)) {
27+
changeTracker.insertNodeBefore(sourceFile, forInitializer, createToken(SyntaxKind.ConstKeyword));
28+
}
2429
}
2530

2631
function isPossiblyPartOfDestructuring(node: Node): boolean {
@@ -35,19 +40,4 @@ namespace ts.codefix {
3540
return false;
3641
}
3742
}
38-
39-
function alreadyContainsConstCodeFixForInitializer(changeTracker: textChanges.ChangeTracker, forInitializer: Node, sourceFile: SourceFile): boolean {
40-
return changeTracker.getChanges().some(change => {
41-
const textChanges = change.textChanges;
42-
if (!textChanges) return false;
43-
return textChanges.some(textChange => {
44-
if (textChange.newText !== "const ") return false;
45-
const changeStart = textChange.span.start;
46-
const changeEnd = changeStart + textChange.span.length;
47-
const initStart = forInitializer.getStart(sourceFile);
48-
const initEnd = forInitializer.getEnd();
49-
return initStart <= changeEnd && changeStart <= initEnd;
50-
});
51-
});
52-
}
5343
}

tests/cases/fourslash/codeFixUnusedIdentifier_destructure_allUnused.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// @noUnusedParameters: true
55

66
////export {};
7-
////const { x, y } = [{}];
7+
////const { x, y } = {};
88

99
verify.codeFix({
1010
description: "Remove destructuring",

tests/cases/fourslash/codeFixUnusedIdentifier_destructure_allUnused_all.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// @noUnusedLocals: true
44
// @noUnusedParameters: true
55

6-
////const { x, y } = [{}];
7-
////const { a, b } = [{}];
6+
////const { x, y } = {};
7+
////const { a, b } = {};
88
////a;
99
////export function f({ a, b }, { x, y }) {
1010
//// a;
@@ -14,7 +14,7 @@ verify.codeFixAll({
1414
fixId: "unusedIdentifier_delete",
1515
fixAllDescription: "Delete all unused declarations",
1616
newFileContent:
17-
`const { a } = [{}];
17+
`const { a } = {};
1818
a;
1919
export function f({ a }) {
2020
a;

tests/cases/fourslash/codeFixUnusedIdentifier_destructure_allUnused_nested.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
// @noUnusedParameters: true
55

66
////export {};
7-
////const { x: { a, b } } = [{}];
7+
////const { x: { a, b } } = {{}};
88

99
verify.codeFix({
1010
description: "Remove destructuring",
1111
newFileContent:
1212
`export {};
13-
const { } = [{}];`,
13+
const { } = {{}};`,
1414
});

tests/cases/fourslash/incompleteFunctionCallCodefix3.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,4 @@
33
// @noImplicitAny: true
44
//// function ...q) {}} f(10);
55

6-
verify.not.codeFixAvailable([
7-
{ "description": "Infer parameter types from usage" }
8-
]);
6+
verify.not.codeFixAvailable();

0 commit comments

Comments
 (0)