Skip to content

Commit a1bcd5f

Browse files
committed
Gonzales 3.0: Update space-before-opening-brace
1 parent 7b08500 commit a1bcd5f

3 files changed

Lines changed: 27 additions & 18 deletions

File tree

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var gonzales = require('gonzales-pe');
2+
13
module.exports = (function() {
24
/**
35
* Gets the last (the deepest) whitespace node.
@@ -8,9 +10,9 @@ module.exports = (function() {
810
*/
911
function getLastWhitespaceNode(node) {
1012
if (typeof node !== 'object') return;
11-
if (node[0] === 's') return node;
13+
if (node.is('s')) return node;
1214

13-
return getLastWhitespaceNode(node[node.length - 1]);
15+
return getLastWhitespaceNode(node.last());
1416
}
1517

1618
return {
@@ -28,31 +30,38 @@ module.exports = (function() {
2830
/**
2931
* Processes tree node.
3032
*
31-
* @param {String} nodeType
3233
* @param {node} node
3334
*/
34-
process: function(nodeType, node) {
35+
process: function(node) {
3536
var value = this.getValue('space-before-opening-brace');
3637

3738
// Loop through node from the end to the beginning:
3839
for (var i = node.length; i--;) {
40+
if (!node.get(i) || typeof node.get(i) === 'string') continue;
41+
42+
try {
43+
node.get(i).is('s');
44+
} catch (e) {
45+
console.log(node);
46+
}
3947
// If found block node stop at the next one for space check:
40-
if (node[i][0] !== 'block' && node[i][0] !== 'atrulers') continue;
48+
if (!node.get(i).is('block') && !node.get(i).is('atrulers')) continue;
4149

4250
// For the pre-block node, find its last (the deepest) child:
4351
// TODO: Exclude nodes with braces (for example, arguments)
44-
var whitespaceNode = getLastWhitespaceNode(node[i - 1]);
52+
var whitespaceNode = getLastWhitespaceNode(node.get(i - 1));
4553

4654
// If it's spaces, modify this node.
4755
// If it's something different from spaces, add a space node to
4856
// the end:
4957
if (whitespaceNode) {
50-
whitespaceNode[1] = value;
58+
whitespaceNode.content = value;
5159
} else if (value !== '') {
52-
if (node[i - 1][0] === 'atrulerq') {
53-
node[i - 1].push(['s', value]);
60+
var space = gonzales.createNode({ type: 's', content: value });
61+
if (node.get(i - 1).is('atrulerq')) {
62+
node.get(i - 1).content.push(space);
5463
} else {
55-
node.splice(i, 0, ['s', value]);
64+
node.insert(i, space);
5665
}
5766
}
5867
}

test/options/space-before-opening-brace-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/space-before-opening-brace (scss):', function() {
1+
describe('options/space-before-opening-brace (scss):', function() {
22
it('Issue 231', function() {
33
this.comb.configure({ 'space-before-opening-brace': 1 });
44
this.shouldBeEqual('issue-231.scss', 'issue-231.expected.scss');

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
describe.skip('options/space-before-opening-brace:', function() {
1+
describe('options/space-before-opening-brace:', function() {
22
it('Array value => should not change anything', function() {
33
this.comb.configure({ 'space-before-opening-brace': ['', ' '] });
44
this.shouldBeEqual('test.css');
@@ -34,47 +34,47 @@ describe.skip('options/space-before-opening-brace:', function() {
3434
this.shouldBeEqual('issue-232.css', 'issue-232.expected.css');
3535
});
3636

37-
it('Should detect no whitespace', function() {
37+
it.skip('Should detect no whitespace', function() {
3838
this.shouldDetect(
3939
['space-before-opening-brace'],
4040
'a{top:0}',
4141
{ 'space-before-opening-brace': '' }
4242
);
4343
});
4444

45-
it('Should detect whitespace', function() {
45+
it.skip('Should detect whitespace', function() {
4646
this.shouldDetect(
4747
['space-before-opening-brace'],
4848
'a \n {top:0}',
4949
{ 'space-before-opening-brace': ' \n ' }
5050
);
5151
});
5252

53-
it('Should detect no whitespace (2 blocks)', function() {
53+
it.skip('Should detect no whitespace (2 blocks)', function() {
5454
this.shouldDetect(
5555
['space-before-opening-brace'],
5656
'a{top:0} b {left:0}',
5757
{ 'space-before-opening-brace': '' }
5858
);
5959
});
6060

61-
it('Should detect whitespace (2 blocks)', function() {
61+
it.skip('Should detect whitespace (2 blocks)', function() {
6262
this.shouldDetect(
6363
['space-before-opening-brace'],
6464
'a {top:0} b{left:0}',
6565
{ 'space-before-opening-brace': ' ' }
6666
);
6767
});
6868

69-
it('Should detect no whitespace (3 blocks)', function() {
69+
it.skip('Should detect no whitespace (3 blocks)', function() {
7070
this.shouldDetect(
7171
['space-before-opening-brace'],
7272
'a {top:0} b{left:0} c{right:0}',
7373
{ 'space-before-opening-brace': '' }
7474
);
7575
});
7676

77-
it('Should detect whitespace (3 blocks)', function() {
77+
it.skip('Should detect whitespace (3 blocks)', function() {
7878
this.shouldDetect(
7979
['space-before-opening-brace'],
8080
'a{top:0} b {left:0} c {right:0}',

0 commit comments

Comments
 (0)