diff --git a/src/services/codefixes/addMissingConstInForLoop.ts b/src/services/codefixes/addMissingConstInForLoop.ts index 3caea2222fa..d2add652e9a 100644 --- a/src/services/codefixes/addMissingConstInForLoop.ts +++ b/src/services/codefixes/addMissingConstInForLoop.ts @@ -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) { 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; - }); - }); - } } diff --git a/tests/cases/fourslash/codeFixUnusedIdentifier_destructure_allUnused.ts b/tests/cases/fourslash/codeFixUnusedIdentifier_destructure_allUnused.ts index 97556092ade..6f2dc76d0ae 100644 --- a/tests/cases/fourslash/codeFixUnusedIdentifier_destructure_allUnused.ts +++ b/tests/cases/fourslash/codeFixUnusedIdentifier_destructure_allUnused.ts @@ -4,7 +4,7 @@ // @noUnusedParameters: true ////export {}; -////const { x, y } = [{}]; +////const { x, y } = {}; verify.codeFix({ description: "Remove destructuring", diff --git a/tests/cases/fourslash/codeFixUnusedIdentifier_destructure_allUnused_all.ts b/tests/cases/fourslash/codeFixUnusedIdentifier_destructure_allUnused_all.ts index 066ba07c4f4..176f108c517 100644 --- a/tests/cases/fourslash/codeFixUnusedIdentifier_destructure_allUnused_all.ts +++ b/tests/cases/fourslash/codeFixUnusedIdentifier_destructure_allUnused_all.ts @@ -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; diff --git a/tests/cases/fourslash/codeFixUnusedIdentifier_destructure_allUnused_nested.ts b/tests/cases/fourslash/codeFixUnusedIdentifier_destructure_allUnused_nested.ts index dc1ae26b3d0..03a8ff826c7 100644 --- a/tests/cases/fourslash/codeFixUnusedIdentifier_destructure_allUnused_nested.ts +++ b/tests/cases/fourslash/codeFixUnusedIdentifier_destructure_allUnused_nested.ts @@ -4,11 +4,11 @@ // @noUnusedParameters: true ////export {}; -////const { x: { a, b } } = [{}]; +////const { x: { a, b } } = {{}}; verify.codeFix({ description: "Remove destructuring", newFileContent: `export {}; -const { } = [{}];`, +const { } = {{}};`, }); diff --git a/tests/cases/fourslash/incompleteFunctionCallCodefix3.ts b/tests/cases/fourslash/incompleteFunctionCallCodefix3.ts index db2e82f94bf..29072c7c855 100644 --- a/tests/cases/fourslash/incompleteFunctionCallCodefix3.ts +++ b/tests/cases/fourslash/incompleteFunctionCallCodefix3.ts @@ -3,6 +3,4 @@ // @noImplicitAny: true //// function ...q) {}} f(10); -verify.not.codeFixAvailable([ - { "description": "Infer parameter types from usage" } -]); +verify.not.codeFixAvailable();