Skip to content

Commit a866e19

Browse files
DanPurdybgriffith
authored andcommitted
🐛 Fix space before bang within value node
1 parent ebc8b68 commit a866e19

7 files changed

+191
-29
lines changed

lib/rules/space-before-bang.js

+54-20
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,65 @@ module.exports = {
1010
'detect': function (ast, parser) {
1111
var result = [];
1212

13-
ast.traverseByTypes(['important', 'default'], function (block, i, parent) {
13+
ast.traverseByTypes(['important', 'default', 'value'], function (block, i, parent) {
1414
var previous = parent.content[i - 1];
15-
16-
if (!previous.is('space')) {
17-
if (parser.options.include) {
18-
result = helpers.addUnique(result, {
19-
'ruleId': parser.rule.name,
20-
'line': block.start.line,
21-
'column': block.start.column,
22-
'message': 'Whitespace required before !important',
23-
'severity': parser.severity
24-
});
15+
if (block.is('value') && block.contains('important')) {
16+
if (block.content.length === 1 && block.first('important')) {
17+
if (previous) {
18+
if (!previous.is('space')) {
19+
if (parser.options.include) {
20+
result = helpers.addUnique(result, {
21+
'ruleId': parser.rule.name,
22+
'line': block.first('important').start.line,
23+
'column': block.first('important').start.column,
24+
'message': 'Whitespace required before !important',
25+
'severity': parser.severity
26+
});
27+
}
28+
}
29+
else {
30+
if (!parser.options.include) {
31+
result = helpers.addUnique(result, {
32+
'ruleId': parser.rule.name,
33+
'line': block.first('important').start.line,
34+
'column': block.first('important').start.column,
35+
'message': 'Whitespace not allowed before !important',
36+
'severity': parser.severity
37+
});
38+
}
39+
}
40+
}
2541
}
2642
}
27-
else {
28-
if (!parser.options.include) {
29-
result = helpers.addUnique(result, {
30-
'ruleId': parser.rule.name,
31-
'line': previous.start.line,
32-
'column': previous.start.column,
33-
'message': 'Whitespace not allowed before !important',
34-
'severity': parser.severity
35-
});
43+
else
44+
if (block.is('important') || block.is('default')) {
45+
var blockString = block.is('important') ? '!important' : '!default';
46+
if (previous) {
47+
if (!previous.is('space')) {
48+
if (parser.options.include) {
49+
result = helpers.addUnique(result, {
50+
'ruleId': parser.rule.name,
51+
'line': block.start.line,
52+
'column': block.start.column,
53+
'message': 'Whitespace required before ' + blockString,
54+
'severity': parser.severity
55+
});
56+
}
57+
}
58+
else {
59+
if (!parser.options.include) {
60+
result = helpers.addUnique(result, {
61+
'ruleId': parser.rule.name,
62+
'line': previous.start.line,
63+
'column': previous.start.column,
64+
'message': 'Whitespace not allowed before ' + blockString,
65+
'severity': parser.severity
66+
});
67+
}
68+
}
3669
}
3770
}
71+
return false;
3872
});
3973

4074
return result;

tests/rules/space-after-bang.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('space after bang - scss', function () {
1212
lint.test(file, {
1313
'space-after-bang': 1
1414
}, function (data) {
15-
lint.assert.equal(7, data.warningCount);
15+
lint.assert.equal(11, data.warningCount);
1616
done();
1717
});
1818
});
@@ -26,7 +26,7 @@ describe('space after bang - scss', function () {
2626
}
2727
]
2828
}, function (data) {
29-
lint.assert.equal(7, data.warningCount);
29+
lint.assert.equal(11, data.warningCount);
3030
done();
3131
});
3232
});
@@ -42,7 +42,7 @@ describe('space after bang - sass', function () {
4242
lint.test(file, {
4343
'space-after-bang': 1
4444
}, function (data) {
45-
lint.assert.equal(7, data.warningCount);
45+
lint.assert.equal(11, data.warningCount);
4646
done();
4747
});
4848
});
@@ -56,7 +56,7 @@ describe('space after bang - sass', function () {
5656
}
5757
]
5858
}, function (data) {
59-
lint.assert.equal(7, data.warningCount);
59+
lint.assert.equal(11, data.warningCount);
6060
done();
6161
});
6262
});

