-
Notifications
You must be signed in to change notification settings - Fork 12.8k
/
Copy pathfixPropertyAssignment.ts
41 lines (37 loc) · 1.62 KB
/
fixPropertyAssignment.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import {
codeFixAll,
createCodeFixAction,
registerCodeFix,
} from "../_namespaces/ts.codefix.js";
import {
cast,
Diagnostics,
Expression,
factory,
getTokenAtPosition,
isShorthandPropertyAssignment,
ShorthandPropertyAssignment,
SourceFile,
textChanges,
} from "../_namespaces/ts.js";
const fixId = "fixPropertyAssignment";
const errorCodes = [
Diagnostics.Did_you_mean_to_use_a_Colon_An_can_only_follow_a_property_name_when_the_containing_object_literal_is_part_of_a_destructuring_pattern.code,
];
registerCodeFix({
errorCodes,
fixIds: [fixId],
getCodeActions(context) {
const { sourceFile, span } = context;
const property = getProperty(sourceFile, span.start);
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, context.sourceFile, property));
return [createCodeFixAction(fixId, changes, [Diagnostics.Change_0_to_1, "=", ":"], fixId, [Diagnostics.Switch_each_misused_0_to_1, "=", ":"])];
},
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => doChange(changes, diag.file, getProperty(diag.file, diag.start))),
});
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, node: ShorthandPropertyAssignment): void {
changes.replaceNode(sourceFile, node, factory.createPropertyAssignment(node.name, node.objectAssignmentInitializer as Expression));
}
function getProperty(sourceFile: SourceFile, pos: number): ShorthandPropertyAssignment {
return cast(getTokenAtPosition(sourceFile, pos).parent, isShorthandPropertyAssignment);
}