forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixExpectedComma.ts
More file actions
46 lines (39 loc) · 1.73 KB
/
fixExpectedComma.ts
File metadata and controls
46 lines (39 loc) · 1.73 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
/* @internal */
namespace ts.codefix {
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);
}
}