mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-04 21:53:42 -06:00
fix(50435): Duplicate seeming Code Actions for convert const to let (#50442)
* fix(50435): omit fix all in constToLet QF * add FixAll action
This commit is contained in:
parent
a08b045d2b
commit
12eb519b3f
@ -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
|
||||
|
||||
@ -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
tests/cases/fourslash/codeFixConstToLet4.ts
Normal file
14
tests/cases/fourslash/codeFixConstToLet4.ts
Normal file
@ -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
tests/cases/fourslash/codeFixConstToLet_all1.ts
Normal file
14
tests/cases/fourslash/codeFixConstToLet_all1.ts
Normal file
@ -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
tests/cases/fourslash/codeFixConstToLet_all2.ts
Normal file
22
tests/cases/fourslash/codeFixConstToLet_all2.ts
Normal file
@ -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;`
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user