Skip to content

Commit 12eb519

Browse files
authoredAug 25, 2022
fix(50435): Duplicate seeming Code Actions for convert const to let (#50442)
* fix(50435): omit fix all in constToLet QF * add FixAll action
1 parent a08b045 commit 12eb519

File tree

5 files changed

+86
-9
lines changed

5 files changed

+86
-9
lines changed
 

‎src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -7064,6 +7064,10 @@
70647064
"category": "Message",
70657065
"code": 95101
70667066
},
7067+
"Convert all 'const' to 'let'": {
7068+
"category": "Message",
7069+
"code": 95102
7070+
},
70677071
"Convert function expression '{0}' to arrow function": {
70687072
"category": "Message",
70697073
"code": 95105

‎src/services/codefixes/convertConstToLet.ts

+32-9
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,51 @@ namespace ts.codefix {
77
errorCodes,
88
getCodeActions: function getCodeActionsToConvertConstToLet(context) {
99
const { sourceFile, span, program } = context;
10-
const range = getConstTokenRange(sourceFile, span.start, program);
11-
if (range === undefined) return;
10+
const info = getInfo(sourceFile, span.start, program);
11+
if (info === undefined) return;
1212

13-
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, range));
14-
return [createCodeFixAction(fixId, changes, Diagnostics.Convert_const_to_let, fixId, Diagnostics.Convert_const_to_let)];
13+
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, info.token));
14+
return [createCodeFixActionMaybeFixAll(fixId, changes, Diagnostics.Convert_const_to_let, fixId, Diagnostics.Convert_all_const_to_let)];
15+
},
16+
getAllCodeActions: context => {
17+
const { program } = context;
18+
const seen = new Map<number, true>();
19+
20+
return createCombinedCodeActions(textChanges.ChangeTracker.with(context, changes => {
21+
eachDiagnostic(context, errorCodes, diag => {
22+
const info = getInfo(diag.file, diag.start, program);
23+
if (info) {
24+
if (addToSeen(seen, getSymbolId(info.symbol))) {
25+
return doChange(changes, diag.file, info.token);
26+
}
27+
}
28+
return undefined;
29+
});
30+
}));
1531
},
1632
fixIds: [fixId]
1733
});
1834

19-
function getConstTokenRange(sourceFile: SourceFile, pos: number, program: Program) {
35+
interface Info {
36+
symbol: Symbol;
37+
token: Token<SyntaxKind.ConstKeyword>;
38+
}
39+
40+
function getInfo(sourceFile: SourceFile, pos: number, program: Program): Info | undefined {
2041
const checker = program.getTypeChecker();
2142
const symbol = checker.getSymbolAtLocation(getTokenAtPosition(sourceFile, pos));
43+
if (symbol === undefined) return;
44+
2245
const declaration = tryCast(symbol?.valueDeclaration?.parent, isVariableDeclarationList);
2346
if (declaration === undefined) return;
2447

25-
const constToken = findChildOfKind(declaration, SyntaxKind.ConstKeyword, sourceFile);
48+
const constToken = findChildOfKind<Token<SyntaxKind.ConstKeyword>>(declaration, SyntaxKind.ConstKeyword, sourceFile);
2649
if (constToken === undefined) return;
2750

28-
return createRange(constToken.pos, constToken.end);
51+
return { symbol, token: constToken };
2952
}
3053

31-
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, range: TextRange) {
32-
changes.replaceRangeWithText(sourceFile, range, "let");
54+
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, token: Token<SyntaxKind.ConstKeyword>) {
55+
changes.replaceNode(sourceFile, token, factory.createToken(SyntaxKind.LetKeyword));
3356
}
3457
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////// Comment
4+
////const a = 1;
5+
////a = 2;
6+
7+
verify.codeFix({
8+
description: "Convert 'const' to 'let'",
9+
index: 0,
10+
newFileContent:
11+
`// Comment
12+
let a = 1;
13+
a = 2;`
14+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////const a = 1;
4+
////a = 2;
5+
////a = 3;
6+
7+
verify.codeFixAll({
8+
fixId: "fixConvertConstToLet",
9+
fixAllDescription: ts.Diagnostics.Convert_all_const_to_let.message,
10+
newFileContent:
11+
`let a = 1;
12+
a = 2;
13+
a = 3;`
14+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////const a = 1;
4+
////a = 2;
5+
////a = 3;
6+
////
7+
////const b = 1;
8+
////b = 2;
9+
////b = 3;
10+
11+
verify.codeFixAll({
12+
fixId: "fixConvertConstToLet",
13+
fixAllDescription: ts.Diagnostics.Convert_all_const_to_let.message,
14+
newFileContent:
15+
`let a = 1;
16+
a = 2;
17+
a = 3;
18+
19+
let b = 1;
20+
b = 2;
21+
b = 3;`
22+
});

0 commit comments

Comments
 (0)
Please sign in to comment.