-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathIfStatement.js
126 lines (101 loc) · 3.72 KB
/
IfStatement.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
"use strict";
var _br = require('rocambole-linebreak');
var _tk = require('rocambole-token');
var _ws = require('rocambole-whitespace');
var _limit = require('../limit');
exports.format = function IfStatement(node) {
var conditionalStart = _tk.findPrev(node.test.startToken, '(');
var conditionalEnd = _tk.findNext(node.test.endToken, ')');
_ws.limit(conditionalStart, 'IfStatementConditionalOpening');
_ws.limit(conditionalEnd, 'IfStatementConditionalClosing');
var ifStatIsBlock = node.consequent.type === 'BlockStatement';
var alt = node.alternate;
if (alt) {
var elseKeyword = _tk.findPrev(alt.startToken, 'else');
if (alt.type === 'IfStatement') {
// ElseIfStatement
// space between "else if" (`alt.startToken` is "if")
_br.limitBefore(alt.startToken, 0);
_ws.limitBefore(alt.startToken, 1);
if (alt.consequent.type === 'BlockStatement') {
_br.limitBefore(alt.consequent.startToken, 'ElseIfStatementOpeningBrace');
_br.limitBefore(alt.consequent.endToken, 'ElseIfStatementClosingBrace');
}
// only remove line break before "else" if previous char is "}"; white
// space is handled by "whiteSpace.after.IfStatementClosingBrace"
_br.limitBefore(
elseKeyword,
ifStatIsBlock ?
'ElseIfStatement' :
1
);
if (!alt.alternate) {
// we only limit the line breaks after the ElseIfStatement if it is not
// followed by an ElseStatement, otherwise it would add line breaks
// that it shouldn't
_br.limitAfter(alt.consequent.endToken, 'ElseIfStatement');
}
} else if (alt.type === 'BlockStatement') {
// ElseStatement
_limit.around(alt.startToken, 'ElseStatementOpeningBrace');
_br.limitBefore(elseKeyword, ifStatIsBlock ? 'ElseStatement' : 1);
_br.limitAfter(alt.endToken, 'ElseStatement');
_limit.around(alt.endToken, 'ElseStatementClosingBrace');
} else {
// ElseStatement without curly braces
_ws.limitAfter(elseKeyword, 1);
}
}
var startBody = node.consequent.startToken;
var endBody = node.consequent.endToken;
if (ifStatIsBlock) {
_limit.around(startBody, 'IfStatementOpeningBrace');
if (!alt) {
_br.limit(endBody, 'IfStatementClosingBrace');
} else {
_br.limitBefore(endBody, 'IfStatementClosingBrace');
}
_ws.limit(endBody, 'IfStatementClosingBrace');
}
};
exports.getIndentEdges = function(node, opts) {
var edges = [];
var test = node.test;
var consequent = node.consequent;
var alt = node.alternate;
// test (IfStatementConditional)
edges.push({
level: opts.IfStatementConditional,
startToken: _tk.findNext(node.startToken, '('),
endToken: _tk.findPrev(consequent.startToken, ')'),
});
function isExecutable(token) {
return _tk.isNotEmpty(token) && !_tk.isComment(token);
}
// consequent (body)
edges.push({
startToken: (consequent.type === 'BlockStatement' ?
consequent.startToken :
test.endToken.next
),
// we have some special rules for comments just before the `else` statement
// because of jQuery style guide. maybe in the future we will add
// a setting to toggle this behavior (if someone asks for it)
endToken: (alt && _tk.isComment(_tk.findPrevNonEmpty(consequent.endToken)) ?
_tk.findPrev(consequent.endToken, isExecutable).next :
consequent.endToken
)
});
// alt (else)
if (alt && alt.type !== 'IfStatement') {
// it the alternate is IfStatement it will already take care of indentation
edges.push({
startToken: (alt.type === 'BlockStatement' ?
alt.startToken :
_tk.findPrevNonEmpty(alt.startToken).next
),
endToken: alt.endToken
});
}
return edges;
};