-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathCallExpression.js
131 lines (106 loc) · 3.56 KB
/
CallExpression.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"use strict";
var _tk = require('rocambole-token');
var _br = require('rocambole-linebreak');
var _ws = require('rocambole-whitespace');
var _limit = require('../limit');
var _parens = require('./expressionParentheses');
exports.format = function CallExpression(node) {
// parentheses that surrounds the arguments eg. `foo()` and `new Foo(1, 2)`
var openingParentheses = getOpeningParentheses(node);
var closingParentheses = node.endToken;
var hasParentheses = openingParentheses && closingParentheses.value === ')';
// NewExpression is almost the same as CallExpression, simpler to keep it here
if (node.type === 'NewExpression') {
_br.limitAfter(node.startToken, 0);
_ws.limitAfter(node.startToken, 1);
}
if (hasParentheses) {
_limit.around(openingParentheses, 'CallExpressionOpeningParentheses');
_limit.around(closingParentheses, 'CallExpressionClosingParentheses');
}
// this is for cases like `new (Foo || Bar)()` and `(lorem || ipsum)()`
var calleeParens = _parens.getParentheses(node.callee);
if (calleeParens) {
_limit.after(calleeParens.opening, 'CalleeOpeningParentheses');
_limit.before(calleeParens.closing, 'CalleeClosingParentheses');
}
var args = node['arguments'];
if (args.length) {
_limit.before(_tk.findNextNonEmpty(openingParentheses), 'ArgumentList');
args.forEach(function(arg) {
var next = _tk.findInBetween(arg.endToken, closingParentheses, ',');
if (next && next.value === ',') {
_limit.around(next, 'ArgumentComma');
}
});
_limit.after(_tk.findPrevNonEmpty(closingParentheses), 'ArgumentList');
} else if (hasParentheses) {
_limit.after(openingParentheses, 0);
_limit.before(closingParentheses, 0);
}
// iife
if (node.callee.type !== 'FunctionExpression') {
return;
}
var parens = _parens.getParentheses({
type: 'Special',
startToken: node.startToken,
endToken: node.endToken
});
if (parens) {
_limit.after(parens.opening, 'IIFEOpeningParentheses');
_limit.before(parens.closing, 'IIFEClosingParentheses');
}
};
function getOpeningParentheses(node) {
return _tk.findInBetween(node.callee.endToken, node.endToken, '(');
}
exports.getIndentEdges = function(node, opts) {
// indent in between "new" and "ConstructorName"
var edges = [{
startToken: node.startToken,
endToken: node.callee.startToken.next
}];
var openingParentheses = getOpeningParentheses(node);
if (!openingParentheses || openingParentheses.value !== '(') {
return edges;
}
if (!node.arguments.length) {
// it might contain comments inside even tho there are no args
edges.push({
startToken: openingParentheses,
endToken: node.endToken
});
return edges;
}
var start;
function hasBr(start, end) {
return _tk.findInBetween(start, end, _tk.isBr);
}
// indent start based on first argument that starts on a new line
node.arguments.some(function(arg, i, args) {
var prev = i ? args[i - 1].endToken.next : openingParentheses;
if (hasBr(prev, arg.startToken)) {
start = prev;
return true;
}
});
if (!start) {
// we handle BinaryExpressions here because multiple operations are grouped
// inside the same root node, and we need to indent if it breaks lines
node.arguments.some(function(arg) {
if (opts['CallExpression.' + arg.type] &&
hasBr(arg.startToken, arg.endToken)) {
start = arg.startToken.next;
return true;
}
});
}
if (start) {
edges.push({
startToken: start,
endToken: node.endToken
});
}
return edges;
};