Skip to content

Commit 2ece912

Browse files
committed
Gonzales 3.0: Update space-before-closing-brace
1 parent a25249a commit 2ece912

File tree

2 files changed

+49
-28
lines changed

2 files changed

+49
-28
lines changed

lib/options/space-before-closing-brace.js

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
var gonzales = require('gonzales-pe');
2+
13
module.exports = (function() {
24
function getLastWhitespaceNode(node) {
3-
var lastNode = node[node.length - 1];
5+
var lastNode = node.last();
6+
7+
if (!lastNode || !lastNode.content) return null;
48

5-
if (typeof lastNode !== 'object' || lastNode[0] === 'block') return null;
6-
if (lastNode[0] === 's') return lastNode;
9+
if (lastNode.is('block')) return null;
10+
if (lastNode.is('s')) return lastNode;
711

812
return getLastWhitespaceNode(lastNode);
913
}
@@ -22,33 +26,50 @@ module.exports = (function() {
2226

2327
/**
2428
* Processes tree node.
25-
* @param {String} nodeType
2629
* @param {node} node
27-
* @param {Number} level
2830
*/
29-
process: function(nodeType, node, level) {
30-
if (nodeType !== 'block' && nodeType !== 'atrulers') return;
31-
31+
process: function(node) {
3232
var value = this.getValue('space-before-closing-brace');
33+
var blockIndent = this.getValue('block-indent');
3334

34-
// If found block node stop at the next one for space check
35-
// For the pre-block node, find its last (the deepest) child
36-
var whitespaceNode = getLastWhitespaceNode(node);
35+
if (!node.is('stylesheet')) return;
3736

38-
if (value.indexOf('\n') > -1) {
39-
var blockIndent = this.getValue('block-indent');
40-
// TODO: Check that it works for '' block indent value <tg>
41-
if (blockIndent) value += new Array(level + 1).join(blockIndent);
42-
}
37+
function processBlock(x, level) {
38+
level = level || 0;
39+
40+
for (var i = 0; i < x.content.length; i++) {
41+
node = x.get(i);
42+
if (!node) continue;
4343

44-
// If it's spaces, modify this node
45-
// If it's something different from spaces, add a space node to the end
44+
if (node.is('block') || node.is('atrulers')) {
45+
// If found block node stop at the next one for space check
46+
// For the pre-block node, find its last (the deepest) child
47+
var whitespaceNode = getLastWhitespaceNode(node);
4648

47-
if (whitespaceNode) {
48-
whitespaceNode[1] = value;
49-
} else if (value !== '') {
50-
node.push(['s', value]);
49+
if (value.indexOf('\n') > -1) {
50+
// TODO: Check that it works for '' block indent value <tg>
51+
if (blockIndent) value += new Array(level + 1).join(blockIndent);
52+
}
53+
54+
// If it's spaces, modify this node
55+
// If it's something different from spaces, add a space node to the end
56+
57+
if (whitespaceNode) {
58+
whitespaceNode.content = value;
59+
} else if (value !== '') {
60+
var space = gonzales.createNode({ type: 's', content: value });
61+
if (Array.isArray(node.content))
62+
node.content.push(space);
63+
}
64+
65+
level++;
66+
}
67+
68+
processBlock(node, level);
69+
}
5170
}
71+
72+
processBlock(node);
5273
},
5374

5475
/**

test/options/space-before-closing-brace/test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
describe.skip('options/space-before-closing-brace:', function() {
1+
describe('options/space-before-closing-brace:', function() {
22
it('Array value => should not change anything', function() {
33
this.comb.configure({ 'space-before-closing-brace': ['', ' '] });
44
this.shouldBeEqual('test.css');
@@ -29,39 +29,39 @@ describe.skip('options/space-before-closing-brace:', function() {
2929
this.shouldBeEqual('test.css', 'test-3.expected.css');
3030
});
3131

32-
it('Should detect no whitespace', function() {
32+
it.skip('Should detect no whitespace', function() {
3333
this.shouldDetect(
3434
['space-before-closing-brace'],
3535
'a{top:0}',
3636
{ 'space-before-closing-brace': '' }
3737
);
3838
});
3939

40-
it('Should detect no whitespace (2 blocks)', function() {
40+
it.skip('Should detect no whitespace (2 blocks)', function() {
4141
this.shouldDetect(
4242
['space-before-closing-brace'],
4343
'a{top:0} b { color: tomato; }',
4444
{ 'space-before-closing-brace': '' }
4545
);
4646
});
4747

48-
it('Should detect whitespace', function() {
48+
it.skip('Should detect whitespace', function() {
4949
this.shouldDetect(
5050
['space-before-closing-brace'],
5151
'a { top:0 }',
5252
{ 'space-before-closing-brace': ' ' }
5353
);
5454
});
5555

56-
it('Should detect whitespace (2 blocks)', function() {
56+
it.skip('Should detect whitespace (2 blocks)', function() {
5757
this.shouldDetect(
5858
['space-before-closing-brace'],
5959
'a { top:0 } b{color:tomato;}',
6060
{ 'space-before-closing-brace': ' ' }
6161
);
6262
});
6363

64-
it('Should detect whitespace (mix with block indent)', function() {
64+
it.skip('Should detect whitespace (mix with block indent)', function() {
6565
this.shouldDetect(
6666
['space-before-closing-brace', 'block-indent'],
6767
'a {\n top:0\n }\nb{\n color:tomato;\n }',

0 commit comments

Comments
 (0)