fix(44676): fix constToLetQuickFix selection range (#44677)

This commit is contained in:
Oleksandr T 2021-06-21 21:22:24 +03:00 committed by GitHub
parent 9e2845227c
commit b72f67f45c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 49 additions and 26 deletions

View File

@ -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");
}
}

View File

@ -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;`
});

View 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;`
});

View 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;`
});

View 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++) {}`
});