Skip to content

Commit 5d0ce4b

Browse files
committed
Gonzales 3.0: Update remove-empty-rulesets
1 parent 5c56ec0 commit 5d0ce4b

File tree

4 files changed

+34
-38
lines changed

4 files changed

+34
-38
lines changed

lib/options/remove-empty-rulesets.js

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
11
module.exports = (function() {
2-
function processStylesheetContent(nodeContent) {
3-
removeEmptyRulesets(nodeContent);
4-
mergeAdjacentWhitespace(nodeContent);
2+
function processStylesheetContent(node) {
3+
removeEmptyRulesets(node);
4+
mergeAdjacentWhitespace(node);
55
}
66

7-
function removeEmptyRulesets(nodeContent) {
8-
var i = nodeContent.length;
7+
function removeEmptyRulesets(stylesheet) {
8+
var i = stylesheet.content.length;
99
// Loop through node and try to find a ruleset:
1010
while (i--) {
11-
var node = nodeContent[i];
11+
var node = stylesheet.content[i];
1212
if (!isRuleset(node)) continue;
1313
// If a ruleset is found, try to find its nested rulesets and remove
1414
// all empty ones:
15-
var j = node.length;
15+
var j = node.content.length;
1616
while (j--) {
1717
// Nested rulesets are located inside blocks, that's why look
1818
// for blocks only:
19-
var blockNode = node[j];
20-
if (blockNode[0] !== 'block') continue;
21-
blockNode.shift();
19+
var blockNode = node.content[j];
20+
if (blockNode.type !== 'block') continue;
2221
processStylesheetContent(blockNode);
23-
blockNode.unshift('block');
24-
node[j] = blockNode;
22+
node.content[j] = blockNode;
2523
}
2624
// If after removing all empty nested rulesets the parent has also
2725
// become empty, remove it too:
2826
if (isEmptyRuleset(node)) {
29-
nodeContent.splice(i, 1);
27+
stylesheet.content.splice(i, 1);
3028
}
3129
}
3230
}
@@ -37,42 +35,41 @@ module.exports = (function() {
3735
* To ensure correctness of further processing we should merge such nodes into one.
3836
* [space, space] => [space]
3937
*/
40-
function mergeAdjacentWhitespace(nodeContent) {
41-
var i = nodeContent.length - 1;
38+
function mergeAdjacentWhitespace(node) {
39+
var i = node.content.length - 1;
4240
while (i-- > 0) {
43-
if (isWhitespace(nodeContent[i]) && isWhitespace(nodeContent[i + 1])) {
44-
nodeContent[i][1] += nodeContent[i + 1][1];
45-
nodeContent.splice(i + 1, 1);
41+
if (isWhitespace(node.content[i]) && isWhitespace(node.content[i + 1])) {
42+
node.content[i].content += node.content[i + 1].content;
43+
node.content.splice(i + 1, 1);
4644
}
4745
}
4846
}
4947

5048
function isEmptyRuleset(ruleset) {
51-
return ruleset.filter(isBlock).every(isEmptyBlock, this);
49+
return isEmptyBlock(ruleset.first('block'));
5250
}
5351

5452
/**
5553
* Block is considered empty when it has nothing but spaces.
5654
*/
5755
function isEmptyBlock(node) {
58-
return node.length === 1 || !node.some(isNotWhitespace);
59-
}
56+
if (!node.content.length) return true;
6057

61-
function isRuleset(node) {
62-
return node[0] === 'ruleset';
58+
var isEmpty = true;
59+
node.forEach(function(childNode) {
60+
if (childNode.type !== 's') isEmpty = false;
61+
});
62+
return isEmpty;
6363
}
6464

65-
function isBlock(node) {
66-
return node[0] === 'block';
65+
function isRuleset(node) {
66+
return node.type === 'ruleset';
6767
}
6868

6969
function isWhitespace(node) {
70-
return node[0] === 's';
70+
return node.type === 's';
7171
}
7272

73-
function isNotWhitespace(node) {
74-
return typeof node === 'object' && node[0] !== 's';
75-
}
7673

7774
return {
7875
name: 'remove-empty-rulesets',
@@ -86,12 +83,11 @@ module.exports = (function() {
8683
/**
8784
* Remove rulesets with no declarations.
8885
*
89-
* @param {String} nodeType
90-
* @param {Array} nodeContent
86+
* @param {String} node
9187
*/
92-
process: function(nodeType, nodeContent) {
93-
if (nodeType === 'stylesheet') {
94-
processStylesheetContent(nodeContent);
88+
process: function(node) {
89+
if (node.type === 'stylesheet') {
90+
processStylesheetContent(node);
9591
}
9692
},
9793

test/options/remove-empty-rulesets-less/test.js

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

3-
describe.skip('options/remove-empty-rulesets (less):', function() {
3+
describe('options/remove-empty-rulesets (less):', function() {
44
it('Issue 201. Test 1', function() {
55
this.comb.configure({ 'remove-empty-rulesets': true });
66
this.shouldBeEqual('1.less', '1.expected.less');

test/options/remove-empty-rulesets-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/remove-empty-rulesets (scss)', function() {
1+
describe('options/remove-empty-rulesets (scss)', function() {
22
beforeEach(function() {
33
this.comb.configure({ 'remove-empty-rulesets': true });
44
});

test/options/remove-empty-rulesets/test.js

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

3-
describe.skip('options/remove-empty-rulesets', function() {
3+
describe('options/remove-empty-rulesets', function() {
44
it('Configured with invalid value, should not remove empty ruleset', function() {
55
this.comb.configure({ 'remove-empty-rulesets': 'foobar' });
66
assert.equal(this.comb.processString('a { width: 10px; } b {}'), 'a { width: 10px; } b {}');
@@ -28,7 +28,7 @@ describe.skip('options/remove-empty-rulesets', function() {
2828
});
2929
});
3030

31-
describe('detecting the value', function() {
31+
describe.skip('detecting the value', function() {
3232
it('Should detect this option set to `true`', function() {
3333
this.shouldDetect(
3434
['remove-empty-rulesets'],

0 commit comments

Comments
 (0)