Skip to content

Commit

Permalink
Merge pull request #361 from DanPurdy/hotfix/space-before-brace
Browse files Browse the repository at this point in the history
Hotfix master: Fix space before brace rule for nested properties
  • Loading branch information
bgriffith committed Oct 28, 2015
2 parents 837736f + 59142e1 commit d705fe5
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 28 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;
}
};
4 changes: 2 additions & 2 deletions tests/rules/space-before-brace.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('space before brace - scss', function () {
lint.test(file, {
'space-before-brace': 1
}, function (data) {
lint.assert.equal(3, data.warningCount);
lint.assert.equal(5, data.warningCount);
done();
});
});
Expand All @@ -26,7 +26,7 @@ describe('space before brace - scss', function () {
}
]
}, function (data) {
lint.assert.equal(4, data.warningCount);
lint.assert.equal(11, data.warningCount);
done();
});
});
Expand Down
30 changes: 30 additions & 0 deletions tests/sass/space-before-brace.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,37 @@

.qux {
content: 'bar';
font:{
family: fantasy;
}
@include breakpoint{
content: bar;
font: {
family: fantasy;
}
}
}


.other {
content: 'bar';
font: {
family: fantasy;
}
@include breakpoint {
content: bar;
font:{
family: fantasy;
}
}
}

.selector {
content: '';

.mergeable {
font: {
family: fantasy;
}
}
}

0 comments on commit d705fe5

Please sign in to comment.