Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(50435): Duplicate seeming Code Actions for convert const to let #50442

Merged
merged 2 commits into from Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Expand Up @@ -7064,6 +7064,10 @@
"category": "Message",
"code": 95101
},
"Convert all 'const' to 'let'": {
"category": "Message",
"code": 95102
},
"Convert function expression '{0}' to arrow function": {
"category": "Message",
"code": 95105
Expand Down
41 changes: 32 additions & 9 deletions src/services/codefixes/convertConstToLet.ts
Expand Up @@ -7,28 +7,51 @@ namespace ts.codefix {
errorCodes,
getCodeActions: function getCodeActionsToConvertConstToLet(context) {
const { sourceFile, span, program } = context;
const range = getConstTokenRange(sourceFile, span.start, program);
if (range === undefined) return;
const info = getInfo(sourceFile, span.start, program);
if (info === 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)];
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, info.token));
return [createCodeFixActionMaybeFixAll(fixId, changes, Diagnostics.Convert_const_to_let, fixId, Diagnostics.Convert_all_const_to_let)];
},
getAllCodeActions: context => {
const { program } = context;
const seen = new Map<number, true>();

return createCombinedCodeActions(textChanges.ChangeTracker.with(context, changes => {
eachDiagnostic(context, errorCodes, diag => {
const info = getInfo(diag.file, diag.start, program);
if (info) {
if (addToSeen(seen, getSymbolId(info.symbol))) {
return doChange(changes, diag.file, info.token);
}
}
return undefined;
});
}));
},
fixIds: [fixId]
});

function getConstTokenRange(sourceFile: SourceFile, pos: number, program: Program) {
interface Info {
symbol: Symbol;
token: Token<SyntaxKind.ConstKeyword>;
}

function getInfo(sourceFile: SourceFile, pos: number, program: Program): Info | undefined {
const checker = program.getTypeChecker();
const symbol = checker.getSymbolAtLocation(getTokenAtPosition(sourceFile, pos));
if (symbol === undefined) return;

const declaration = tryCast(symbol?.valueDeclaration?.parent, isVariableDeclarationList);
if (declaration === undefined) return;

const constToken = findChildOfKind(declaration, SyntaxKind.ConstKeyword, sourceFile);
const constToken = findChildOfKind<Token<SyntaxKind.ConstKeyword>>(declaration, SyntaxKind.ConstKeyword, sourceFile);
if (constToken === undefined) return;

return createRange(constToken.pos, constToken.end);
return { symbol, token: constToken };
}

function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, range: TextRange) {
changes.replaceRangeWithText(sourceFile, range, "let");
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, token: Token<SyntaxKind.ConstKeyword>) {
changes.replaceNode(sourceFile, token, factory.createToken(SyntaxKind.LetKeyword));
}
}
14 changes: 14 additions & 0 deletions tests/cases/fourslash/codeFixConstToLet4.ts
@@ -0,0 +1,14 @@
/// <reference path='fourslash.ts' />

////// Comment
////const a = 1;
////a = 2;

verify.codeFix({
description: "Convert 'const' to 'let'",
index: 0,
newFileContent:
`// Comment
let a = 1;
a = 2;`
});
14 changes: 14 additions & 0 deletions tests/cases/fourslash/codeFixConstToLet_all1.ts
@@ -0,0 +1,14 @@
/// <reference path="fourslash.ts" />

////const a = 1;
////a = 2;
////a = 3;

verify.codeFixAll({
fixId: "fixConvertConstToLet",
fixAllDescription: ts.Diagnostics.Convert_all_const_to_let.message,
newFileContent:
`let a = 1;
a = 2;
a = 3;`
});
22 changes: 22 additions & 0 deletions tests/cases/fourslash/codeFixConstToLet_all2.ts
@@ -0,0 +1,22 @@
/// <reference path="fourslash.ts" />

////const a = 1;
////a = 2;
////a = 3;
////
////const b = 1;
////b = 2;
////b = 3;

verify.codeFixAll({
fixId: "fixConvertConstToLet",
fixAllDescription: ts.Diagnostics.Convert_all_const_to_let.message,
newFileContent:
`let a = 1;
a = 2;
a = 3;

let b = 1;
b = 2;
b = 3;`
});