Skip to content

Commit 35159d8

Browse files
committed
Sort order: Handle properties preceeding rulesets
Previously, if a property-value pair was followed by a linebreak and anything but property, import or include (e.g. ruleset or condition), the pair appeared twice in the result code. For example, a { color: tomato; span {foo: bar}} would become: a { color: tomato; color: tomato; span {foo: bar}} That was due to incorrect removal of spaces form `deleted` array. This commit fixes the issue.
1 parent 7f4dd15 commit 35159d8

8 files changed

Lines changed: 79 additions & 1 deletion

File tree

lib/options/sort-order.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ module.exports = {
223223
}
224224
}
225225

226+
// If current node is not property-value pair or import or include,
227+
// skip it and continue with the next node:
226228
if (!propertyName) {
227229
deleted.splice(deleted.length - sc0.length, deleted.length + 1);
228230
continue;

test/sort-order-scss.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,19 @@ describe('options/sort-order (scss)', function() {
1616
comb = new Comb();
1717
});
1818

19-
it('Should sort properties inside rules', function() {
19+
it('Should sort properties inside rules (single line)', function() {
20+
config = { 'sort-order': [
21+
['top', 'color']
22+
] };
23+
24+
input = readFile('rule.scss');
25+
expected = readFile('rule.expected.scss');
26+
27+
comb.configure(config);
28+
assert.equal(comb.processString(input, 'scss'), expected);
29+
});
30+
31+
it('Should sort properties inside rules (multiple lines)', function() {
2032
config = { 'sort-order': [
2133
['top', 'color']
2234
] };
@@ -147,4 +159,28 @@ describe('options/sort-order (scss)', function() {
147159
comb.configure(config);
148160
assert.equal(comb.processString(input, 'scss'), expected);
149161
});
162+
163+
it('Should handle properties preceeding rulesets', function() {
164+
config = { 'sort-order': [
165+
['top', 'left', 'color']
166+
] };
167+
168+
input = readFile('ruleset.scss');
169+
expected = readFile('ruleset.expected.scss');
170+
171+
comb.configure(config);
172+
assert.equal(comb.processString(input, 'scss'), expected);
173+
});
174+
175+
it('Should handle properties preceeding conditions', function() {
176+
config = { 'sort-order': [
177+
['font-size', 'display', 'top', 'color']
178+
] };
179+
180+
input = readFile('condition.scss');
181+
expected = readFile('condition.expected.scss');
182+
183+
comb.configure(config);
184+
assert.equal(comb.processString(input, 'scss'), expected);
185+
});
150186
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
div {
2+
top: 0;
3+
color: tomato;
4+
@if @color == tomato {
5+
font-size: 2px;
6+
display: block;
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
div {
2+
color: tomato;
3+
@if @color == tomato {
4+
display: block;
5+
font-size: 2px;
6+
}
7+
top: 0;
8+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
div {
2+
top: 0;
3+
color: tomato;
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
div {
2+
color: tomato;
3+
top: 0;
4+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
div {
2+
left: 0;
3+
color: tomato;
4+
a {
5+
top: 0;
6+
color: nani;
7+
}
8+
}

test/sort-order-scss/ruleset.scss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
div {
2+
color: tomato;
3+
a {
4+
color: nani;
5+
top: 0;
6+
}
7+
left: 0;
8+
}

0 commit comments

Comments
 (0)