-
Notifications
You must be signed in to change notification settings - Fork 455
/
Copy pathlines-between-rulesets.js
257 lines (208 loc) · 5.35 KB
/
lines-between-rulesets.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
'use strict';
let gonzales = require('gonzales-pe');
let os = require('os');
let option = {
newLinesString: '',
newLinesNode: null,
/**
* Option's name as it's used in config.
* @type {String}
*/
get name() {
return 'lines-between-rulesets';
},
/**
* Name of option that must run after this option.
* @type {String}
*/
get runBefore() {
return 'block-indent';
},
/**
* List of syntaxes that are supported by this option.
* @type {Array}
*/
get syntax() {
return ['css', 'less', 'sass', 'scss'];
},
/**
* Types of values this option accepts in config.
* @type {Object}
*/
get accepts() {
return {
number: true
};
},
/**
* @param {number} value
* @returns {number}
*/
/*
** Still need to override, as the core implementation of setValue doesn't
** pass numbers through, but creates a string of spaces of the same length.
*/
setValue(value) {
let valueType = typeof value;
if (valueType !== 'number') {
throw new Error('Value must be a number.');
}
return value;
},
buildSpacing(syntax) {
let spacing = '';
let numNewLines = 0;
let newLinesOffset = 1;
if (syntax === 'sass') {
newLinesOffset = 0;
}
numNewLines = Math.round(this.value) + newLinesOffset;
for (var i = 0; i < numNewLines; i++) {
spacing += os.EOL;
}
return spacing;
},
/**
* Processes ast and fixes found code style errors.
* @param {Node} ast
*/
process(ast) {
this.newLinesString = this.buildSpacing(ast.syntax);
this.newLinesNode = gonzales.createNode({
type: 'space',
content: this.newLinesString
});
this.processBlock(ast);
},
processBlock(x) {
if (x.is('stylesheet')) {
// Check all @rules
this.processAtRules(x);
// Check all rulesets
this.processRuleSets(x);
}
x.forEach((node) => {
if (!node.is('block')) {
return this.processBlock(node);
}
// Check all @rules
this.processAtRules(node);
// Check all rulesets
this.processRuleSets(node);
this.processBlock(node);
});
},
processAtRules(node) {
node.forEach('atrule', (atRuleNode, index) => {
this.insertNewlines(node, index);
});
},
processRuleSets(node) {
node.forEach('ruleset', (ruleSetNode, index) => {
this.insertNewlines(node, index);
});
},
isComment(node) {
if (!node) {
return false;
}
return (node.is('singlelineComment') || node.is('multilineComment'));
},
isNewline(node) {
if (!node) {
return false;
}
var content = node.content;
return (content === '\n' || content === '\r\n');
},
prevLineIsComment(parent, index) {
let indexThreshold = 2;
let prevChild;
let prevMinusOneChild;
let prevMinusTwoChild;
let parentSyntax = parent ? parent.syntax : null;
// Sass is troublesome because newlines are counted as separate nodes
if (parentSyntax === 'sass') {
indexThreshold = 3;
}
if (!parent || index < indexThreshold) {
return false;
}
prevChild = parent.get(index - 1);
prevMinusOneChild = parent.get(index - 2);
if (parentSyntax === 'sass') {
prevMinusTwoChild = parent.get(index - 3);
return (
this.isComment(prevMinusTwoChild) &&
this.isNewline(prevMinusOneChild) &&
prevChild.is('space')
);
}
return (this.isComment(prevMinusOneChild) && prevChild.is('space'));
},
/*
** Find the latest previous child that isn't a comment, and return its index.
*/
findLatestNonCommentNode(parent, index) {
let prevChild;
let lastNonCommentIndex = -1;
let currentIndex = index;
let jumpSize = 2;
if (parent.syntax === 'sass') {
jumpSize = 3;
}
while (currentIndex >= 0) {
if (this.prevLineIsComment(parent, currentIndex)) {
currentIndex -= jumpSize;
continue;
}
prevChild = parent.get(currentIndex - 1);
if (!this.isComment(prevChild)) {
lastNonCommentIndex = currentIndex - 1;
break;
}
currentIndex--;
}
return lastNonCommentIndex;
},
insertNewlinesAsString(node) {
let content = node.content;
let lastNewline = content.lastIndexOf('\n');
let newContent;
if (lastNewline > -1) {
content = content.substring(lastNewline + 1);
}
newContent = this.newLinesString + content;
node.content = newContent;
},
insertNewlinesAsNode(node) {
node.insert(node.length, this.newLinesNode);
},
insertNewlines(node, index) {
let prevChild = node.get(index - 1);
let shouldInsert = false;
// Check for previous nodes that are not a space
// Do not insert if the ruleset is the first item
for (var i = 0; i < index; i++) {
if (!node.get(i).is('space')) {
shouldInsert = true;
break;
}
}
if (prevChild && shouldInsert) {
if (this.prevLineIsComment(node, index) || this.isComment(prevChild)) {
let lastNonCommentIndex = this.findLatestNonCommentNode(node, index);
prevChild = node.get(lastNonCommentIndex);
if (!prevChild) {
return;
}
}
if (prevChild.is('space')) {
this.insertNewlinesAsString(prevChild);
} else {
this.insertNewlinesAsNode(prevChild);
}
}
}
};
module.exports = option;