tests/rules/space-before-bang.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('space before bang - scss', function () {
1212
lint.test(file, {
1313
'space-before-bang': 1
1414
}, function (data) {
15-
lint.assert.equal(4, data.warningCount);
15+
lint.assert.equal(8, data.warningCount);
1616
done();
1717
});
1818
});
@@ -26,7 +26,7 @@ describe('space before bang - scss', function () {
2626
}
2727
]
2828
}, function (data) {
29-
lint.assert.equal(4, data.warningCount);
29+
lint.assert.equal(8, data.warningCount);
3030
done();
3131
});
3232
});
@@ -42,7 +42,7 @@ describe('space before bang - sass', function () {
4242
lint.test(file, {
4343
'space-before-bang': 1
4444
}, function (data) {
45-
lint.assert.equal(4, data.warningCount);
45+
lint.assert.equal(8, data.warningCount);
4646
done();
4747
});
4848
});
@@ -56,7 +56,7 @@ describe('space before bang - sass', function () {
5656
}
5757
]
5858
}, function (data) {
59-
lint.assert.equal(4, data.warningCount);
59+
lint.assert.equal(8, data.warningCount);
6060
done();
6161
});
6262
});

tests/sass/space-after-bang.sass

+32
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,35 @@ $qux: green ! global
5050
//
5151
// .qux
5252
// @extend .notice! optional
53+
54+
=testcase($important: null)
55+
@if $important == true
56+
$important: !important
57+
58+
=testcaseFail($important: null)
59+
@if $important == true
60+
$important:!important
61+
62+
=testcase($important: null)
63+
@if $important == true
64+
$important: ! important
65+
66+
=testcaseFail($important: null)
67+
@if $important == true
68+
$important:! important
69+
70+
=testcase($important: null)
71+
@if $important == true
72+
$color: red !default
73+
74+
=testcaseFail($important: null)
75+
@if $important == true
76+
$color: red!default
77+
78+
=testcaseFail($important: null)
79+
@if $important == true
80+
$color: red ! default
81+
82+
=testcaseFail($important: null)
83+
@if $important == true
84+
$color: red! default

tests/sass/space-after-bang.scss

+32
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,35 @@ $qux: red ! global;
5454
// .qux {
5555
// @extend .notice! optional;
5656
// }
57+
58+
@mixin testcase($important: null) {
59+
@if ($important == true) {$important: !important;}
60+
}
61+
62+
@mixin testcaseFail($important: null) {
63+
@if ($important == true) {$important:!important;}
64+
}
65+
66+
@mixin testcase($important: null) {
67+
@if ($important == true) {$important: ! important;}
68+
}
69+
70+
@mixin testcaseFail($important: null) {
71+
@if ($important == true) {$important:! important;}
72+
}
73+
74+
@mixin testcase($important: null) {
75+
@if ($important == true) {$color: red !default;}
76+
}
77+
78+
@mixin testcaseFail($important: null) {
79+
@if ($important == true) {$color: red!default;}
80+
}
81+
82+
@mixin testcase($important: null) {
83+
@if ($important == true) {$color: red ! default;}
84+
}
85+
86+
@mixin testcaseFail($important: null) {
87+
@if ($important == true) {$color: red! default;}
88+
}

tests/sass/space-before-bang.sass

+32
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,35 @@ $foo: orange !default
2525
$foo: blue! default
2626

2727
$foo: green ! default
28+
29+
=testcase($important: null)
30+
@if $important == true
31+
$important: !important
32+
33+
=testcaseFail($important: null)
34+
@if $important == true
35+
$important:!important
36+
37+
=testcase($important: null)
38+
@if $important == true
39+
$important: ! important
40+
41+
=testcaseFail($important: null)
42+
@if $important == true
43+
$important:! important
44+
45+
=testcase($important: null)
46+
@if $important == true
47+
$color: red !default
48+
49+
=testcaseFail($important: null)
50+
@if $important == true
51+
$color: red!default
52+
53+
=testcaseFail($important: null)
54+
@if $important == true
55+
$color: red ! default
56+
57+
=testcaseFail($important: null)
58+
@if $important == true
59+
$color: red! default

tests/sass/space-before-bang.scss

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Important
22

33
.foo {
4-
color: red!important;
4+
color:red!important;
55
}
66

77
.bar {
@@ -25,3 +25,35 @@ $foo: red !default;
2525
$foo: red! default;
2626

2727
$foo: red ! default;
28+
29+
@mixin testcase($important: null) {
30+
@if ($important == true) {$important: !important;}
31+
}
32+
33+
@mixin testcaseFail($important: null) {
34+
@if ($important == true) {$important:!important;}
35+
}
36+
37+
@mixin testcase($important: null) {
38+
@if ($important == true) {$important: ! important;}
39+
}
40+
41+
@mixin testcaseFail($important: null) {
42+
@if ($important == true) {$important:! important;}
43+
}
44+
45+
@mixin testcase($important: null) {
46+
@if ($important == true) {$color: red !default;}
47+
}
48+
49+
@mixin testcaseFail($important: null) {
50+
@if ($important == true) {$color: red!default;}
51+
}
52+
53+
@mixin testcase($important: null) {
54+
@if ($important == true) {$color: red ! default;}
55+
}
56+
57+
@mixin testcaseFail($important: null) {
58+
@if ($important == true) {$color: red! default;}
59+
}

0 commit comments

Comments
 (0)