Skip to content

Commit

Permalink
🐛 Fix space before bang within value node
Browse files Browse the repository at this point in the history
  • Loading branch information
DanPurdy authored and bgriffith committed Aug 27, 2017
1 parent ebc8b68 commit a866e19
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 29 deletions.
74 changes: 54 additions & 20 deletions lib/rules/space-before-bang.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,65 @@ module.exports = {
'detect': function (ast, parser) {
var result = [];

ast.traverseByTypes(['important', 'default'], function (block, i, parent) {
ast.traverseByTypes(['important', 'default', 'value'], function (block, i, parent) {
var previous = parent.content[i - 1];

if (!previous.is('space')) {
if (parser.options.include) {
result = helpers.addUnique(result, {
'ruleId': parser.rule.name,
'line': block.start.line,
'column': block.start.column,
'message': 'Whitespace required before !important',
'severity': parser.severity
});
if (block.is('value') && block.contains('important')) {
if (block.content.length === 1 && block.first('important')) {
if (previous) {
if (!previous.is('space')) {
if (parser.options.include) {
result = helpers.addUnique(result, {
'ruleId': parser.rule.name,
'line': block.first('important').start.line,
'column': block.first('important').start.column,
'message': 'Whitespace required before !important',
'severity': parser.severity
});
}
}
else {
if (!parser.options.include) {
result = helpers.addUnique(result, {
'ruleId': parser.rule.name,
'line': block.first('important').start.line,
'column': block.first('important').start.column,
'message': 'Whitespace not allowed before !important',
'severity': parser.severity
});
}
}
}
}
}
else {
if (!parser.options.include) {
result = helpers.addUnique(result, {
'ruleId': parser.rule.name,
'line': previous.start.line,
'column': previous.start.column,
'message': 'Whitespace not allowed before !important',
'severity': parser.severity
});
else
if (block.is('important') || block.is('default')) {
var blockString = block.is('important') ? '!important' : '!default';
if (previous) {
if (!previous.is('space')) {
if (parser.options.include) {
result = helpers.addUnique(result, {
'ruleId': parser.rule.name,
'line': block.start.line,
'column': block.start.column,
'message': 'Whitespace required before ' + blockString,
'severity': parser.severity
});
}
}
else {
if (!parser.options.include) {
result = helpers.addUnique(result, {
'ruleId': parser.rule.name,
'line': previous.start.line,
'column': previous.start.column,
'message': 'Whitespace not allowed before ' + blockString,
'severity': parser.severity
});
}
}
}
}
return false;
});

return result;
Expand Down
8 changes: 4 additions & 4 deletions tests/rules/space-after-bang.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('space after bang - scss', function () {
lint.test(file, {
'space-after-bang': 1
}, function (data) {
lint.assert.equal(7, data.warningCount);
lint.assert.equal(11, data.warningCount);
done();
});
});
Expand All @@ -26,7 +26,7 @@ describe('space after bang - scss', function () {
}
]
}, function (data) {
lint.assert.equal(7, data.warningCount);
lint.assert.equal(11, data.warningCount);
done();
});
});
Expand All @@ -42,7 +42,7 @@ describe('space after bang - sass', function () {
lint.test(file, {
'space-after-bang': 1
}, function (data) {
lint.assert.equal(7, data.warningCount);
lint.assert.equal(11, data.warningCount);
done();
});
});
Expand All @@ -56,7 +56,7 @@ describe('space after bang - sass', function () {
}
]
}, function (data) {
lint.assert.equal(7, data.warningCount);
lint.assert.equal(11, data.warningCount);
done();
});
});
Expand Down
8 changes: 4 additions & 4 deletions tests/rules/space-before-bang.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('space before bang - scss', function () {
lint.test(file, {
'space-before-bang': 1
}, function (data) {
lint.assert.equal(4, data.warningCount);
lint.assert.equal(8, data.warningCount);
done();
});
});
Expand All @@ -26,7 +26,7 @@ describe('space before bang - scss', function () {
}
]
}, function (data) {
lint.assert.equal(4, data.warningCount);
lint.assert.equal(8, data.warningCount);
done();
});
});
Expand All @@ -42,7 +42,7 @@ describe('space before bang - sass', function () {
lint.test(file, {
'space-before-bang': 1
}, function (data) {
lint.assert.equal(4, data.warningCount);
lint.assert.equal(8, data.warningCount);
done();
});
});
Expand All @@ -56,7 +56,7 @@ describe('space before bang - sass', function () {
}
]
}, function (data) {
lint.assert.equal(4, data.warningCount);
lint.assert.equal(8, data.warningCount);
done();
});
});
Expand Down
32 changes: 32 additions & 0 deletions tests/sass/space-after-bang.sass
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,35 @@ $qux: green ! global
//
// .qux
// @extend .notice! optional
=testcase($important: null)
@if $important == true
$important: !important

