forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfixMissingCallParentheses.ts
More file actions
46 lines (40 loc) · 1.9 KB
/
fixMissingCallParentheses.ts
File metadata and controls
46 lines (40 loc) · 1.9 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 = "fixMissingCallParentheses";
const errorCodes = [
Diagnostics.This_condition_will_always_return_true_since_this_function_is_always_defined_Did_you_mean_to_call_it_instead.code,
Diagnostics.This_expression_refers_to_function_Did_you_mean_to_call_it_instead.code,
];
registerCodeFix({
errorCodes,
fixIds: [fixId],
getCodeActions(context) {
const { sourceFile, span } = context;
const callName = getCallName(sourceFile, span.start);
if (!callName) return;
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, context.sourceFile, callName));
return [createCodeFixAction(fixId, changes, Diagnostics.Add_missing_call_parentheses, fixId, Diagnostics.Add_all_missing_call_parentheses)];
},
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => {
const callName = getCallName(diag.file, diag.start);
if (callName) doChange(changes, diag.file, callName);
})
});
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, name: Identifier | PrivateIdentifier): void {
changes.replaceNodeWithText(sourceFile, name, `${ name.text }()`);
}
function getCallName(sourceFile: SourceFile, start: number): Identifier | PrivateIdentifier | undefined {
const token = getTokenAtPosition(sourceFile, start);
if (isPropertyAccessExpression(token.parent)) {
let current: PropertyAccessExpression = token.parent;
while (isPropertyAccessExpression(current.parent)) {
current = current.parent;
}
return current.name;
}
if (isIdentifier(token)) {
return token;
}
return undefined;
}
}