Skip to content

Commit

Permalink
fix(50435): Duplicate seeming Code Actions for convert const to let (#…
Browse files Browse the repository at this point in the history
…50442)

* fix(50435): omit fix all in constToLet QF

* add FixAll action
  • Loading branch information
a-tarasyuk committed Aug 25, 2022
1 parent a08b045 commit 12eb519
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Expand Up @@ -7064,6 +7064,10 @@
"category": "Message",
"code": 95101
},
"Convert all 'const' to 'let'": {
"category": "Message",
"code": 95102
},
"Convert function expression '{0}' to arrow function": {
"category": "Message",
"code": 95105
Expand Down
41 changes: 32 additions & 9 deletions src/services/codefixes/convertConstToLet.ts
Expand Up @@ -7,28 +7,51 @@ namespace ts.codefix {
errorCodes,
getCodeActions: function getCodeActionsToConvertConstToLet(context) {
const { sourceFile, span, program } = context;
const range = getConstTokenRange(sourceFile, span.start, program);
if (range === undefined) return;
const info = getInfo(sourceFile, span.start, program);
if (info === undefined) return;

const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, range));
return [createCodeFixAction(fixId, changes, Diagnostics.Convert_const_to_let, fixId, Diagnostics.Convert_const_to_let)];
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, info.token));
return [createCodeFixActionMaybeFixAll(fixId, changes, Diagnostics.Convert_const_to_let, fixId, Diagnostics.Convert_all_const_to_let)];
},
getAllCodeActions: context => {
const { program } = context;
const seen = new Map<number, true>();

return createCombinedCodeActions(textChanges.ChangeTracker.with(context, changes => {
eachDiagnostic(context, errorCodes, diag => {
const info = getInfo(diag.file, diag.start, program);
if (info) {
if (addToSeen(seen, getSymbolId(info.symbol))) {
return doChange(changes, diag.file, info.token);
}
}
return undefined;
});
}));
},
fixIds: [fixId]
});

function getConstTokenRange(sourceFile: SourceFile, pos: number, program: Program) {
interface Info {
symbol: Symbol;
token: Token<SyntaxKind.ConstKeyword>;
}

function getInfo(sourceFile: SourceFile, pos: number, program: Program): Info | undefined {
const checker = program.getTypeChecker();
const symbol = checker.getSymbolAtLocation(getTokenAtPosition(sourceFile, pos));
if (symbol === undefined) return;

const declaration = tryCast(symbol?.valueDeclaration?.parent, isVariableDeclarationList);
if (declaration === undefined) return;

const constToken = findChildOfKind(declaration, SyntaxKind.ConstKeyword, sourceFile);
const constToken = findChildOfKind<Token<SyntaxKind.ConstKeyword>>(declaration, SyntaxKind.ConstKeyword, sourceFile);
if (constToken === undefined) return;

return createRange(constToken.pos, constToken.end);
return { symbol, token: constToken };
}

function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, range: TextRange) {
changes.replaceRangeWithText(sourceFile, range, "let");
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, token: Token<SyntaxKind.ConstKeyword>) {
changes.replaceNode(sourceFile, token, factory.createToken(SyntaxKind.LetKeyword));
}
}
14 changes: 14 additions & 0 deletions tests/cases/fourslash/codeFixConstToLet4.ts
@@ -0,0 +1,14 @@
/// <reference path='fourslash.ts' />

////// Comment
////const a = 1;
////a = 2;

verify.codeFix({
description: "Convert 'const' to 'let'",
index: 0,
newFileContent:
`// Comment
let a = 1;
a = 2;`
});
14 changes: 14 additions & 0 deletions tests/cases/fourslash/codeFixConstToLet_all1.ts
@@ -0,0 +1,14 @@
/// <reference path="fourslash.ts" />

////const a = 1;
////a = 2;
////a = 3;

verify.codeFixAll({
fixId: "fixConvertConstToLet",
fixAllDescription: ts.Diagnostics.Convert_all_const_to_let.message,
newFileContent:
`let a = 1;
a = 2;
a = 3;`
});
22 changes: 22 additions & 0 deletions tests/cases/fourslash/codeFixConstToLet_all2.ts
@@ -0,0 +1,22 @@
/// <reference path="fourslash.ts" />

////const a = 1;
////a = 2;
////a = 3;
////
////const b = 1;
////b = 2;
////b = 3;

verify.codeFixAll({
fixId: "fixConvertConstToLet",
fixAllDescription: ts.Diagnostics.Convert_all_const_to_let.message,
newFileContent:
`let a = 1;
a = 2;
a = 3;
let b = 1;
b = 2;
b = 3;`
});

0 comments on commit 12eb519

Please sign in to comment.