Skip to content

Commit b72f67f

Browse files
authored
fix(44676): fix constToLetQuickFix selection range (#44677)
1 parent 9e28452 commit b72f67f

File tree

5 files changed

+49
-26
lines changed

5 files changed

+49
-26
lines changed

src/services/codefixes/convertConstToLet.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,28 @@ namespace ts.codefix {
77
errorCodes,
88
getCodeActions: context => {
99
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));
10+
const range = getConstTokenRange(sourceFile, span.start, program);
11+
if (range === undefined) return;
12+
13+
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, range));
1214
return [createCodeFixAction(fixId, changes, Diagnostics.Convert_const_to_let, fixId, Diagnostics.Convert_const_to_let)];
1315
},
1416
fixIds: [fixId]
1517
});
1618

17-
function getVariableStatement(sourceFile: SourceFile, pos: number, program: Program) {
18-
const token = getTokenAtPosition(sourceFile, pos);
19+
function getConstTokenRange(sourceFile: SourceFile, pos: number, program: Program) {
1920
const checker = program.getTypeChecker();
20-
const symbol = checker.getSymbolAtLocation(token);
21-
if (symbol?.valueDeclaration) {
22-
return symbol.valueDeclaration.parent.parent as VariableStatement;
23-
}
21+
const symbol = checker.getSymbolAtLocation(getTokenAtPosition(sourceFile, pos));
22+
const declaration = tryCast(symbol?.valueDeclaration?.parent, isVariableDeclarationList);
23+
if (declaration === undefined) return;
24+
25+
const constToken = findChildOfKind(declaration, SyntaxKind.ConstKeyword, sourceFile);
26+
if (constToken === undefined) return;
27+
28+
return createRange(constToken.pos, constToken.end);
2429
}
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");
30+
31+
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, range: TextRange) {
32+
changes.replaceRangeWithText(sourceFile, range, "let");
3133
}
3234
}

tests/cases/fourslash/codeFixConstToLet.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.
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+
});
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 a = 1, b = 1;
4+
////b = 2;
5+
6+
verify.codeFix({
7+
description: "Convert 'const' to 'let'",
8+
index: 0,
9+
newFileContent:
10+
`let a = 1, b = 1;
11+
b = 2;`
12+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////for (const i = 0; i < 10; i++) {}
4+
5+
verify.codeFix({
6+
description: "Convert 'const' to 'let'",
7+
index: 0,
8+
newFileContent: `for (let i = 0; i < 10; i++) {}`
9+
});

0 commit comments

Comments
 (0)