|
| 1 | +/* @internal */ |
| 2 | +namespace ts.codefix { |
| 3 | + const fixId = "fixMissingCallParentheses"; |
| 4 | + const errorCodes = [ |
| 5 | + Diagnostics.This_condition_will_always_return_true_since_the_function_is_always_defined_Did_you_mean_to_call_it_instead.code, |
| 6 | + ]; |
| 7 | + |
| 8 | + registerCodeFix({ |
| 9 | + errorCodes, |
| 10 | + fixIds: [fixId], |
| 11 | + getCodeActions(context) { |
| 12 | + const { sourceFile, span } = context; |
| 13 | + const callName = getCallName(sourceFile, span.start); |
| 14 | + if (!callName) return; |
| 15 | + |
| 16 | + const changes = textChanges.ChangeTracker.with(context, t => doChange(t, context.sourceFile, callName)); |
| 17 | + return [createCodeFixAction(fixId, changes, Diagnostics.Add_missing_call_parentheses, fixId, Diagnostics.Add_all_missing_call_parentheses)]; |
| 18 | + }, |
| 19 | + getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => { |
| 20 | + const callName = getCallName(diag.file, diag.start); |
| 21 | + if (callName) doChange(changes, diag.file, callName); |
| 22 | + }) |
| 23 | + }); |
| 24 | + |
| 25 | + function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, name: Identifier | PrivateIdentifier): void { |
| 26 | + changes.replaceNodeWithText(sourceFile, name, `${ name.text }()`); |
| 27 | + } |
| 28 | + |
| 29 | + function getCallName(sourceFile: SourceFile, start: number): Identifier | PrivateIdentifier | undefined { |
| 30 | + const token = getTokenAtPosition(sourceFile, start); |
| 31 | + if (isPropertyAccessExpression(token.parent)) { |
| 32 | + let current: PropertyAccessExpression = token.parent; |
| 33 | + while (isPropertyAccessExpression(current.parent)) { |
| 34 | + current = current.parent; |
| 35 | + } |
| 36 | + return current.name; |
| 37 | + } |
| 38 | + |
| 39 | + if (isIdentifier(token)) { |
| 40 | + return token; |
| 41 | + } |
| 42 | + |
| 43 | + return undefined; |
| 44 | + } |
| 45 | +} |
0 commit comments