Skip to content
Permalink
Browse files
fix(50435): Duplicate seeming Code Actions for convert const to let (#…
…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 12eb519b3f81d0999bbc4ce9a845744f4dfc215e
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 9 deletions.
@@ -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
@@ -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));
}
}
@@ -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;`
});
@@ -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;`
});
@@ -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.