mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-06 02:33:53 -06:00
fix(44676): fix constToLetQuickFix selection range (#44677)
This commit is contained in:
parent
9e2845227c
commit
b72f67f45c
@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,12 +0,0 @@
|
||||
/// <reference path='fourslash.ts' />
|
||||
|
||||
//// const x = 42;
|
||||
//// x = 75;
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert 'const' to 'let'",
|
||||
index: 0,
|
||||
newFileContent:
|
||||
`let x = 42;
|
||||
x = 75;`
|
||||
});
|
||||
12
tests/cases/fourslash/codeFixConstToLet1.ts
Normal file
12
tests/cases/fourslash/codeFixConstToLet1.ts
Normal file
@ -0,0 +1,12 @@
|
||||
/// <reference path='fourslash.ts' />
|
||||
|
||||
////const x = 42;
|
||||
////x = 75;
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert 'const' to 'let'",
|
||||
index: 0,
|
||||
newFileContent:
|
||||
`let x = 42;
|
||||
x = 75;`
|
||||
});
|
||||
12
tests/cases/fourslash/codeFixConstToLet2.ts
Normal file
12
tests/cases/fourslash/codeFixConstToLet2.ts
Normal file
@ -0,0 +1,12 @@
|
||||
/// <reference path='fourslash.ts' />
|
||||
|
||||
////const a = 1, b = 1;
|
||||
////b = 2;
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert 'const' to 'let'",
|
||||
index: 0,
|
||||
newFileContent:
|
||||
`let a = 1, b = 1;
|
||||
b = 2;`
|
||||
});
|
||||
9
tests/cases/fourslash/codeFixConstToLet3.ts
Normal file
9
tests/cases/fourslash/codeFixConstToLet3.ts
Normal file
@ -0,0 +1,9 @@
|
||||
/// <reference path='fourslash.ts' />
|
||||
|
||||
////for (const i = 0; i < 10; i++) {}
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert 'const' to 'let'",
|
||||
index: 0,
|
||||
newFileContent: `for (let i = 0; i < 10; i++) {}`
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user