-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Expand file tree
/
Copy pathfixExpectedComma.ts
More file actions
63 lines (55 loc) · 1.91 KB
/
fixExpectedComma.ts
File metadata and controls
63 lines (55 loc) · 1.91 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import {
codeFixAll,
createCodeFixAction,
registerCodeFix,
} from "../_namespaces/ts.codefix.js";
import {
Diagnostics,
factory,
getTokenAtPosition,
isArrayLiteralExpression,
isObjectLiteralExpression,
Node,
SourceFile,
SyntaxKind,
textChanges,
} from "../_namespaces/ts.js";
const fixId = "fixExpectedComma";
const expectedErrorCode = Diagnostics._0_expected.code;
const errorCodes = [expectedErrorCode];
registerCodeFix({
errorCodes,
getCodeActions(context) {
const { sourceFile } = context;
const info = getInfo(sourceFile, context.span.start, context.errorCode);
if (!info) return undefined;
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, info));
return [createCodeFixAction(
fixId,
changes,
[Diagnostics.Change_0_to_1, ";", ","],
fixId,
[Diagnostics.Change_0_to_1, ";", ","],
)];
},
fixIds: [fixId],
getAllCodeActions: context =>
codeFixAll(context, errorCodes, (changes, diag) => {
const info = getInfo(diag.file, diag.start, diag.code);
if (info) doChange(changes, context.sourceFile, info);
}),
});
interface Info {
readonly node: Node;
}
function getInfo(sourceFile: SourceFile, pos: number, _: number): Info | undefined {
const node = getTokenAtPosition(sourceFile, pos);
return (node.kind === SyntaxKind.SemicolonToken &&
node.parent &&
(isObjectLiteralExpression(node.parent) ||
isArrayLiteralExpression(node.parent))) ? { node } : undefined;
}
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, { node }: Info): void {
const newNode = factory.createToken(SyntaxKind.CommaToken);
changes.replaceNode(sourceFile, node, newNode);
}