Skip to content

Commit c8dd880

Browse files
committed
Gonzales 3.0: Update block-indent option
1 parent 9be754b commit c8dd880

4 files changed

Lines changed: 53 additions & 35 deletions

File tree

lib/options/block-indent.js

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,50 @@
11
module.exports = (function() {
2+
var syntax;
3+
var value;
4+
25
function processStylesheet(node) {
36
var spaces;
47
var whitespaceNode;
58
var i;
69

710
for (i = node.length; i--;) {
8-
whitespaceNode = node[i];
11+
whitespaceNode = node.get(i);
912

10-
if (whitespaceNode[0] !== 's') continue;
13+
if (!whitespaceNode.is('s')) continue;
1114

12-
spaces = whitespaceNode[1].replace(/\n[ \t]+/gm, '\n');
15+
spaces = whitespaceNode.content.replace(/\n[ \t]+/gm, '\n');
1316

1417
if (spaces === '') {
15-
node.splice(i, 1);
18+
node.content.splice(i, 1);
1619
} else {
17-
whitespaceNode[1] = spaces;
20+
whitespaceNode.content = spaces;
21+
}
22+
}
23+
24+
function processBlock(x, level) {
25+
level = level || 0;
26+
27+
for (var i = 0; i < x.content.length; i++) {
28+
var n = x.get(i);
29+
if (!n) continue;
30+
31+
32+
if (syntax === 'sass' && n.is('block')) {
33+
processSassBlock(n, level, value);
34+
}
35+
36+
// Continue only with space nodes inside {...}:
37+
if (syntax !== 'sass' && level !== 0 && n.is('s')) {
38+
processSpaceNode(n, level, value);
39+
}
40+
41+
if (n.is('block') || n.is('atrulers')) level++;
42+
43+
processBlock(n, level);
1844
}
1945
}
46+
47+
processBlock(node);
2048
}
2149

2250
function processSassBlock(node, level, value) {
@@ -25,28 +53,28 @@ module.exports = (function() {
2553
var i;
2654

2755
for (i = node.length; i--;) {
28-
whitespaceNode = node[i];
56+
whitespaceNode = node.get(i);
2957

30-
if (whitespaceNode[0] !== 's') continue;
58+
if (!whitespaceNode.is('s')) continue;
3159

32-
if (whitespaceNode[1] === '\n') continue;
60+
if (whitespaceNode.content === '\n') continue;
3361

34-
spaces = whitespaceNode[1].replace(/[ \t]/gm, '');
62+
spaces = whitespaceNode.content.replace(/[ \t]/gm, '');
3563
spaces += new Array(level + 2).join(value);
36-
whitespaceNode[1] = spaces;
64+
whitespaceNode.content = spaces;
3765
}
3866
}
3967

4068
function processSpaceNode(node, level, value) {
4169
var spaces;
4270

4371
// Remove all whitespaces and tabs, leave only new lines:
44-
spaces = node[0].replace(/[ \t]/gm, '');
72+
spaces = node.content.replace(/[ \t]/gm, '');
4573

4674
if (!spaces) return;
4775

4876
spaces += new Array(level + 1).join(value);
49-
node[0] = spaces;
77+
node.content = spaces;
5078
}
5179

5280
return {
@@ -64,26 +92,16 @@ module.exports = (function() {
6492
/**
6593
* Processes tree node.
6694
*
67-
* @param {String} nodeType
6895
* @param {node} node
69-
* @param {Number} level
7096
*/
71-
process: function process(nodeType, node, level) {
72-
var syntax = this.getSyntax();
73-
var value = this.getValue('block-indent');
97+
process: function process(node) {
98+
if (!node.is('stylesheet')) return;
7499

75-
if (nodeType === 'stylesheet') {
76-
return processStylesheet(node);
77-
}
100+
syntax = this.getSyntax();
101+
value = this.getValue('block-indent');
78102

79-
if (syntax === 'sass' && nodeType === 'block') {
80-
return processSassBlock(node, level, value);
81-
}
103+
processStylesheet(node);
82104

83-
// Continue only with space nodes inside {...}:
84-
if (syntax !== 'sass' && level !== 0 && nodeType === 's') {
85-
processSpaceNode(node, level, value);
86-
}
87105
},
88106

89107
/**

test/options/block-indent-sass/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
describe.skip('options/block-indent (sass):', function() {
1+
describe('options/block-indent (sass):', function() {
22
it('First level ruleset\'s block', function() {
33
this.comb.configure({ 'block-indent': 2 });
44
this.shouldBeEqual('block.sass', 'block.expected.sass');

test/options/block-indent-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/block-indent (scss):', function() {
1+
describe('options/block-indent (scss):', function() {
22
it('Issue 213', function() {
33
this.comb.configure({ 'block-indent': 2 });
44
this.shouldBeEqual('nested-include.scss', 'nested-include.expected.scss');

test/options/block-indent/test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
describe.skip('options/block-indent:', function() {
1+
describe('options/block-indent:', function() {
22
it('Array value => should not change anything', function() {
33
this.comb.configure({ 'block-indent': ['', ' '] });
44
this.shouldBeEqual('test.css');
@@ -24,38 +24,38 @@ describe.skip('options/block-indent:', function() {
2424
this.shouldBeEqual('test.css', 'test-2.expected.css');
2525
});
2626

27-
it('Should detect nothing with an empty block, test 1', function() {
27+
it.skip('Should detect nothing with an empty block, test 1', function() {
2828
this.shouldDetect(
2929
['block-indent'],
3030
'a{ }',
3131
{}
3232
);
3333
});
3434

35-
it('Should detect nothing with an empty block, test 2', function() {
35+
it.skip('Should detect nothing with an empty block, test 2', function() {
3636
this.shouldDetect(
3737
['block-indent'],
3838
'a{}',
3939
{}
4040
);
4141
});
4242

43-
it('Should detect correct number of spaces', function() {
43+
it.skip('Should detect correct number of spaces', function() {
4444
this.shouldDetect(
4545
['block-indent'],
4646
'a{\n top: 0;\n color: tomato;\n}',
4747
{ 'block-indent': ' ' }
4848
);
4949
});
5050

51-
it('Should detect no indent for one-line code', function() {
51+
it.skip('Should detect no indent for one-line code', function() {
5252
this.shouldDetect(
5353
['block-indent'],
5454
'a{ top: 0; color: tomato; }',
5555
{}
5656
);
5757
});
58-
it('Valid string value => should set proper space after combnator', function() {
58+
it.skip('Valid string value => should set proper space after combnator', function() {
5959
this.comb.configure({ 'block-indent': ' ', 'space-before-closing-brace': '\n' });
6060
this.shouldBeEqual('test.css', 'test-3.expected.css');
6161
});

0 commit comments

Comments
 (0)