Skip to content

Commit f0a241e

Browse files
committed
Added the detection for remove-empty-rulesets option
1 parent 3851fb9 commit f0a241e

File tree

3 files changed

+109
-1
lines changed

3 files changed

+109
-1
lines changed

lib/options/remove-empty-rulesets.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,22 @@ module.exports = {
8080

8181
_isWhitespace: function(node) {
8282
return node[0] === 's';
83-
}
83+
},
84+
85+
/**
86+
* Detects the syntax at the tree node.
87+
* This option is treated as `true` by default, but any trailing space would invalidate it.
88+
*
89+
* @param {String} nodeType
90+
* @param {node} node
91+
*/
92+
_detectDefault: true,
8493

94+
detect: function(nodeType, node) {
95+
if (nodeType === 'atrulers' || nodeType === 'block') {
96+
if (node.length === 0 || (node.length === 1 && node[0][0] === 's')) {
97+
return false;
98+
}
99+
}
100+
}
85101
};

test/integral.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ describe('integral test', function() {
3838
assert.equal(
3939
JSON.stringify(comb.processString(results[1])),
4040
JSON.stringify({
41+
'remove-empty-rulesets': true,
4142
'always-semicolon': true,
4243
'color-case': 'lower',
4344
'color-shorthand': true,

test/remove-empty-rulesets.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,97 @@ describe('options/remove-empty-rulesets', function() {
3535
assert.equal(comb.processString('a { /* comment */ }\nb {} '), 'a { /* comment */ }\n ');
3636
});
3737
});
38+
39+
describe('detecting the value', function() {
40+
// Helper to check the detection
41+
function should_detect(options, a, b) {
42+
comb.detect(options);
43+
assert.equal(
44+
JSON.stringify(comb.processString(a)),
45+
JSON.stringify(b)
46+
);
47+
}
48+
49+
it('Should detect this option set to `true`', function() {
50+
should_detect(
51+
['remove-empty-rulesets'],
52+
'a { color: red }',
53+
{
54+
'remove-empty-rulesets': true
55+
}
56+
);
57+
});
58+
59+
it('Should detect this option set to `false` with empty block', function() {
60+
should_detect(
61+
['remove-empty-rulesets'],
62+
'a {}',
63+
{
64+
'remove-empty-rulesets': false
65+
}
66+
);
67+
});
68+
69+
it('Should detect this option set to `false` with block containing whitespace', function() {
70+
should_detect(
71+
['remove-empty-rulesets'],
72+
'a { }',
73+
{
74+
'remove-empty-rulesets': false
75+
}
76+
);
77+
});
78+
79+
it('Should detect this option set to `true` with block containing comment', function() {
80+
should_detect(
81+
['remove-empty-rulesets'],
82+
'a { /* Hello */ }',
83+
{
84+
'remove-empty-rulesets': true
85+
}
86+
);
87+
});
88+
89+
it('Should detect this option set to `true` with media query containing block', function() {
90+
should_detect(
91+
['remove-empty-rulesets'],
92+
'@media all and (min-width:0) { a { /* Hello */ } }',
93+
{
94+
'remove-empty-rulesets': true
95+
}
96+
);
97+
});
98+
99+
it('Should detect this option set to `true` with media query containing comment', function() {
100+
should_detect(
101+
['remove-empty-rulesets'],
102+
'@media all and (min-width:0) {/* Hello */}',
103+
{
104+
'remove-empty-rulesets': true
105+
}
106+
);
107+
});
108+
109+
it('Should detect this option set to `false` with empty media query', function() {
110+
should_detect(
111+
['remove-empty-rulesets'],
112+
'@media all and (min-width:0) {}',
113+
{
114+
'remove-empty-rulesets': false
115+
}
116+
);
117+
});
118+
119+
it('Should detect this option set to `false` with media query containing whitespace', function() {
120+
should_detect(
121+
['remove-empty-rulesets'],
122+
'@media all and (min-width:0) { \n }',
123+
{
124+
'remove-empty-rulesets': false
125+
}
126+
);
127+
});
128+
});
38129
});
39130

40131
describe('options/remove-empty-rulesets AST manipulation', function() {

0 commit comments

Comments
 (0)