-
Notifications
You must be signed in to change notification settings - Fork 12.8k
/
Copy pathfixMissingCallParentheses.ts
60 lines (53 loc) · 1.99 KB
/
fixMissingCallParentheses.ts
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import {
codeFixAll,
createCodeFixAction,
registerCodeFix,
} from "../_namespaces/ts.codefix.js";
import {
Diagnostics,
getTokenAtPosition,
Identifier,
isIdentifier,
isPropertyAccessExpression,
PrivateIdentifier,
PropertyAccessExpression,
SourceFile,
textChanges,
} from "../_namespaces/ts.js";
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,
];
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;
}