-
Notifications
You must be signed in to change notification settings - Fork 119
/
FunctionApplicationPatcher.js
53 lines (44 loc) · 1.47 KB
/
FunctionApplicationPatcher.js
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
import NodePatcher from './../../../patchers/NodePatcher.js';
import type { Editor, Node, ParseContext } from './../../../patchers/types.js';
import { CALL_START } from 'coffee-lex';
export default class FunctionApplicationPatcher extends NodePatcher {
fn: NodePatcher;
args: Array<NodePatcher>;
constructor(node: Node, context: ParseContext, editor: Editor, fn: NodePatcher, args: Array<NodePatcher>) {
super(node, context, editor);
this.fn = fn;
this.args = args;
}
patchAsExpression() {
let implicitCall = this.isImplicitCall();
let { args } = this;
this.fn.patch();
if (implicitCall && args.length === 0) {
this.insert(this.fn.outerEnd, '()');
return;
}
if (implicitCall) {
let firstArg = args[0];
let hasOneArg = args.length === 1;
let firstArgIsOnNextLine = !firstArg ? false :
/[\r\n]/.test(this.context.source.slice(this.fn.outerEnd, firstArg.outerStart));
if ((hasOneArg && firstArg.node.virtual) || firstArgIsOnNextLine) {
this.insert(this.fn.outerEnd, '(');
} else {
this.overwrite(this.fn.outerEnd, firstArg.outerStart, '(');
}
}
args.forEach(arg => arg.patch());
if (implicitCall) {
let lastArg = args[args.length - 1];
if (lastArg.isMultiline()) {
this.appendLineAfter(')');
} else {
this.insert(this.innerEnd, ')');
}
}
}
isImplicitCall() {
return !this.fn.hasSourceTokenAfter(CALL_START);
}
}