-
Notifications
You must be signed in to change notification settings - Fork 119
/
FunctionApplicationPatcher.js
57 lines (50 loc) · 1.75 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
54
55
56
57
import NodePatcher from './../../../patchers/NodePatcher.js';
import type { Editor, Node, ParseContext } from './../../../patchers/types.js';
import { COMMA } from 'coffee-lex';
import { isSemanticToken } from '../../../utils/types.js';
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;
}
initialize() {
this.fn.setRequiresExpression();
this.args.forEach(arg => arg.setRequiresExpression());
}
/**
* Note that we don't need to worry about implicit function applications,
* since the normalize stage would have already added parens.
*/
patchAsExpression() {
let { args, outerEndTokenIndex } = this;
this.fn.patch();
args.forEach((arg, i) => {
arg.patch();
let isLast = i === args.length - 1;
let commaTokenIndex = arg.node.virtual ? null : this.indexOfSourceTokenAfterSourceTokenIndex(
arg.outerEndTokenIndex,
COMMA,
isSemanticToken
);
// Ignore commas after the end of the function call.
if (commaTokenIndex && commaTokenIndex.compare(outerEndTokenIndex) <= 0) {
commaTokenIndex = null;
}
let commaToken = commaTokenIndex && this.sourceTokenAtIndex(commaTokenIndex);
if (isLast && commaToken) {
this.remove(arg.outerEnd, commaToken.end);
} else if (!isLast && !commaToken) {
this.insert(arg.outerEnd, ',');
}
});
}
/**
* Probably can't happen, but just for completeness.
*/
statementNeedsParens(): boolean {
return this.fn.statementShouldAddParens();
}
}