Skip to content

Commit a25249a

Browse files
committed
Gonzales 3.0: Update sort-order option
1 parent 76732e4 commit a25249a

6 files changed

Lines changed: 60 additions & 51 deletions

File tree

lib/options/sort-order.js

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var gonzales = require('gonzales-pe');
2+
13
module.exports = {
24
name: 'sort-order',
35

@@ -33,10 +35,9 @@ module.exports = {
3335

3436
/**
3537
* Processes tree node.
36-
* @param {String} nodeType
3738
* @param {node} node
3839
*/
39-
process: function(nodeType, node) {
40+
process: function(node) {
4041
var _this = this;
4142
// Types of nodes that can be sorted:
4243
var NODES = ['atruleb', 'atruler', 'atrules', 'commentML', 'commentSL',
@@ -73,7 +74,7 @@ module.exports = {
7374
* @param {node} node Space node.
7475
*/
7576
var removeEmptyLines = function(node) {
76-
node[1] = node[1].replace(/\n[\s\t\n\r]*\n/, '\n');
77+
node.content = node.content.replace(/\n[\s\t\n\r]*\n/, '\n');
7778
};
7879

7980
/**
@@ -88,7 +89,7 @@ module.exports = {
8889
var d = [];
8990

9091
for (; i < l; i++) {
91-
currentNode = node[i];
92+
currentNode = node.get(i);
9293
// If there is no node left,
9394
// stop and do nothing with previously found spaces/comments:
9495
if (!currentNode) {
@@ -97,7 +98,7 @@ module.exports = {
9798

9899
// If the node is declaration or @-rule, stop and return all
99100
// found nodes with spaces and comments (if there are any):
100-
if (SC.indexOf(currentNode[0]) === -1) break;
101+
if (SC.indexOf(currentNode.type) === -1) break;
101102

102103
sc.push(currentNode);
103104
d.push(i);
@@ -124,26 +125,28 @@ module.exports = {
124125

125126
// Check every next node:
126127
for (; i < l; i++) {
127-
currentNode = node[i + 1];
128+
currentNode = node.get(i + 1);
128129
// If there is no node, or it is nor spaces neither comment, stop:
129-
if (!currentNode || SC.indexOf(currentNode[0]) === -1) break;
130+
if (!currentNode || SC.indexOf(currentNode.type) === -1) break;
130131

131-
if (['commentML', 'commentSL'].indexOf(currentNode[0]) > -1) {
132+
if (currentNode.is('commentML') || currentNode.is('commentSL')) {
132133
sc.push(currentNode);
133134
d.push(i + 1);
134135
continue;
135136
}
136137

137-
lbIndex = currentNode[1].indexOf('\n');
138+
lbIndex = currentNode.content.indexOf('\n');
138139

139140
// If there are any line breaks in a node with spaces, stop and
140141
// split the node into two: one with spaces before line break
141142
// and one with `\n` symbol and everything that goes after.
142143
// Combine the first one with declaration/@-rule's node:
143144
if (lbIndex > -1) {
144145
// TODO: Don't push an empty array
145-
sc.push(['s', currentNode[1].substring(0, lbIndex)]);
146-
currentNode[1] = currentNode[1].substring(lbIndex);
146+
var s = currentNode.content.substring(0, lbIndex);
147+
var space = gonzales.createNode({ type: 's', content: s });
148+
sc.push(space);
149+
currentNode.content = currentNode.content.substring(lbIndex);
147150
break;
148151
}
149152

@@ -162,8 +165,8 @@ module.exports = {
162165
* @returns {Object} Extended node
163166
*/
164167
var extendNode = function() {
165-
currentNode = node[i];
166-
var nextNode = node[i + 1];
168+
currentNode = node.get(i);
169+
var nextNode = node.get(i + 1);
167170
// Object containing current node, all corresponding spaces,
168171
// comments and other information:
169172
var extendedNode;
@@ -195,13 +198,13 @@ module.exports = {
195198
extendedNode.sc1 = checkSC1();
196199

197200
if (extendedNode.sc1.length) {
198-
currentNode = node[i];
199-
nextNode = node[i + 1];
201+
currentNode = node.get(i);
202+
nextNode = node.get(i + 1);
200203
}
201204

202205
// If there is `;` right after the declaration, save it with the
203206
// declaration and mark it for removing from parent node:
204-
if (currentNode && nextNode && nextNode[0] === 'declDelim') {
207+
if (currentNode && nextNode && nextNode.is('declDelim')) {
205208
extendedNode.delim.push(nextNode);
206209
deleted.push(i + 1);
207210
i++;
@@ -229,8 +232,8 @@ module.exports = {
229232
var prefixesRegExp = /^(-webkit-|-moz-|-ms-|-o-)(.*)$/;
230233

231234
// Get property name (i.e. `color`, `-o-animation`):
232-
a = a.node[1][1][1];
233-
b = b.node[1][1][1];
235+
a = a.node.get(0).get(0).content;
236+
b = b.node.get(0).get(0).content;
234237

235238
// Get prefix and unprefixed part. For example:
236239
// ['-o-animation', '-o-', 'animation']
@@ -252,23 +255,23 @@ module.exports = {
252255

253256
// TODO: Think it through!
254257
// Sort properties only inside blocks:
255-
if (nodeType !== 'block') return;
258+
if (!node.is('block')) return;
256259

257260
// Check every child node.
258261
// If it is declaration (property-value pair, e.g. `color: tomato`),
259262
// or @-rule (e.g. `@include nani`),
260263
// combine it with spaces, semicolon and comments and move them from
261264
// current node to a separate list for further sorting:
262265
for (i = 0, l = node.length; i < l; i++) {
263-
if (NODES.indexOf(node[i][0]) === -1) continue;
266+
if (NODES.indexOf(node.get(i).type) === -1) continue;
264267

265268
// Save preceding spaces and comments, if there are any, and mark
266269
// them for removing from parent node:
267270
sc0 = checkSC0();
268271
if (!sc0) continue;
269272

270273
// If spaces/comments are the last nodes, stop and go to sorting:
271-
if (!node[i]) {
274+
if (!node.get(i)) {
272275
deleted.splice(deleted.length - sc0.length, deleted.length + 1);
273276
break;
274277
}
@@ -279,17 +282,19 @@ module.exports = {
279282
// If not, proceed with the next node:
280283
propertyName = null;
281284
// Look for includes:
282-
if (node[i][0] === 'include') {
285+
if (node.get(i).is('include')) {
283286
propertyName = '$include';
284287
} else {
285-
for (j = 1, nl = node[i].length; j < nl; j++) {
286-
currentNode = node[i][j];
287-
if (currentNode[0] === 'property') {
288-
propertyName = currentNode[1][0] === 'variable' ?
289-
'$variable' : currentNode[1][1];
288+
for (j = 0, nl = node.get(i).length; j < nl; j++) {
289+
currentNode = node.get(i).get(j);
290+
if (!currentNode) continue;
291+
292+
if (currentNode.is('property')) {
293+
propertyName = currentNode.get(0).is('variable') ?
294+
'$variable' : currentNode.get(0).content;
290295
break;
291-
} else if (currentNode[0] === 'atkeyword' &&
292-
currentNode[1][1] === 'import') { // Look for imports
296+
} else if (currentNode.is('atkeyword') &&
297+
currentNode.get(0).content === 'import') { // Look for imports
293298
propertyName = '$import';
294299
break;
295300
}
@@ -310,7 +315,7 @@ module.exports = {
310315

311316
// Remove all nodes, that were moved to a `sorted` list, from parent node:
312317
for (i = deleted.length - 1; i > -1; i--) {
313-
node.splice(deleted[i], 1);
318+
node.content.splice(deleted[i], 1);
314319
}
315320

316321
// Sort declarations saved for sorting:
@@ -353,25 +358,29 @@ module.exports = {
353358

354359
// Divide declarations from different groups with an empty line:
355360
if (prevNode && currentNode.groupIndex > prevNode.groupIndex) {
356-
if (sc0[0] && sc0[0][0] === 's' &&
361+
if (sc0[0] && sc0[0].is('s') &&
357362
(this.syntax === 'sass' ||
358-
sc0[0][1].match(/\n/g) &&
359-
sc0[0][1].match(/\n/g).length < 2)) {
360-
sc0[0][1] = '\n' + sc0[0][1];
363+
sc0[0].content.match(/\n/g) &&
364+
sc0[0].content.match(/\n/g).length < 2)) {
365+
sc0[0].content = '\n' + sc0[0].content;
361366
}
362367
}
363368

364369
for (j = 0, nl = sc2.length; j < nl; j++) {
365-
node.unshift(sc2[j]);
370+
node.content.unshift(sc2[j]);
371+
}
372+
if (currentNode.delim.length > 0) {
373+
var delim = this.syntax === 'sass' ? '\n' : ';';
374+
var declDelim = gonzales.createNode({ type: 'declDelim', content: delim });
375+
node.content.unshift(declDelim);
366376
}
367-
if (currentNode.delim.length > 0) node.unshift(['declDelim']);
368377
for (j = 0, nl = sc1.length; j < nl; j++) {
369-
node.unshift(sc1[j]);
378+
node.content.unshift(sc1[j]);
370379
}
371-
node.unshift(currentNode.node);
380+
node.content.unshift(currentNode.node);
372381

373382
for (j = 0, nl = sc0.length; j < nl; j++) {
374-
node.unshift(sc0[j]);
383+
node.content.unshift(sc0[j]);
375384
}
376385
}
377386
}

test/options/sort-order-fallback/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
describe.skip('options/sort-order-fallback', function() {
1+
describe('options/sort-order-fallback', function() {
22
it('Should sort leftovers alphabetically if `sort-order-fallback` is set', function() {
33
var config = {
44
'sort-order-fallback': 'abc',

test/options/sort-order-less/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
describe.skip('options/sort-order (less)', function() {
1+
describe('options/sort-order (less)', function() {
22
it('Should sort properties inside rules', function() {
33
this.comb.configure({ 'sort-order': [
44
['top', 'color']

test/options/sort-order-sass/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
describe.skip('options/sort-order (sass)', function() {
1+
describe('options/sort-order (sass)', function() {
22
it('Should sort properties inside rules', function() {
33
this.comb.configure({ 'sort-order': [
44
['top', 'color']

test/options/sort-order-scss/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
describe.skip('options/sort-order (scss)', function() {
1+
describe('options/sort-order (scss)', function() {
22
it('Should sort properties inside rules (single line)', function() {
33
this.comb.configure({ 'sort-order': [
44
['top', 'color']

test/options/sort-order/test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var assert = require('assert');
22

3-
describe.skip('options/sort-order', function() {
3+
describe('options/sort-order', function() {
44
it('Should be in expected order in case properties are not grouped', function() {
55
this.comb.configure({ 'sort-order': ['position', 'z-index'] });
66
this.shouldBeEqual('single-group.css', 'single-group.expected.css');
@@ -61,47 +61,47 @@ describe.skip('options/sort-order', function() {
6161
assert.equal(input, expected);
6262
});
6363

64-
it('Issue 94. Test 1', function() {
64+
it.skip('Issue 94. Test 1', function() {
6565
var config = this.Comb.getConfig('csscomb');
6666
this.comb.configure(config);
6767
this.shouldBeEqual('issue-94-1.css', 'issue-94-1.expected.css');
6868
});
6969

70-
it('Issue 94. Test 2', function() {
70+
it.skip('Issue 94. Test 2', function() {
7171
var config = this.Comb.getConfig('csscomb');
7272
this.comb.configure(config);
7373
this.shouldBeEqual('issue-94-2.css', 'issue-94-2.expected.css');
7474
});
7575

76-
it('Issue 94. Test 3', function() {
76+
it.skip('Issue 94. Test 3', function() {
7777
var config = this.Comb.getConfig('csscomb');
7878
this.comb.configure(config);
7979
this.shouldBeEqual('issue-94-3.css', 'issue-94-3.expected.css');
8080
});
8181

82-
it('Should place the leftovers in the end', function() {
82+
it.skip('Should place the leftovers in the end', function() {
8383
var config = this.Comb.getConfig('csscomb');
8484
this.comb.configure(config);
8585
this.shouldBeEqual('leftovers-1.css', 'leftovers-1.expected.css');
8686
});
8787

88-
it('Should place the leftovers in the beginning', function() {
88+
it.skip('Should place the leftovers in the beginning', function() {
8989
var config = this.Comb.getConfig('csscomb');
9090
config['sort-order'][0].unshift(['...']);
9191
this.comb.configure(config);
9292
this.shouldBeEqual('leftovers-2.css', 'leftovers-2.expected.css');
9393
config['sort-order'][0].shift();
9494
});
9595

96-
it('Should place the leftovers in the beginning of its group', function() {
96+
it.skip('Should place the leftovers in the beginning of its group', function() {
9797
var config = this.Comb.getConfig('csscomb');
9898
config['sort-order'][1].unshift('...');
9999
this.comb.configure(config);
100100
this.shouldBeEqual('leftovers-3.css', 'leftovers-3.expected.css');
101101
config['sort-order'][1].shift();
102102
});
103103

104-
it('Should place the leftovers in the middle of its group', function() {
104+
it.skip('Should place the leftovers in the middle of its group', function() {
105105
var config = this.Comb.getConfig('csscomb');
106106
config['sort-order'][1].splice(1, 0, '...');
107107
this.comb.configure(config);

0 commit comments

Comments
 (0)