1/* @internal */ 2namespace ts.codefix { 3 const fixId = "fixExpectedComma"; 4 const expectedErrorCode = Diagnostics._0_expected.code; 5 const errorCodes = [expectedErrorCode]; 6 7 registerCodeFix({ 8 errorCodes, 9 getCodeActions(context) { 10 const { sourceFile } = context; 11 const info = getInfo(sourceFile, context.span.start, context.errorCode); 12 if (!info) return undefined; 13 14 const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, info)); 15 16 return [createCodeFixAction( 17 fixId, 18 changes, 19 [Diagnostics.Change_0_to_1, ";", ","], 20 fixId, 21 [Diagnostics.Change_0_to_1, ";", ","] 22 )]; 23 }, 24 fixIds: [fixId], 25 getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => { 26 const info = getInfo(diag.file, diag.start, diag.code); 27 if (info) doChange(changes, context.sourceFile, info); 28 }), 29 }); 30 31 interface Info { readonly node: Node; } 32 33 function getInfo(sourceFile: SourceFile, pos: number, _: number): Info | undefined { 34 const node = getTokenAtPosition(sourceFile, pos); 35 36 return (node.kind === SyntaxKind.SemicolonToken && 37 node.parent && 38 (isObjectLiteralExpression(node.parent) || 39 isArrayLiteralExpression(node.parent))) ? { node } : undefined; 40 } 41 42 function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, { node }: Info): void { 43 const newNode = factory.createToken(SyntaxKind.CommaToken); 44 changes.replaceNode(sourceFile, node, newNode); 45 } 46} 47