Skip to content

Commit

Permalink
🐛 Fix issue with nested values space-before-brace
Browse files Browse the repository at this point in the history
  • Loading branch information
DanPurdy committed Oct 27, 2015
1 parent 9a094af commit 1d230cc
Showing 1 changed file with 50 additions and 26 deletions.
76 changes: 50 additions & 26 deletions lib/rules/space-before-brace.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
var helpers = require('../helpers');

var getLastWhitespace = function (node) {
if (node === false) {
return null;
}

if (typeof node !== 'object') {
return false;
}
Expand All @@ -20,35 +24,55 @@ module.exports = {
},
'detect': function (ast, parser) {
var result = [];
if (ast.syntax === 'scss') {
ast.traverseByTypes(['block', 'atrulers', 'declaration'], function (block, i, parent) {
var previous = false,
whitespace,
warn = {};

ast.traverseByTypes(['block', 'atrulers'], function (block, i, parent) {
var previous = parent.get(i - 1),
whitespace = getLastWhitespace(previous);

if (whitespace === false) {
if (parser.options.include) {
result = helpers.addUnique(result, {
'ruleId': parser.rule.name,
'line': block.start.line,
'column': block.start.column - 1,
'message': 'Whitespace required before {',
'severity': parser.severity
});
if ((block.is('block') || block.is('atrulers')) && !parent.is('value')) {
previous = parent.get(i - 1);
}
}
else {
if (!parser.options.include) {
result = helpers.addUnique(result, {
'ruleId': parser.rule.name,
'line': whitespace.start.line,
'column': whitespace.start.column,
'message': 'Whitespace not allowed before {',
'severity': parser.severity
});
else if (block.is('declaration')) {
if (block.contains('value')) {
for (var j = 0; j < block.content.length; j++) {
if (block.content[j].is('value') && block.content[j].content[0].is('block')) {
previous = block.content[j - 1];
warn.line = block.content[j].content[0].start.line;
warn.col = block.content[j].content[0].start.column;
}
}
}
}
}
});

whitespace = getLastWhitespace(previous);
if (whitespace === false) {
if (parser.options.include) {
if (!warn.hasOwnProperty('line')) {
warn.line = block.start.line;
warn.col = block.start.column;
}
result = helpers.addUnique(result, {
'ruleId': parser.rule.name,
'line': warn.line,
'column': warn.col - 1,
'message': 'Whitespace required before {',
'severity': parser.severity
});
}
}
else {
if (!parser.options.include && whitespace !== null) {
result = helpers.addUnique(result, {
'ruleId': parser.rule.name,
'line': whitespace.start.line,
'column': whitespace.start.column,
'message': 'Whitespace not allowed before {',
'severity': parser.severity
});
}
}
});
}
return result;
}
};

0 comments on commit 1d230cc

Please sign in to comment.