Skip to content

Commit ccc2a8f

Browse files
committed
space-after-declaration -> space-between-declarations
1 parent 519255a commit ccc2a8f

18 files changed

+122
-80
lines changed

lib/options/space-after-declaration.js

Lines changed: 0 additions & 57 deletions
This file was deleted.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
module.exports = (function() {
2+
function getDeclarationEnd(node, i) {
3+
for (;i < node.length; i++) {
4+
if (!node[i + 1]) {
5+
return 0;
6+
} else if (node[i + 1][0] === 's') {
7+
if (node[i + 1][1].indexOf('\n') > -1) {
8+
if (node[i + 2] && node[i + 2][0] === 'declaration') {
9+
return i;
10+
} else {
11+
return 0;
12+
}
13+
} else if (node[i + 2] && node[i + 2][0] === 'commentML') {
14+
if (node[i + 3] && node[i + 3][0] === 'declaration') {
15+
return i + 2;
16+
} else if (node[i + 3] && node[i + 3][0] === 's') {
17+
if (node[i + 4] && node[i + 4][0] === 'declaration') {
18+
return i + 2;
19+
} else {
20+
return 0;
21+
}
22+
} else {
23+
return 0;
24+
}
25+
} else if (node[i + 2] && node[i + 2][0] === 'declaration') {
26+
return i;
27+
}
28+
} else if (node[i + 1][0] === 'declaration') {
29+
return i;
30+
} else if (node[i + 1][0] === 'commentML') {
31+
if (node[i + 2] && node[i + 2][0] === 'declaration') {
32+
return i + 1;
33+
} else if (node[i + 2] && node[i + 2][0] === 's') {
34+
if (node[i + 3] && node[i + 3][0] === 'declaration') {
35+
return i + 1;
36+
}
37+
} else {
38+
return 0;
39+
}
40+
} else {
41+
return 0;
42+
}
43+
}
44+
}
45+
46+
return {
47+
name: 'space-between-declarations',
48+
49+
runBefore: 'block-indent',
50+
51+
syntax: ['css', 'less', 'scss'],
52+
53+
accepts: {
54+
number: true,
55+
string: /^[ \t\n]*$/
56+
},
57+
58+
/**
59+
* Processes tree node.
60+
*
61+
* @param {String} nodeType
62+
* @param {node} node
63+
*/
64+
process: function(nodeType, node) {
65+
var value = this.getValue('space-between-declarations');
66+
67+
// TODO: Limit nodes to blocks, stylesheet, etc.
68+
69+
for (var i = 0, l = node.length; i < l; i++) {
70+
var currentNode = node[i];
71+
72+
if (currentNode[0] !== 'declDelim') continue;
73+
74+
// Grom user's point of view "declaration" includes semicolons
75+
// and comments placed on the same line.
76+
// So group those things together:
77+
var declarationEnd = getDeclarationEnd(node, i);
78+
if (!declarationEnd) {
79+
continue;
80+
} else {
81+
i = declarationEnd;
82+
}
83+
84+
var nextNode = node[i + 1];
85+
if (nextNode[0] === 's') {
86+
nextNode[1] = value;
87+
} else {
88+
i++;
89+
l++;
90+
node.splice(i, 0, ['s', value]);
91+
}
92+
}
93+
}
94+
};
95+
})();

test/core/use.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ describe('.use()', function() {
2121
'sort-order-fallback',
2222
'space-after-colon',
2323
'space-after-combinator',
24-
'space-after-declaration',
2524
'space-after-opening-brace',
2625
'space-after-selector-delimiter',
2726
'space-before-colon',
2827
'space-before-combinator',
2928
'space-before-opening-brace',
3029
'space-before-selector-delimiter',
30+
'space-between-declarations',
3131
'block-indent',
3232
'sort-order',
3333
'strip-spaces',

test/options/space-after-declaration/comments.expected.css

Lines changed: 0 additions & 4 deletions
This file was deleted.

test/options/space-after-declaration/integer-value.expected.css

Lines changed: 0 additions & 2 deletions
This file was deleted.

test/options/space-after-declaration/space-value.expected.css

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
1-
describe('options/space-after-declaration:', function() {
1+
describe('options/space-between-declarations:', function() {
22
beforeEach(function() {
33
this.filename = __filename;
44
});
55

66
it('Array value => should not change anything', function() {
7-
this.comb.configure({ 'space-after-declaration': ['', ' '] });
7+
this.comb.configure({ 'space-between-declarations': ['', ' '] });
88
this.shouldBeEqual('test.css');
99
});
1010

1111
it('Invalid string value => should not change anything', function() {
12-
this.comb.configure({ 'space-after-declaration': ' nani ' });
12+
this.comb.configure({ 'space-between-declarations': ' nani ' });
1313
this.shouldBeEqual('test.css');
1414
});
1515

1616
it('Float number value => should not change anything', function() {
17-
this.comb.configure({ 'space-after-declaration': 3.5 });
17+
this.comb.configure({ 'space-between-declarations': 3.5 });
1818
this.shouldBeEqual('test.css');
1919
});
2020

2121
it('Integer value => should set proper space after declaration', function() {
22-
this.comb.configure({ 'space-after-declaration': 0 });
22+
this.comb.configure({ 'space-between-declarations': 0 });
2323
this.shouldBeEqual('integer-value.css', 'integer-value.expected.css');
2424
});
2525

2626
it('Valid string value (spaces only) => should set proper space after declaration', function() {
27-
this.comb.configure({ 'space-after-declaration': ' ' });
27+
this.comb.configure({ 'space-between-declarations': ' ' });
2828
this.shouldBeEqual('space-value.css', 'space-value.expected.css');
2929
});
3030

3131
it('Valid string value (spaces and newlines) => should set proper space after declaration', function() {
32-
this.comb.configure({ 'space-after-declaration': '\n ' });
32+
this.comb.configure({ 'space-between-declarations': '\n ' });
3333
this.shouldBeEqual('space-newline-value.css', 'space-newline-value.expected.css');
3434
});
3535

3636
it('Should leave comments as is', function() {
37-
this.comb.configure({ 'space-after-declaration': 1 });
37+
this.comb.configure({ 'space-between-declarations': 1 });
3838
this.shouldBeEqual('comments.css', 'comments.expected.css');
3939
});
4040

4141
it('Issue 239', function() {
42-
this.comb.configure({ 'space-after-declaration': '\n ' });
42+
this.comb.configure({ 'space-between-declarations': '\n ' });
4343
this.shouldBeEqual('issue-239.css', 'issue-239.expected.css');
4444
});
4545
});

test/options/space-after-declaration/comments.css renamed to test/options/space-between-declarations/comments.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
a {
22
color: tomato;
33
/* comment */
4-
top: 0;
4+
top: 0; /* comment */
55
left: 10px;
66
right: 0;
77
/* comment */
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
a {
2+
color: tomato;
3+
/* comment */
4+
top: 0; /* comment */ left: 10px; right: 0;
5+
/* comment */
6+
}

0 commit comments

Comments
 (0)