-
Notifications
You must be signed in to change notification settings - Fork 527
/
Copy pathbrace-style.js
338 lines (300 loc) · 10.3 KB
/
brace-style.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
'use strict';
var helpers = require('../helpers');
/**
* Get the current block within a node
*
* @param {Object} node - The node containing our desired block
* @returns {Object} The current block of the node
*/
var getCurrentNode = function (node) {
return node.contains('block') ? node.first('block') : false;
};
/**
* Get the previous node
*
* @param {Object} node - Our current node
* @returns {Object|bool} The previous node or false if not found
*/
var getPreviousNode = function (node) {
// Rulesets
if (node.is('ruleset')) {
return node.contains('selector') ? node.last('selector') : false;
}
// Conditonal statements
if (node.is('conditionalStatement')) {
var previousParent = node.contains('condition') ? node.last('condition') : false;
return previousParent && previousParent.contains('atkeyword') ? previousParent.last('atkeyword') : false;
}
// Loops
if (node.is('loop')) {
return node.contains('atkeyword') ? node.last('atkeyword') : false;
}
// Mixins and atrules (functions etc)
if (node.is('mixin') || node.is('atrule')) {
if (node.contains('function')) {
return node.last('function');
}
if (node.contains('arguments')) {
return node.last('arguments');
}
return node.contains('atkeyword') ? node.last('atkeyword') : false;
}
return false;
};
/**
* Determine if current node is an exception and end checks if it is
* If we've picked up a return @rule ignore it
*
* @param {Object} node - The original node
* @param {object} currentNode - The current node block
* @param {Object} previousNode - The node previous to our current node
* @returns {bool} Whether or not the it is an exception
*/
var isException = function (node, currentNode, previousNode) {
if (node.is('atrule')) {
if (previousNode.contains('ident') && previousNode.first('ident').content === 'return') {
return true;
}
}
return false;
};
/**
* Determine if statement is a single line statement
*
* @param {Object} node - The statement to check
* @returns {bool} True or false
*/
var isSingleLineStatement = function (node) {
return node.start.line === node.end.line;
};
/**
* Determine if opening brace of statement is on a new line
*
* @param {Object} nodeA - The previous block
* @param {Object} nodeB - The current block
* @returns {bool} True or false
*/
var isOpeningBraceOnNewLine = function (nodeA, nodeB) {
return nodeA.end.line === nodeB.start.line;
};
/**
* Determine if closing brace of statement is on new line
*
* @param {Object} node - The current block
* @returns {bool|null} True or false if relevant else null
*/
var isClosingBraceOnNewLine = function (node) {
if (node.contains('block')) {
var content = node.first('block'),
contentLength = content.length - 1,
lastNode = content.get(contentLength);
if (lastNode && lastNode.is('space') && helpers.hasEOL(lastNode.content)) {
return true;
}
return false;
}
return null;
};
/**
* Determine if condition starts on a new line by checking the leading node for
* an end-of-line
*
* @param {Object} node - The node that is our condition
* @param {Object} parentNode - The condition node's parent
* @param {Number} j - The index of our node in the context of the parent's children
* @returns {bool|null} True or false if relevant else null
*/
var isConditionOnNewLine = function (node, parentNode, j) {
var currentNode = node;
// Check node is part of an `else if` and if it is, use the else node instead
if (node.first('ident').content === 'if') {
var initialNode = parentNode.get(j);
currentNode = initialNode.contains('atkeyword') ? initialNode.first('atkeyword') : false;
}
// Only check if it's an @else condition
if (currentNode && currentNode.contains('ident') && currentNode.first('ident').content === 'else') {
// Reverse back up tree
var previousChild = parentNode.get(--j) || false;
if (previousChild) {
// Determine if we have a leading new line
if (previousChild.is('space') && helpers.hasEOL(previousChild.content)) {
return true;
}
return false;
}
return false;
}
return null;
};
/**
* Run the rule checks and return their results
*
* @param {Object} node - The original node
* @param {Object} currentNode - The current node block
* @param {Object} previousNode - The node previous to our current node
* @param {Object} parentNode - The parent of the original node
* @param {int} index - The index of the original node
* @returns {Object} The results of the rule checks
*/
var runRuleChecks = function (node, currentNode, previousNode, parentNode, index) {
var checks = {};
// Determine if single line statement
checks.singleLineStatement = isSingleLineStatement(node);
// Determine if condition is on a new line
if (node.is('atrule') || node.is('conditionalStatement')) {
checks.conditionOnNewLine = isConditionOnNewLine(previousNode, parentNode, index);
}
// Determine if opening brace is on new line
if (previousNode && currentNode) {
checks.openingBraceOnNewLine = isOpeningBraceOnNewLine(previousNode, currentNode);
}
// Determine if closing brace is on new line
checks.closingBraceOnNewLine = isClosingBraceOnNewLine(node);
return checks;
};
/**
* Filter at-rules
*
* @param {Object} node - The node to filter
* @param {Array} accepted - The array of accepted at-rule types
* @returns {bool} true if we should ignore, false to continue
*/
var filterAtrule = function (node, accepted) {
if (node.is('atrule')) {
if (node.contains('atkeyword') && node.first('atkeyword').contains('ident')) {
if (accepted.indexOf(node.first('atkeyword').first('ident').content) === -1) {
return true;
}
}
}
return false;
};
/**
* Create an issue using the supplied information
*
* @param {Object} parser - The parser
* @param {Object} node - The node with the issue
* @param {string} message - The message to display
* @returns {Object} An object containing an issue
*/
var createIssue = function (parser, node, message) {
return {
'ruleId': parser.rule.name,
'line': node.line,
'column': node.column,
'message': message,
'severity': parser.severity
};
};
module.exports = {
'name': 'brace-style',
'defaults': {
'style': '1tbs',
'allow-single-line': true
},
'detect': function (ast, parser) {
var result = [],
acceptedAtrules = [
'function',
'if',
'else'
];
ast.traverseByTypes(['conditionalStatement', 'atrule', 'ruleset', 'mixin', 'loop'], function (node, i, parent) {
var currentNode = false,
previousNode = false,
checks = {
singleLineStatement: null,
openingBraceOnNewLine: null,
closingBraceOnNewLine: null,
conditionOnNewLine: null
},
messages = [
'Single line statements are not allowed',
'Opening brace must be on the same line as condition',
'Brace must be on a new line',
'Statement must start on the same line as the closing brace of the previous statement',
'Statement must begin on a new line',
'Closing brace must be on a new line'
];
// SCSS syntax only rule
if (ast.syntax === 'sass') {
return false;
}
// Filter at-rule types
if (filterAtrule(node, acceptedAtrules)) {
return false;
}
// Assign current & previous nodes based on node type
currentNode = getCurrentNode(node);
previousNode = getPreviousNode(node);
// If not an exception carry on
if (!isException(node, currentNode, previousNode)) {
// Run and store rule check results
checks = runRuleChecks(node, currentNode, previousNode, parent, i);
// Build single-line statement results
if (checks.singleLineStatement === false && checks.closingBraceOnNewLine === false) {
result = helpers.addUnique(result, createIssue(parser, {
line: currentNode.end.line,
column: currentNode.end.column
}, messages[5]));
}
if (checks.singleLineStatement === true) {
if (parser.options['allow-single-line'] === false) {
result = helpers.addUnique(result, createIssue(parser, {
line: node.start.line,
column: node.start.column
}, messages[0]));
}
return false;
}
// Build brace-style results
if (previousNode && currentNode) {
if (parser.options.style === '1tbs') {
if (checks.openingBraceOnNewLine === false) {
result = helpers.addUnique(result, createIssue(parser, {
line: currentNode.start.line,
column: currentNode.start.column
}, messages[1]));
}
if (checks.conditionOnNewLine === true) {
result = helpers.addUnique(result, createIssue(parser, {
line: previousNode.start.line,
column: previousNode.start.column
}, messages[3]));
}
}
if (parser.options.style === 'stroustrup') {
if (checks.openingBraceOnNewLine === false) {
result = helpers.addUnique(result, createIssue(parser, {
line: currentNode.start.line,
column: currentNode.start.column
}, messages[1]));
}
if (checks.conditionOnNewLine === false) {
result = helpers.addUnique(result, createIssue(parser, {
line: previousNode.start.line,
column: previousNode.start.column
}, messages[4]));
}
}
if (parser.options.style === 'allman') {
if (checks.openingBraceOnNewLine === true) {
result = helpers.addUnique(result, createIssue(parser, {
line: currentNode.end.line,
column: currentNode.end.column
}, messages[2]));
}
if (checks.conditionOnNewLine === false) {
result = helpers.addUnique(result, createIssue(parser, {
line: previousNode.start.line,
column: previousNode.start.column
}, messages[4]));
}
}
}
}
return true;
});
return result;
}
};