Skip to content

Commit 21b4be9

Browse files
committed
🐛 Fix issue with nested values space-before-brace
1 parent 46592df commit 21b4be9

File tree

1 file changed

+50
-26
lines changed

1 file changed

+50
-26
lines changed

lib/rules/space-before-brace.js

+50-26
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
var helpers = require('../helpers');
44

55
var getLastWhitespace = function (node) {
6+
if (node === false) {
7+
return null;
8+
}
9+
610
if (typeof node !== 'object') {
711
return false;
812
}
@@ -20,35 +24,55 @@ module.exports = {
2024
},
2125
'detect': function (ast, parser) {
2226
var result = [];
27+
if (ast.syntax === 'scss') {
28+
ast.traverseByTypes(['block', 'atrulers', 'declaration'], function (block, i, parent) {
29+
var previous = false,
30+
whitespace,
31+
warn = {};
2332

24-
ast.traverseByTypes(['block', 'atrulers'], function (block, i, parent) {
25-
var previous = parent.get(i - 1),
26-
whitespace = getLastWhitespace(previous);
27-
28-
if (whitespace === false) {
29-
if (parser.options.include) {
30-
result = helpers.addUnique(result, {
31-
'ruleId': parser.rule.name,
32-
'line': block.start.line,
33-
'column': block.start.column - 1,
34-
'message': 'Whitespace required before {',
35-
'severity': parser.severity
36-
});
33+
if ((block.is('block') || block.is('atrulers')) && !parent.is('value')) {
34+
previous = parent.get(i - 1);
3735
}
38-
}
39-
else {
40-
if (!parser.options.include) {
41-
result = helpers.addUnique(result, {
42-
'ruleId': parser.rule.name,
43-
'line': whitespace.start.line,
44-
'column': whitespace.start.column,
45-
'message': 'Whitespace not allowed before {',
46-
'severity': parser.severity
47-
});
36+
else if (block.is('declaration')) {
37+
if (block.contains('value')) {
38+
for (var j = 0; j < block.content.length; j++) {
39+
if (block.content[j].is('value') && block.content[j].content[0].is('block')) {
40+
previous = block.content[j - 1];
41+
warn.line = block.content[j].content[0].start.line;
42+
warn.col = block.content[j].content[0].start.column;
43+
}
44+
}
45+
}
4846
}
49-
}
50-
});
51-
47+
whitespace = getLastWhitespace(previous);
48+
if (whitespace === false) {
49+
if (parser.options.include) {
50+
if (!warn.hasOwnProperty('line')) {
51+
warn.line = block.start.line;
52+
warn.col = block.start.column;
53+
}
54+
result = helpers.addUnique(result, {
55+
'ruleId': parser.rule.name,
56+
'line': warn.line,
57+
'column': warn.col - 1,
58+
'message': 'Whitespace required before {',
59+
'severity': parser.severity
60+
});
61+
}
62+
}
63+
else {
64+
if (!parser.options.include && whitespace !== null) {
65+
result = helpers.addUnique(result, {
66+
'ruleId': parser.rule.name,
67+
'line': whitespace.start.line,
68+
'column': whitespace.start.column,
69+
'message': 'Whitespace not allowed before {',
70+
'severity': parser.severity
71+
});
72+
}
73+
}
74+
});
75+
}
5276
return result;
5377
}
5478
};

0 commit comments

Comments
 (0)