From a866e199da8c2d202685379b17f7766a637e463d Mon Sep 17 00:00:00 2001 From: Dan Purdy Date: Sun, 27 Aug 2017 14:52:36 +0100 Subject: [PATCH] :bug: Fix space before bang within value node --- lib/rules/space-before-bang.js | 74 ++++++++++++++++++++++--------- tests/rules/space-after-bang.js | 8 ++-- tests/rules/space-before-bang.js | 8 ++-- tests/sass/space-after-bang.sass | 32 +++++++++++++ tests/sass/space-after-bang.scss | 32 +++++++++++++ tests/sass/space-before-bang.sass | 32 +++++++++++++ tests/sass/space-before-bang.scss | 34 +++++++++++++- 7 files changed, 191 insertions(+), 29 deletions(-) diff --git a/lib/rules/space-before-bang.js b/lib/rules/space-before-bang.js index 065d1ab8..c0cff586 100644 --- a/lib/rules/space-before-bang.js +++ b/lib/rules/space-before-bang.js @@ -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; diff --git a/tests/rules/space-after-bang.js b/tests/rules/space-after-bang.js index 2b340d69..7747d640 100644 --- a/tests/rules/space-after-bang.js +++ b/tests/rules/space-after-bang.js @@ -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(); }); }); @@ -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(); }); }); @@ -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(); }); }); @@ -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(); }); }); diff --git a/tests/rules/space-before-bang.js b/tests/rules/space-before-bang.js index ec17a9a1..6810ef2e 100644 --- a/tests/rules/space-before-bang.js +++ b/tests/rules/space-before-bang.js @@ -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(); }); }); @@ -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(); }); }); @@ -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(); }); }); @@ -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(); }); }); diff --git a/tests/sass/space-after-bang.sass b/tests/sass/space-after-bang.sass index d247d047..2fed1ee6 100644 --- a/tests/sass/space-after-bang.sass +++ b/tests/sass/space-after-bang.sass @@ -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 diff --git a/tests/sass/space-after-bang.scss b/tests/sass/space-after-bang.scss index a6d890be..86acb723 100644 --- a/tests/sass/space-after-bang.scss +++ b/tests/sass/space-after-bang.scss @@ -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;} +} diff --git a/tests/sass/space-before-bang.sass b/tests/sass/space-before-bang.sass index 70a832d9..8301efce 100644 --- a/tests/sass/space-before-bang.sass +++ b/tests/sass/space-before-bang.sass @@ -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 diff --git a/tests/sass/space-before-bang.scss b/tests/sass/space-before-bang.scss index 17610d6d..db491a81 100644 --- a/tests/sass/space-before-bang.scss +++ b/tests/sass/space-before-bang.scss @@ -1,7 +1,7 @@ // Important .foo { - color: red!important; + color:red!important; } .bar { @@ -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;} +}