diff --git a/src/services/codefixes/convertConstToLet.ts b/src/services/codefixes/convertConstToLet.ts
index ba54cea297a..fddb697911c 100644
--- a/src/services/codefixes/convertConstToLet.ts
+++ b/src/services/codefixes/convertConstToLet.ts
@@ -7,26 +7,28 @@ namespace ts.codefix {
errorCodes,
getCodeActions: context => {
const { sourceFile, span, program } = context;
- const variableStatement = getVariableStatement(sourceFile, span.start, program);
- const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, variableStatement));
+ const range = getConstTokenRange(sourceFile, span.start, program);
+ if (range === 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)];
},
fixIds: [fixId]
});
- function getVariableStatement(sourceFile: SourceFile, pos: number, program: Program) {
- const token = getTokenAtPosition(sourceFile, pos);
+ function getConstTokenRange(sourceFile: SourceFile, pos: number, program: Program) {
const checker = program.getTypeChecker();
- const symbol = checker.getSymbolAtLocation(token);
- if (symbol?.valueDeclaration) {
- return symbol.valueDeclaration.parent.parent as VariableStatement;
- }
+ const symbol = checker.getSymbolAtLocation(getTokenAtPosition(sourceFile, pos));
+ const declaration = tryCast(symbol?.valueDeclaration?.parent, isVariableDeclarationList);
+ if (declaration === undefined) return;
+
+ const constToken = findChildOfKind(declaration, SyntaxKind.ConstKeyword, sourceFile);
+ if (constToken === undefined) return;
+
+ return createRange(constToken.pos, constToken.end);
}
- function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, variableStatement?: VariableStatement) {
- if (!variableStatement) {
- return;
- }
- const start = variableStatement.getStart();
- changes.replaceRangeWithText(sourceFile, { pos: start, end: start + 5 }, "let");
+
+ function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, range: TextRange) {
+ changes.replaceRangeWithText(sourceFile, range, "let");
}
}
diff --git a/tests/cases/fourslash/codeFixConstToLet.ts b/tests/cases/fourslash/codeFixConstToLet.ts
deleted file mode 100644
index 949c45104a2..00000000000
--- a/tests/cases/fourslash/codeFixConstToLet.ts
+++ /dev/null
@@ -1,12 +0,0 @@
-///
-
-//// const x = 42;
-//// x = 75;
-
-verify.codeFix({
- description: "Convert 'const' to 'let'",
- index: 0,
- newFileContent:
-`let x = 42;
-x = 75;`
-});
\ No newline at end of file
diff --git a/tests/cases/fourslash/codeFixConstToLet1.ts b/tests/cases/fourslash/codeFixConstToLet1.ts
new file mode 100644
index 00000000000..35d789529f5
--- /dev/null
+++ b/tests/cases/fourslash/codeFixConstToLet1.ts
@@ -0,0 +1,12 @@
+///
+
+////const x = 42;
+////x = 75;
+
+verify.codeFix({
+ description: "Convert 'const' to 'let'",
+ index: 0,
+ newFileContent:
+`let x = 42;
+x = 75;`
+});
diff --git a/tests/cases/fourslash/codeFixConstToLet2.ts b/tests/cases/fourslash/codeFixConstToLet2.ts
new file mode 100644
index 00000000000..0dddff803f5
--- /dev/null
+++ b/tests/cases/fourslash/codeFixConstToLet2.ts
@@ -0,0 +1,12 @@
+///
+
+////const a = 1, b = 1;
+////b = 2;
+
+verify.codeFix({
+ description: "Convert 'const' to 'let'",
+ index: 0,
+ newFileContent:
+`let a = 1, b = 1;
+b = 2;`
+});
diff --git a/tests/cases/fourslash/codeFixConstToLet3.ts b/tests/cases/fourslash/codeFixConstToLet3.ts
new file mode 100644
index 00000000000..83887c0d8e4
--- /dev/null
+++ b/tests/cases/fourslash/codeFixConstToLet3.ts
@@ -0,0 +1,9 @@
+///
+
+////for (const i = 0; i < 10; i++) {}
+
+verify.codeFix({
+ description: "Convert 'const' to 'let'",
+ index: 0,
+ newFileContent: `for (let i = 0; i < 10; i++) {}`
+});