Skip to content

Commit cc8d964

Browse files
committed
Merge pull request #95 from csscomb/tg/scss-less
Support sorting in *.scss and *.less files
2 parents 408f982 + fa747e6 commit cc8d964

12 files changed

Lines changed: 1014 additions & 136 deletions

.jshint-groups.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module.exports = {
2727
test: {
2828
options: {
2929
node: true,
30-
predef: ['describe', 'beforeEach', 'it']
30+
predef: ['describe', 'beforeEach', 'afterEach', 'it']
3131
},
3232
includes: ['test/*.js']
3333
}

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.X.X - 2013-XX-XX
4+
- Use Gonzales PE to parse *.scss and *.less files
5+
- Support sorting properties in *.scss and *.less files
6+
37
## 1.0.0 - 2013-11-06
48
- Option: vendor-prefix-align
59
- Dependencies updated

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ You can easily write your own [configuration](#configuration) to make your style
66

77
The main feature is the [sorting properties](#sort-order) in specific order.
88
It was inspired by the same-named [@miripiruni](https://github.com/miripiruni)'s [PHP-based tool](https://github.com/csscomb/csscomb).
9-
This is the new JavaScript version, based on powerful CSS parser [Gonzales](https://github.com/css/gonzales).
9+
This is the new JavaScript version, based on powerful CSS parser [Gonzales PE](https://github.com/tonyganch/gonzales-pe).
1010

1111
## Installation
1212

@@ -363,6 +363,35 @@ p {
363363
}
364364
```
365365
366+
If you sort properties in `*.scss` or `*.less` files, you can use one of 3
367+
keywords in your config:
368+
* `$variable` for variable declarations (e.g. `$var` in Sass or `@var` in LESS);
369+
* `$include` for included mixins (e.g. `@include ...` and `@extend ...` in Sass
370+
or `.mixin()` in LESS);
371+
* `$import` for `@import` rules.
372+
373+
Example: `{ "sort-order": [ [ "$variable" ], [ "$include" ], [ "top", "padding" ] ] }`
374+
375+
```scss
376+
/* before */
377+
p {
378+
padding: 0;
379+
@include mixin($color);
380+
$color: tomato;
381+
top: 0;
382+
}
383+
384+
/* after */
385+
p {
386+
$color: tomato;
387+
388+
@include mixin($color);
389+
390+
top: 0;
391+
padding: 0;
392+
}
393+
```
394+
366395
### stick-brace
367396
368397
Available values:
@@ -465,5 +494,6 @@ This software is released under the terms of the [MIT license](https://github.co
465494
## Other projects
466495
* https://github.com/senchalabs/cssbeautify
467496
* https://github.com/css/gonzales
497+
* https://github.com/tonyganch/gonzales-pe
468498
* https://github.com/css/csso
469499
* https://github.com/nzakas/parser-lib

lib/csscomb.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var gonzales = require('gonzales');
1+
var gonzales = require('gonzales-pe');
22
var minimatch = require('minimatch');
33
var vow = require('vow');
44
var vfs = require('vow-fs');
@@ -103,22 +103,23 @@ Comb.prototype = {
103103
/**
104104
* Process file provided with a string.
105105
* @param {String} text
106+
* @param {String} [syntax] Syntax name (e.g. `scss`)
106107
* @param {String} [filename]
107108
*/
108-
processString: function(text, filename) {
109+
processString: function(text, syntax, filename) {
109110
if (!text) return text;
110111
var tree;
111112
var string = JSON.stringify;
112113
try {
113-
tree = gonzales.srcToCSSP(text);
114+
tree = gonzales.cssToAST({ syntax: syntax, css: text });
114115
} catch (e) {
115116
throw new Error('Parsing error at ' + filename + ': ' + e.message);
116117
}
117118
if (typeof tree === 'undefined') {
118119
throw new Error('Undefined tree at ' + filename + ': ' + string(text) + ' => ' + string(tree));
119120
}
120121
tree = this.processTree(tree);
121-
return gonzales.csspToSrc(tree);
122+
return gonzales.astToCSS({ syntax: syntax, ast: tree });
122123
},
123124

124125
/**
@@ -129,9 +130,11 @@ Comb.prototype = {
129130
*/
130131
processFile: function(path) {
131132
var _this = this;
132-
if (this._shouldProcess(path) && path.match(/\.css$/)) {
133+
// TODO: Move extension check into `_shouldProcess` method
134+
if (this._shouldProcess(path) && path.match(/\.[css, scss]$/)) {
133135
return vfs.read(path, 'utf8').then(function(data) {
134-
var processedData = _this.processString(data, path);
136+
var syntax = path.split('.').pop();
137+
var processedData = _this.processString(data, syntax, path);
135138
var changed = data !== processedData;
136139
var lint = _this._lint;
137140

lib/options/always-semicolon.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module.exports = {
2929
if (type === 'declaration') {
3030
var space = [];
3131
for (var j = value.length; j--;) {
32-
if (value[j][0] !== 's' && value[j][0] !== 'comment') break;
32+
if (['s', 'commentML', 'commentSL'].indexOf(value[j][0]) === -1) break;
3333
space.unshift(value.splice(j)[0]);
3434
}
3535
node.splice.apply(node, [i + 1, 0, ['decldelim']].concat(space));

lib/options/remove-empty-rulesets.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ module.exports = {
6767
},
6868

6969
_isDeclarationOrComment: function(node) {
70-
return node[0] === 'declaration' || node[0] === 'comment';
70+
return ['declaration', 'commentML', 'commentSL'].indexOf(node[0]) > -1;
7171
},
7272

7373
_isRuleset: function(node) {

0 commit comments

Comments
 (0)