Skip to content

Commit bf46ded

Browse files
author
Orta
authored
Merge pull request #33157 from fuafa/convert-const-to-let
Add convert const to let
2 parents ed152b7 + 67aa2b2 commit bf46ded

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5140,6 +5140,10 @@
51405140
"category": "Message",
51415141
"code": 95092
51425142
},
5143+
"Convert 'const' to 'let'": {
5144+
"category": "Message",
5145+
"code": 95093
5146+
},
51435147

51445148
"No value exists in scope for the shorthand property '{0}'. Either declare one or provide an initializer.": {
51455149
"category": "Error",
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* @internal */
2+
namespace ts.codefix {
3+
const fixId = "fixConvertConstToLet";
4+
const errorCodes = [Diagnostics.Cannot_assign_to_0_because_it_is_a_constant.code];
5+
6+
registerCodeFix({
7+
errorCodes,
8+
getCodeActions: context => {
9+
const { sourceFile, span, program } = context;
10+
const variableStatement = getVariableStatement(sourceFile, span.start, program);
11+
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, variableStatement));
12+
return [createCodeFixAction(fixId, changes, Diagnostics.Convert_const_to_let, fixId, Diagnostics.Convert_const_to_let)];
13+
},
14+
fixIds: [fixId]
15+
});
16+
17+
function getVariableStatement(sourceFile: SourceFile, pos: number, program: Program) {
18+
const token = getTokenAtPosition(sourceFile, pos);
19+
const checker = program.getTypeChecker();
20+
const symbol = checker.getSymbolAtLocation(token);
21+
if (symbol) {
22+
return symbol.valueDeclaration.parent.parent as VariableStatement;
23+
}
24+
}
25+
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, variableStatement?: VariableStatement) {
26+
if (!variableStatement) {
27+
return;
28+
}
29+
const start = variableStatement.getStart();
30+
changes.replaceRangeWithText(sourceFile, { pos: start, end: start + 5 }, "let");
31+
}
32+
}

src/services/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"codefixes/fixAddModuleReferTypeMissingTypeof.ts",
8484
"codefixes/convertToMappedObjectType.ts",
8585
"codefixes/removeUnnecessaryAwait.ts",
86+
"codefixes/convertConstToLet.ts",
8687
"refactors/convertExport.ts",
8788
"refactors/convertImport.ts",
8889
"refactors/extractSymbol.ts",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const x = 42;
4+
//// x = 75;
5+
6+
verify.codeFix({
7+
description: "Convert 'const' to 'let'",
8+
index: 0,
9+
newFileContent:
10+
`let x = 42;
11+
x = 75;`
12+
});

0 commit comments

Comments
 (0)