=testcaseFail($important: null)
@if $important == true
$important:!important

=testcase($important: null)
@if $important == true
$important: ! important

=testcaseFail($important: null)
@if $important == true
$important:! important

=testcase($important: null)
@if $important == true
$color: red !default

=testcaseFail($important: null)
@if $important == true
$color: red!default

=testcaseFail($important: null)
@if $important == true
$color: red ! default

=testcaseFail($important: null)
@if $important == true
$color: red! default
32 changes: 32 additions & 0 deletions tests/sass/space-after-bang.scss
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,35 @@ $qux: red ! global;
// .qux {
// @extend .notice! optional;
// }

@mixin testcase($important: null) {
@if ($important == true) {$important: !important;}
}

@mixin testcaseFail($important: null) {
@if ($important == true) {$important:!important;}
}

@mixin testcase($important: null) {
@if ($important == true) {$important: ! important;}
}

@mixin testcaseFail($important: null) {
@if ($important == true) {$important:! important;}
}

@mixin testcase($important: null) {
@if ($important == true) {$color: red !default;}
}

@mixin testcaseFail($important: null) {
@if ($important == true) {$color: red!default;}
}

@mixin testcase($important: null) {
@if ($important == true) {$color: red ! default;}
}

@mixin testcaseFail($important: null) {
@if ($important == true) {$color: red! default;}
}
32 changes: 32 additions & 0 deletions tests/sass/space-before-bang.sass
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,35 @@ $foo: orange !default
$foo: blue! default

$foo: green ! default

=testcase($important: null)
@if $important == true
$important: !important

=testcaseFail($important: null)
@if $important == true
$important:!important

=testcase($important: null)
@if $important == true
$important: ! important

=testcaseFail($important: null)
@if $important == true
$important:! important

=testcase($important: null)
@if $important == true
$color: red !default

=testcaseFail($important: null)
@if $important == true
$color: red!default

=testcaseFail($important: null)
@if $important == true
$color: red ! default

=testcaseFail($important: null)
@if $important == true
$color: red! default
34 changes: 33 additions & 1 deletion tests/sass/space-before-bang.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Important

.foo {
color: red!important;
color:red!important;
}

.bar {
Expand All @@ -25,3 +25,35 @@ $foo: red !default;
$foo: red! default;

$foo: red ! default;

@mixin testcase($important: null) {
@if ($important == true) {$important: !important;}
}

@mixin testcaseFail($important: null) {
@if ($important == true) {$important:!important;}
}

@mixin testcase($important: null) {
@if ($important == true) {$important: ! important;}
}

@mixin testcaseFail($important: null) {
@if ($important == true) {$important:! important;}
}

@mixin testcase($important: null) {
@if ($important == true) {$color: red !default;}
}

@mixin testcaseFail($important: null) {
@if ($important == true) {$color: red!default;}
}

@mixin testcase($important: null) {
@if ($important == true) {$color: red ! default;}
}

@mixin testcaseFail($important: null) {
@if ($important == true) {$color: red! default;}
}

0 comments on commit a866e19

Please sign in to comment.