forked from csscomb/csscomb.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsscomb.js
More file actions
389 lines (353 loc) · 12.2 KB
/
csscomb.js
File metadata and controls
389 lines (353 loc) · 12.2 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
var gonzales = require('gonzales-pe');
var minimatch = require('minimatch');
var vow = require('vow');
var fs = require('fs');
var vfs = require('vow-fs');
var doNothing = function() {};
/**
* Starts Code Style processing process.
*
* @constructor
* @name Comb
*/
var Comb = function(config) {
this.SUPPORTED_SYNTAXES = ['css', 'scss', 'less'];
this._options = [
'remove-empty-rulesets',
'always-semicolon',
'color-case',
'color-shorthand',
'element-case',
'leading-zero',
'quotes',
'strip-spaces',
'eof-newline',
'stick-brace',
'colon-space',
'combinator-space',
'rule-indent',
'block-indent',
'unitless-zero',
'sort-order',
'vendor-prefix-align'
];
this._exclude = null;
this._detect = false;
// If config was passed, configure:
if (typeof config === 'string' &&
['csscomb', 'zen', 'yandex'].indexOf(config) > -1) {
config = require('../config/' + config + '.json');
}
if (typeof config === 'object') this.configure(config);
// Return Comb's object to make creating new instance chainable:
return this;
};
Comb.prototype = {
/**
* Get one of configuration files from `config` directory
* @param {String} name Config's name: 'csscomb', 'zen' or 'yandex'
* @returns {Object} Configuration object
*/
getConfig: function(name) {
name = name || 'csscomb';
if (typeof name !== 'string') {
throw new Error('Config name should be a string');
}
if (['csscomb', 'zen', 'yandex'].indexOf(name) < 0) {
var message = name + ' is not a valid config name. Try one of ' +
'the following: \'csscomb\', \'zen\' or \'yandex\'.';
throw new Error(message);
}
return require('../config/' + name + '.json');
},
/**
* Loads configuration from JSON.
* Activates and configures required options.
*
* @param {Object} config
*/
configure: function(config) {
this._handlers = [];
this._options.forEach(function(option) {
if (typeof config[option] === 'undefined') return;
try {
var handler = require('./options/' + option).setValue(config[option]);
handler && this._handlers.push(handler);
} catch (e) {
console.warn('Error loading "%s" handler: %s', option, e.message);
}
}, this);
this._exclude = (config.exclude || []).map(function(pattern) {
return new minimatch.Minimatch(pattern);
});
this.processed = 0;
this.tbchanged = 0;
this.changed = 0;
this._verbose = config.verbose;
this._lint = config.lint;
// Return Comb's object to make the method chainable:
return this;
},
/**
* Detects the options in the given string
*
* @param {String} text Stylesheet
* @param {Array} options List of options to detect
* @returns {Object}
*/
detectInString: function(text, options) {
var result;
this._detect = true;
this._detected = {};
this._handlers = [];
this._options.forEach(function(option) {
try {
var handler = require('./options/' + option);
if (!handler || options && options.indexOf(option) === -1) return;
handler._name = option;
this._detected[option] = [];
this._handlers.push(handler);
} catch (e) {
console.warn('Error loading "%s" handler: %s', option, e.message);
}
}, this);
result = this.processString(text);
this._detect = false;
return result;
},
/**
* Detects the options in the given file
*
* @param {String} path Path to the stylesheet
* @param {Array} options List of options to detect
* @returns {Object}
*/
detectInFile: function(path, options) {
var stylesheet = fs.readFileSync(path, 'utf8');
return this.detectInString(stylesheet, options);
},
/**
* Processes stylesheet tree node.
*
* @param {Array} tree Parsed tree
* @returns {Array}
*/
processTree: function(tree) {
// We walk across complete tree for each handler,
// because we need strictly maintain order in which handlers work,
// despite fact that handlers work on different level of the tree.
this._handlers.forEach(function(handler) {
this.processNode(['tree', tree], 0, handler);
}, this);
return tree;
},
/**
* Processes tree node.
* @param {Array} node Tree node
* @param {Number} level Indent level
*/
processNode: function(node, level, handler) {
node.forEach(function(node) {
if (!Array.isArray(node)) return;
var nodeType = node.shift();
if (this._detect) {
if (handler.detect) {
var detected = handler.detect(nodeType, node, level);
if (detected !== undefined) {
this._detected[handler._name].push(detected);
}
}
} else {
handler.process(nodeType, node, level);
}
node.unshift(nodeType);
if (nodeType === 'atrulers') level++;
this.processNode(node, level, handler);
}, this);
},
/**
* Process file provided with a string.
* @param {String} text
* @param {String} [syntax] Syntax name (e.g. `scss`)
* @param {String} [filename]
*/
processString: function(text, syntax, filename) {
if (!text) return text;
var tree;
var string = JSON.stringify;
try {
tree = gonzales.cssToAST({ syntax: syntax, css: text });
} catch (e) {
throw new Error('Parsing error at ' + filename + ': ' + e.message);
}
if (typeof tree === 'undefined') {
throw new Error('Undefined tree at ' + filename + ': ' + string(text) + ' => ' + string(tree));
}
tree = this.processTree(tree);
if (this._detect) {
return this._getDetectedOptions(this._detected);
} else {
return gonzales.astToCSS({ syntax: syntax, ast: tree });
}
},
/**
* Process single file.
*
* @param {String} path
* @returns {Promise}
*/
processFile: function(path) {
var _this = this;
if (this._shouldProcessFile(path)) {
return vfs.read(path, 'utf8').then(function(data) {
var syntax = path.split('.').pop();
var processedData = _this.processString(data, syntax, path);
var changed = data !== processedData;
var lint = _this._lint;
var tick = changed ? (lint ? '!' : '✓') : ' ';
var message = _this._verbose ? console.log.bind(null, tick, path) : doNothing;
_this.processed++;
changed && _this.tbchanged++;
if (!changed || lint) {
message();
} else {
return vfs.write(path, processedData, 'utf8').then(function() {
_this.changed++;
message();
});
}
});
}
return null;
},
/**
* Process directory recursively.
*
* @param {String} path
* @returns {Promise}
*/
processDirectory: function(path) {
var _this = this;
return vfs.listDir(path).then(function(filenames) {
return vow.all(filenames.map(function(filename) {
var fullname = path + '/' + filename;
return vfs.stat(fullname).then(function(stat) {
if (stat.isDirectory()) {
return _this._shouldProcess(fullname) && _this.processDirectory(fullname);
} else {
return vow.when(_this.processFile(fullname)).then(function(errors) {
if (errors) return errors;
});
}
});
})).then(function(results) {
return [].concat.apply([], results);
});
});
},
/**
* Process directory or file.
*
* @param {String} path
*/
processPath: function(path) {
path = path.replace(/\/$/, '');
var _this = this;
return vfs.exists(path).then(function(exists) {
if (exists) {
return vfs.stat(path).then(function(stat) {
if (stat.isDirectory()) {
return _this.processDirectory(path);
} else {
return vow.when(_this.processFile(path)).then(function(errors) {
if (errors) return [errors];
return;
});
}
});
} else {
throw new Error('Path ' + path + ' was not found.');
}
});
},
/**
* Returns true if specified path is not in excluded list.
*
* @returns {Boolean}
*/
_shouldProcess: function(path) {
path = path.replace(/^\.\//, '');
var exclude = this._exclude;
for (var i = exclude.length; i--;) {
if (exclude[i].match(path)) return false;
}
return true;
},
/**
* Returns true if specified path is not in excluded list and has one of
* acceptable extensions.
*
* @returns {Boolean}
*/
_shouldProcessFile: function(path) {
// Get file's extension:
var syntax = path.split('.').pop();
// Check if syntax is supported. If not, ignore the file:
if (this.SUPPORTED_SYNTAXES.indexOf(syntax) < 0) {
return false;
}
return this._shouldProcess(path);
},
/**
* Gets the detected options.
*
* @param {Object} detected
* @returns {Object}
*/
_getDetectedOptions: function(detected) {
var options = {};
Object.keys(detected).forEach(function(option) {
// List of all the detected variants from the stylesheet for the given option:
var values = detected[option];
var i;
if (values.length) {
if (values.length === 1) {
options[option] = values[0];
} else {
// If there are more than one value for the option, find the most popular one;
// `variants` would be populated with the popularity for different values.
var variants = {};
var bestGuess = null;
var maximum = 0;
for (i = values.length; i--;) {
var currentValue = values[i];
// Count the current value:
if (variants[currentValue]) {
variants[currentValue]++;
} else {
variants[currentValue] = 1;
}
// If the current variant is the most popular one, treat it as the best guess:
if (variants[currentValue] >= maximum) {
maximum = variants[currentValue];
bestGuess = currentValue;
}
}
if (bestGuess !== null) {
options[option] = bestGuess;
}
}
} else {
// If there are no values for the option, check if there is a default one:
for (i = this._handlers.length; i--;) {
if (this._handlers[i]._name === option && this._handlers[i]._detectDefault !== undefined) {
options[option] = this._handlers[i]._detectDefault;
break;
}
}
}
}, this);
return options;
}
};
module.exports = Comb;