From 27c430aaf004a95fcc5db9d6fde27bdd10c478ba Mon Sep 17 00:00:00 2001 From: Dan Purdy Date: Wed, 22 Jun 2016 20:10:23 +0100 Subject: [PATCH 1/4] :white_check_mark: Add no-color-hex tests --- tests/rules/no-color-hex.js | 35 +++++++++++++++++++++++++++++++++++ tests/sass/no-color-hex.sass | 25 +++++++++++++++++++++++++ tests/sass/no-color-hex.scss | 26 ++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 tests/rules/no-color-hex.js create mode 100644 tests/sass/no-color-hex.sass create mode 100644 tests/sass/no-color-hex.scss diff --git a/tests/rules/no-color-hex.js b/tests/rules/no-color-hex.js new file mode 100644 index 00000000..2cfe53e5 --- /dev/null +++ b/tests/rules/no-color-hex.js @@ -0,0 +1,35 @@ +'use strict'; + +var lint = require('./_lint'); + +////////////////////////////// +// SCSS syntax tests +////////////////////////////// +describe('no color hex - scss', function () { + var file = lint.file('no-color-hex.scss'); + + it('enforce', function (done) { + lint.test(file, { + 'no-color-hex': 1 + }, function (data) { + lint.assert.equal(9, data.warningCount); + done(); + }); + }); +}); + +////////////////////////////// +// Sass syntax tests +////////////////////////////// +describe('no color hex - sass', function () { + var file = lint.file('no-color-hex.sass'); + + it('enforce', function (done) { + lint.test(file, { + 'no-color-hex': 1 + }, function (data) { + lint.assert.equal(9, data.warningCount); + done(); + }); + }); +}); diff --git a/tests/sass/no-color-hex.sass b/tests/sass/no-color-hex.sass new file mode 100644 index 00000000..69c26e65 --- /dev/null +++ b/tests/sass/no-color-hex.sass @@ -0,0 +1,25 @@ +$foo-color: #123 + +.foo + background: linear-gradient(top, #cc2, #44d) + color: #fff + + +$bar-color: #112233 + +.bar + background: linear-gradient(top, #cccc22, #4444dd) + color: #ffffff + +.baz + border-color: #123456 + + +// color literals, rgb and hsl values currently don't get returned +// by the AST's color type + +$qux-color: red +$rgb-color: rgb(255, 255, 255) +$rgba-color: rgba(0, 0, 0, .1) +$hsl-color: hsl(40, 50%, 50%) +$hsla-color: hsla(40, 50%, 50%, .3) diff --git a/tests/sass/no-color-hex.scss b/tests/sass/no-color-hex.scss new file mode 100644 index 00000000..58c383d4 --- /dev/null +++ b/tests/sass/no-color-hex.scss @@ -0,0 +1,26 @@ +$foo-color: #123; + +.foo { + background: linear-gradient(top, #cc2, #44d); + color: #fff; +} + +$bar-color: #112233; + +.bar { + background: linear-gradient(top, #cccc22, #4444dd); + color: #ffffff; +} + +.baz { + border-color: #123456; +} + +// color literals, rgb and hsl values currently don't get returned +// by the AST's color type + +$qux-color: red; +$rgb-color: rgb(255, 255, 255); +$rgba-color: rgba(0, 0, 0, .1); +$hsl-color: hsl(40, 50%, 50%); +$hsla-color: hsla(40, 50%, 50%, .3); From 254c42e2c2f86873fdeb4b735052c0c91410a5b0 Mon Sep 17 00:00:00 2001 From: Dan Purdy Date: Wed, 22 Jun 2016 20:11:22 +0100 Subject: [PATCH 2/4] :mag: Add no-color-hex rule --- lib/rules/no-color-hex.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 lib/rules/no-color-hex.js diff --git a/lib/rules/no-color-hex.js b/lib/rules/no-color-hex.js new file mode 100644 index 00000000..d5190056 --- /dev/null +++ b/lib/rules/no-color-hex.js @@ -0,0 +1,22 @@ +'use strict'; + +var helpers = require('../helpers'); + +module.exports = { + 'name': 'no-color-hex', + 'defaults': {}, + 'detect': function (ast, parser) { + var result = []; + + ast.traverseByType('color', function (value) { + result = helpers.addUnique(result, { + 'ruleId': parser.rule.name, + 'line': value.start.line, + 'column': value.start.column, + 'message': 'Hexadecimal colors should not be used', + 'severity': parser.severity + }); + }); + return result; + } +}; From e96f4e1d06550da540c420fc4f99bfc893aaee56 Mon Sep 17 00:00:00 2001 From: Dan Purdy Date: Wed, 22 Jun 2016 20:11:48 +0100 Subject: [PATCH 3/4] :memo: Add no-color-hex documentation --- docs/rules/no-color-hex.md | 33 +++++++++++++++++++++++++++++++++ lib/config/sass-lint.yml | 1 + 2 files changed, 34 insertions(+) create mode 100644 docs/rules/no-color-hex.md diff --git a/docs/rules/no-color-hex.md b/docs/rules/no-color-hex.md new file mode 100644 index 00000000..9d55d95e --- /dev/null +++ b/docs/rules/no-color-hex.md @@ -0,0 +1,33 @@ +# No Color Hex + +Rule `no-color-hex` will disallow the use of hexadecimal colors + +## Examples + +When enabled the following are disallowed. + +```scss +$foo-color: #456; + +.bar { + background: linear-gradient(top, #3ff, #ddd); +} + +.baz { + color: #fff; +} +``` + +When enabled the following are allowed: + +```scss +$foo-color: red; + +.bar { + background: linear-gradient(top, blue, green); +} + +.baz { + color: white; +} +``` diff --git a/lib/config/sass-lint.yml b/lib/config/sass-lint.yml index dd3e5113..1b3ed511 100644 --- a/lib/config/sass-lint.yml +++ b/lib/config/sass-lint.yml @@ -18,6 +18,7 @@ rules: # Disallows no-attribute-selectors: 0 + no-color-hex: 0 no-color-keywords: 1 no-color-literals: 1 no-combinators: 0 From c993574a7366d31cce9fcfe1b0391b54922d8615 Mon Sep 17 00:00:00 2001 From: Dan Purdy Date: Mon, 8 Aug 2016 22:58:37 +0100 Subject: [PATCH 4/4] :bug: Fix comments warning as selectors #789 --- lib/rules/single-line-per-selector.js | 47 +++++++++++++++++------- tests/sass/single-line-per-selector.sass | 9 +++++ tests/sass/single-line-per-selector.scss | 10 +++++ 3 files changed, 52 insertions(+), 14 deletions(-) diff --git a/lib/rules/single-line-per-selector.js b/lib/rules/single-line-per-selector.js index 9ba9c7ca..53cdf902 100644 --- a/lib/rules/single-line-per-selector.js +++ b/lib/rules/single-line-per-selector.js @@ -2,6 +2,31 @@ var helpers = require('../helpers'); +/** + * Checks a ruleset for selectors or EOL characters. If a selector is found before an EOL + * then it returns the selector node for reporting or returns false + * + * @param {Object} ruleset - The ruleset node + * @param {number} index - The current index of the delimiter + * @returns {Object|boolean} Either the selector node or false + */ +var checkLineForSelector = function (ruleset, index) { + var curIndex = index += 1; + if (ruleset.content[curIndex]) { + for (; curIndex < ruleset.content.length; curIndex++) { + var curType = ruleset.content[curIndex].type; + if (curType === 'space' && helpers.hasEOL(ruleset.content[curIndex])) { + return false; + } + if (curType === 'selector' || curType === 'typeSelector') { + return ruleset.content[curIndex]; + } + } + } + + return false; +}; + module.exports = { 'name': 'single-line-per-selector', 'defaults': {}, @@ -10,22 +35,16 @@ module.exports = { ast.traverseByType('ruleset', function (ruleset) { ruleset.forEach('delimiter', function (delimiter, j) { - var next = ruleset.content[j + 1] || false; + var next = checkLineForSelector(ruleset, j); if (next) { - if (next.is('selector')) { - next = next.content[0]; - } - - if (!(next.is('space') && helpers.hasEOL(next.content))) { - result = helpers.addUnique(result, { - 'ruleId': parser.rule.name, - 'line': next.start.line, - 'column': next.start.column, - 'message': 'Selectors must be placed on new lines', - 'severity': parser.severity - }); - } + result = helpers.addUnique(result, { + 'ruleId': parser.rule.name, + 'line': next.start.line, + 'column': next.start.column, + 'message': 'Selectors must be placed on new lines', + 'severity': parser.severity + }); } }); }); diff --git a/tests/sass/single-line-per-selector.sass b/tests/sass/single-line-per-selector.sass index 72210d79..f2b94b0f 100644 --- a/tests/sass/single-line-per-selector.sass +++ b/tests/sass/single-line-per-selector.sass @@ -48,3 +48,12 @@ .foo .bar &, .baz & content: 'foo' + +// Issue #789 - Issue with comments being warned as selector content on single lines +button, +html, +html input[type='button'], // 6 +input[type='reset'], +input[type='submit'] + -webkit-appearance: button; // 7 + cursor: pointer; // 8 diff --git a/tests/sass/single-line-per-selector.scss b/tests/sass/single-line-per-selector.scss index 65ed18ef..18fcb5df 100644 --- a/tests/sass/single-line-per-selector.scss +++ b/tests/sass/single-line-per-selector.scss @@ -54,3 +54,13 @@ content: 'foo'; } } + +// Issue #789 - Issue with comments being warned as selector content on single lines +button, +html, +html input[type='button'], // 6 +input[type='reset'], +input[type='submit'] { + -webkit-appearance: button; // 7 + cursor: pointer; // 8 +}