Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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;`
});