|
| 1 | +/* @internal */ |
| 2 | +namespace ts.codefix { |
| 3 | + const fixId = "addMissingNewOperator"; |
| 4 | + const errorCodes = [Diagnostics.Value_of_type_0_is_not_callable_Did_you_mean_to_include_new.code]; |
| 5 | + registerCodeFix({ |
| 6 | + errorCodes, |
| 7 | + getCodeActions(context) { |
| 8 | + const { sourceFile, span } = context; |
| 9 | + const changes = textChanges.ChangeTracker.with(context, t => addMissingNewOperator(t, sourceFile, span)); |
| 10 | + return [createCodeFixAction(fixId, changes, Diagnostics.Add_missing_new_operator_to_call, fixId, Diagnostics.Add_missing_new_operator_to_all_calls)]; |
| 11 | + }, |
| 12 | + fixIds: [fixId], |
| 13 | + getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => |
| 14 | + addMissingNewOperator(changes, context.sourceFile, diag)), |
| 15 | + }); |
| 16 | + |
| 17 | + function addMissingNewOperator(changes: textChanges.ChangeTracker, sourceFile: SourceFile, span: TextSpan): void { |
| 18 | + const call = cast(findAncestorMatchingSpan(sourceFile, span), isCallExpression); |
| 19 | + const newExpression = createNew(call.expression, call.typeArguments, call.arguments); |
| 20 | + |
| 21 | + changes.replaceNode(sourceFile, call, newExpression); |
| 22 | + } |
| 23 | + |
| 24 | + function findAncestorMatchingSpan(sourceFile: SourceFile, span: TextSpan): Node { |
| 25 | + let token = getTokenAtPosition(sourceFile, span.start); |
| 26 | + const end = textSpanEnd(span); |
| 27 | + while (token.end < end) { |
| 28 | + token = token.parent; |
| 29 | + } |
| 30 | + return token; |
| 31 | + } |
| 32 | +} |
0 commit comments