From 3add5d827ee36b0d52ee2be30566df45816f0d66 Mon Sep 17 00:00:00 2001 From: sivanatarjan Date: Sat, 8 Aug 2020 23:31:12 +0530 Subject: [PATCH 01/20] added color-hex-alpha rule --- lib/rules/color-hex-alpha/README.md | 0 lib/rules/color-hex-alpha/__tests__/index.js | 44 +++++++++++++ lib/rules/color-hex-alpha/index.js | 68 ++++++++++++++++++++ lib/rules/index.js | 1 + 4 files changed, 113 insertions(+) create mode 100644 lib/rules/color-hex-alpha/README.md create mode 100644 lib/rules/color-hex-alpha/__tests__/index.js create mode 100644 lib/rules/color-hex-alpha/index.js diff --git a/lib/rules/color-hex-alpha/README.md b/lib/rules/color-hex-alpha/README.md new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/rules/color-hex-alpha/__tests__/index.js b/lib/rules/color-hex-alpha/__tests__/index.js new file mode 100644 index 0000000000..1a53a13403 --- /dev/null +++ b/lib/rules/color-hex-alpha/__tests__/index.js @@ -0,0 +1,44 @@ +'use strict'; + +const { messages, ruleName } = require('..'); + +testRule({ + ruleName, + config: 'never', + accept: [ + { + code: 'a{color:#888; }', + }, + { + code: 'a{color:#888888; }', + }, + ], + reject: [ + { + code: 'a{color:#8888;}', + message: messages.unexpected('#8888'), + line: 1, + column: 9, + }, + ], +}); +testRule({ + ruleName, + config: 'always', + accept: [ + { + code: 'a { color: #8888; }', + }, + { + code: 'a { color: #88888888; }', + }, + ], + reject: [ + { + code: 'a{color:#888;}', + message: messages.expected('#888'), + line: 1, + column: 9, + }, + ], +}); diff --git a/lib/rules/color-hex-alpha/index.js b/lib/rules/color-hex-alpha/index.js new file mode 100644 index 0000000000..84e7d7383b --- /dev/null +++ b/lib/rules/color-hex-alpha/index.js @@ -0,0 +1,68 @@ +'use strict'; + +const declarationValueIndex = require('../../utils/declarationValueIndex'); +const isStandardSyntaxDeclaration = require('../../utils/isStandardSyntaxDeclaration'); +const isStandardSyntaxValue = require('../../utils/isStandardSyntaxValue'); +const report = require('../../utils/report'); +const ruleMessages = require('../../utils/ruleMessages'); +const validateOptions = require('../../utils/validateOptions'); +const valueParser = require('postcss-value-parser'); +const ruleName = 'color-hex-alpha'; + +const messages = ruleMessages(ruleName, { + expected: (unfixed) => `Expected alpha channel in "${unfixed}"`, + unexpected: (unfixed) => `Unexpected alpha channel in "${unfixed}"`, +}); + +function rule(primary) { + return (root, result) => { + const validOptions = validateOptions(result, ruleName, { + actual: primary, + possible: ['always', 'never'], + }); + + if (!validOptions) return; + + root.walkDecls((decl) => { + if (!isStandardSyntaxDeclaration(decl)) return; + + const parsedValue = valueParser(getValue(decl)); + + parsedValue.walk((node) => { + if (node.type === 'word' && node.value && node.value.startsWith('#')) { + let hexValue = node.value; + + /* + length calculations for with "#" + */ + if (!isStandardSyntaxValue(hexValue)) return; + + if (primary === 'always' && (hexValue.length === 5 || hexValue.length === 9)) { + return; + } + + if (primary === 'never' && (hexValue.length === 4 || hexValue.length === 7)) { + return; + } + + report({ + message: + primary === 'never' ? messages.unexpected(hexValue) : messages.expected(hexValue), + node: decl, + index: declarationValueIndex(decl) + node.sourceIndex, + result, + ruleName, + }); + } + }); + }); + }; +} + +function getValue(decl) { + return decl.raws.value ? decl.raws.value.raw : decl.value; +} + +rule.ruleName = ruleName; +rule.messages = messages; +module.exports = rule; diff --git a/lib/rules/index.js b/lib/rules/index.js index 4846a04021..004b168fbc 100644 --- a/lib/rules/index.js +++ b/lib/rules/index.js @@ -55,6 +55,7 @@ const rules = { require('./block-opening-brace-space-before'), )(), 'color-function-notation': importLazy(() => require('./color-function-notation'))(), + 'color-hex-alpha': importLazy(() => require('./color-hex-alpha'))(), 'color-hex-case': importLazy(() => require('./color-hex-case'))(), 'color-hex-length': importLazy(() => require('./color-hex-length'))(), 'color-named': importLazy(() => require('./color-named'))(), From c39912f355d8f16bbdd6c16c487352ab39fc2f81 Mon Sep 17 00:00:00 2001 From: sivanatarjan Date: Sat, 8 Aug 2020 23:44:17 +0530 Subject: [PATCH 02/20] added ts-nocheck comment --- lib/rules/color-hex-alpha/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/rules/color-hex-alpha/index.js b/lib/rules/color-hex-alpha/index.js index 84e7d7383b..6b9a2499e1 100644 --- a/lib/rules/color-hex-alpha/index.js +++ b/lib/rules/color-hex-alpha/index.js @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; const declarationValueIndex = require('../../utils/declarationValueIndex'); From 9abdeb9eedac5e02e4eb677ddccdb30c497b3cf8 Mon Sep 17 00:00:00 2001 From: sivanatarjan Date: Sat, 8 Aug 2020 23:46:14 +0530 Subject: [PATCH 03/20] added readme.md --- lib/rules/color-hex-alpha/README.md | 66 +++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/lib/rules/color-hex-alpha/README.md b/lib/rules/color-hex-alpha/README.md index e69de29bb2..079e6725ab 100644 --- a/lib/rules/color-hex-alpha/README.md +++ b/lib/rules/color-hex-alpha/README.md @@ -0,0 +1,66 @@ +# color-hex-alpha + +Require or disallow alpha channel for hex colors. + + +```css +a { color: #ffff } +/** ↑ + * This hex alpha */ +``` + +## Options + +`string`: `"always"|"never"` + +### `"always"` + +The following patterns are considered violations: + + +```css +a { color: #fff; } +``` + + +```css +a { color: #ffffff; } +``` + +The following patterns are _not_ considered violations: + + +```css +a { color: #fffa; } +``` + + +```css +a { color: #ffffffaa; } +``` + +### `"never"` + +The following patterns are considered violations: + + +```css +a { color: #fffa; } +``` + + +```css +a { color: #ffffffaa; } +``` + +The following patterns are _not_ considered violations: + + +```css +a { color: #fff; } +``` + + +```css +a { color: #ffffff; } +``` From 76a225b262d08edd228ab546dbca857e21b420a0 Mon Sep 17 00:00:00 2001 From: Sivanatarajan Date: Mon, 10 Aug 2020 22:50:18 +0530 Subject: [PATCH 04/20] Update lib/rules/color-hex-alpha/index.js Co-authored-by: Richard Hallows --- lib/rules/color-hex-alpha/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/rules/color-hex-alpha/index.js b/lib/rules/color-hex-alpha/index.js index 6b9a2499e1..5738f6b055 100644 --- a/lib/rules/color-hex-alpha/index.js +++ b/lib/rules/color-hex-alpha/index.js @@ -8,6 +8,7 @@ const report = require('../../utils/report'); const ruleMessages = require('../../utils/ruleMessages'); const validateOptions = require('../../utils/validateOptions'); const valueParser = require('postcss-value-parser'); + const ruleName = 'color-hex-alpha'; const messages = ruleMessages(ruleName, { From b010bd352f8561b6cfa1ecbb00c7fff111b9c2b4 Mon Sep 17 00:00:00 2001 From: Sivanatarajan Date: Mon, 10 Aug 2020 22:50:34 +0530 Subject: [PATCH 05/20] Update lib/rules/color-hex-alpha/index.js Co-authored-by: Richard Hallows --- lib/rules/color-hex-alpha/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/color-hex-alpha/index.js b/lib/rules/color-hex-alpha/index.js index 5738f6b055..df10119fdd 100644 --- a/lib/rules/color-hex-alpha/index.js +++ b/lib/rules/color-hex-alpha/index.js @@ -28,7 +28,7 @@ function rule(primary) { root.walkDecls((decl) => { if (!isStandardSyntaxDeclaration(decl)) return; - const parsedValue = valueParser(getValue(decl)); + const parsedValue = valueParser(decl.value)); parsedValue.walk((node) => { if (node.type === 'word' && node.value && node.value.startsWith('#')) { From 91cd023587607fbed41b9f4e2f93cb853e4d2ce8 Mon Sep 17 00:00:00 2001 From: Sivanatarajan Date: Mon, 10 Aug 2020 22:50:54 +0530 Subject: [PATCH 06/20] Update lib/rules/color-hex-alpha/index.js Co-authored-by: Richard Hallows --- lib/rules/color-hex-alpha/index.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/rules/color-hex-alpha/index.js b/lib/rules/color-hex-alpha/index.js index df10119fdd..3533eb9452 100644 --- a/lib/rules/color-hex-alpha/index.js +++ b/lib/rules/color-hex-alpha/index.js @@ -61,10 +61,6 @@ function rule(primary) { }; } -function getValue(decl) { - return decl.raws.value ? decl.raws.value.raw : decl.value; -} - rule.ruleName = ruleName; rule.messages = messages; module.exports = rule; From af7f37e0131657359e7adef83c30250e5fe599c5 Mon Sep 17 00:00:00 2001 From: Sivanatarajan Date: Mon, 10 Aug 2020 22:51:06 +0530 Subject: [PATCH 07/20] Update lib/rules/color-hex-alpha/__tests__/index.js Co-authored-by: Richard Hallows --- lib/rules/color-hex-alpha/__tests__/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/rules/color-hex-alpha/__tests__/index.js b/lib/rules/color-hex-alpha/__tests__/index.js index 1a53a13403..2faf601cd4 100644 --- a/lib/rules/color-hex-alpha/__tests__/index.js +++ b/lib/rules/color-hex-alpha/__tests__/index.js @@ -22,6 +22,7 @@ testRule({ }, ], }); + testRule({ ruleName, config: 'always', From 0bfd429e16558eb8f20248b34c2b61e583bf3eeb Mon Sep 17 00:00:00 2001 From: sivanatarjan Date: Sat, 15 Aug 2020 00:02:51 +0530 Subject: [PATCH 08/20] added more testRules --- lib/rules/color-hex-alpha/__tests__/index.js | 50 ++++++++++++++++---- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/lib/rules/color-hex-alpha/__tests__/index.js b/lib/rules/color-hex-alpha/__tests__/index.js index 1a53a13403..496f1bdeb1 100644 --- a/lib/rules/color-hex-alpha/__tests__/index.js +++ b/lib/rules/color-hex-alpha/__tests__/index.js @@ -7,18 +7,18 @@ testRule({ config: 'never', accept: [ { - code: 'a{color:#888; }', + code: 'a { color:#fff; }', }, { - code: 'a{color:#888888; }', + code: 'a { color:#ffffff; }', }, ], reject: [ { - code: 'a{color:#8888;}', - message: messages.unexpected('#8888'), + code: 'a { color:#ffff; }', + message: messages.unexpected('#ffff'), line: 1, - column: 9, + column: 12, }, ], }); @@ -27,18 +27,48 @@ testRule({ config: 'always', accept: [ { - code: 'a { color: #8888; }', + code: 'a { color: #ffff; }', }, { - code: 'a { color: #88888888; }', + code: 'a { color: #ffffffff; }', + }, + { + code: 'a { background: linear-gradient(to left, #fffa, red 100%); }', + }, + { + code: 'a { background: url("/url/images/img.svg#img"); }', }, ], reject: [ { - code: 'a{color:#888;}', - message: messages.expected('#888'), + code: 'a { color:#fff; }', + message: messages.expected('#fff'), + line: 1, + column: 12, + }, + { + code: 'a { color:#ffffff; }', + message: messages.expected('#ffffff'), + line: 1, + column: 12, + }, + { + code: 'a { background: linear-gradient(to left, #fff, red 100%); }', + message: messages.expected('#fff'), line: 1, - column: 9, + column: 42, + }, + ], +}); +testRule({ + ruleName, + config: ['never'], + syntax: 'scss', + + accept: [ + { + code: 'a { color: #{ff}; }', + description: 'scss interpolation', }, ], }); From 0f203b374a01d7b85c0b0e148cad98cd0e35af16 Mon Sep 17 00:00:00 2001 From: sivanatarjan Date: Sat, 15 Aug 2020 00:07:29 +0530 Subject: [PATCH 09/20] lint errors --- lib/rules/color-hex-alpha/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/color-hex-alpha/index.js b/lib/rules/color-hex-alpha/index.js index 3533eb9452..14c1e41851 100644 --- a/lib/rules/color-hex-alpha/index.js +++ b/lib/rules/color-hex-alpha/index.js @@ -28,7 +28,7 @@ function rule(primary) { root.walkDecls((decl) => { if (!isStandardSyntaxDeclaration(decl)) return; - const parsedValue = valueParser(decl.value)); + const parsedValue = valueParser(decl.value); parsedValue.walk((node) => { if (node.type === 'word' && node.value && node.value.startsWith('#')) { From 2ba28521b19f95a2b85a54f2460da937c93a55d4 Mon Sep 17 00:00:00 2001 From: sivanatarjan Date: Sat, 15 Aug 2020 00:36:34 +0530 Subject: [PATCH 10/20] lint fix --- lib/rules/color-hex-alpha/__tests__/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/rules/color-hex-alpha/__tests__/index.js b/lib/rules/color-hex-alpha/__tests__/index.js index 643a3d3d1f..bfa7fb8d7b 100644 --- a/lib/rules/color-hex-alpha/__tests__/index.js +++ b/lib/rules/color-hex-alpha/__tests__/index.js @@ -18,7 +18,7 @@ testRule({ code: 'a { color:#ffff; }', message: messages.unexpected('#ffff'), line: 1, - column: 12, + column: 11, }, ], }); @@ -45,13 +45,13 @@ testRule({ code: 'a { color:#fff; }', message: messages.expected('#fff'), line: 1, - column: 12, + column: 11, }, { code: 'a { color:#ffffff; }', message: messages.expected('#ffffff'), line: 1, - column: 12, + column: 11, }, { code: 'a { background: linear-gradient(to left, #fff, red 100%); }', From 3c816c591290439ae1514fe11b268fb3f1d46880 Mon Sep 17 00:00:00 2001 From: sivanatarjan Date: Wed, 19 Aug 2020 04:16:07 +0530 Subject: [PATCH 11/20] added color-hex-alpha rule in list.md --- docs/user-guide/rules/list.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/user-guide/rules/list.md b/docs/user-guide/rules/list.md index 2fb3483afa..a0a41341ae 100644 --- a/docs/user-guide/rules/list.md +++ b/docs/user-guide/rules/list.md @@ -88,6 +88,7 @@ Grouped first by the following categories and then by the [_thing_](http://apps. ### Color - [`color-function-notation`](../../../lib/rules/color-function-notation/README.md): Specify modern or legacy notation for applicable color-functions (Autofixable). +- [`color-hex-alpha`](../../../lib/rules/color-hex-alpha/README.md): Require or disallow alpha channel for hex colors. - [`color-named`](../../../lib/rules/color-named/README.md): Require (where possible) or disallow named colors. - [`color-no-hex`](../../../lib/rules/color-no-hex/README.md): Disallow hex colors. From 74aef399b35802d09c6915d0984c7e0862a884fa Mon Sep 17 00:00:00 2001 From: Sivanatarajan Date: Mon, 4 Jan 2021 08:08:49 +0530 Subject: [PATCH 12/20] Update lib/rules/color-hex-alpha/__tests__/index.js Co-authored-by: Richard Hallows --- lib/rules/color-hex-alpha/__tests__/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/color-hex-alpha/__tests__/index.js b/lib/rules/color-hex-alpha/__tests__/index.js index bfa7fb8d7b..408773f702 100644 --- a/lib/rules/color-hex-alpha/__tests__/index.js +++ b/lib/rules/color-hex-alpha/__tests__/index.js @@ -10,7 +10,7 @@ testRule({ code: 'a { color:#fff; }', }, { - code: 'a { color:#ffffff; }', + code: 'a { color: #ffffff; }', }, ], reject: [ From f49a000ca581d45eb63d3659cff5c325f4ee47e5 Mon Sep 17 00:00:00 2001 From: Sivanatarajan Date: Mon, 4 Jan 2021 08:08:59 +0530 Subject: [PATCH 13/20] Update lib/rules/color-hex-alpha/__tests__/index.js Co-authored-by: Richard Hallows --- lib/rules/color-hex-alpha/__tests__/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/color-hex-alpha/__tests__/index.js b/lib/rules/color-hex-alpha/__tests__/index.js index 408773f702..4de2b847a4 100644 --- a/lib/rules/color-hex-alpha/__tests__/index.js +++ b/lib/rules/color-hex-alpha/__tests__/index.js @@ -7,7 +7,7 @@ testRule({ config: 'never', accept: [ { - code: 'a { color:#fff; }', + code: 'a { color: #fff; }', }, { code: 'a { color: #ffffff; }', From e1a7c9c3e69cd9f8438fedbe32829ef4ea4fa091 Mon Sep 17 00:00:00 2001 From: Sivanatarajan Date: Mon, 4 Jan 2021 08:09:07 +0530 Subject: [PATCH 14/20] Update lib/rules/color-hex-alpha/__tests__/index.js Co-authored-by: Richard Hallows --- lib/rules/color-hex-alpha/__tests__/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/color-hex-alpha/__tests__/index.js b/lib/rules/color-hex-alpha/__tests__/index.js index 4de2b847a4..52c6796760 100644 --- a/lib/rules/color-hex-alpha/__tests__/index.js +++ b/lib/rules/color-hex-alpha/__tests__/index.js @@ -15,7 +15,7 @@ testRule({ ], reject: [ { - code: 'a { color:#ffff; }', + code: 'a { color: #ffff; }', message: messages.unexpected('#ffff'), line: 1, column: 11, From ed97deb81b54c6eb9795fed6387ba5250707808a Mon Sep 17 00:00:00 2001 From: Sivanatarajan Date: Mon, 4 Jan 2021 08:34:11 +0530 Subject: [PATCH 15/20] Update lib/rules/color-hex-alpha/index.js Co-authored-by: Richard Hallows --- lib/rules/color-hex-alpha/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/color-hex-alpha/index.js b/lib/rules/color-hex-alpha/index.js index 14c1e41851..6e6248e5d4 100644 --- a/lib/rules/color-hex-alpha/index.js +++ b/lib/rules/color-hex-alpha/index.js @@ -12,7 +12,7 @@ const valueParser = require('postcss-value-parser'); const ruleName = 'color-hex-alpha'; const messages = ruleMessages(ruleName, { - expected: (unfixed) => `Expected alpha channel in "${unfixed}"`, + expected: (hex) => `Expected alpha channel in "${hex}"`, unexpected: (unfixed) => `Unexpected alpha channel in "${unfixed}"`, }); From ba285ac8ed684419be4c8badc6369e6f2400b5a4 Mon Sep 17 00:00:00 2001 From: Sivanatarajan Date: Mon, 4 Jan 2021 08:34:40 +0530 Subject: [PATCH 16/20] Update lib/rules/color-hex-alpha/index.js Co-authored-by: Richard Hallows --- lib/rules/color-hex-alpha/index.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/rules/color-hex-alpha/index.js b/lib/rules/color-hex-alpha/index.js index 6e6248e5d4..10b6893357 100644 --- a/lib/rules/color-hex-alpha/index.js +++ b/lib/rules/color-hex-alpha/index.js @@ -34,9 +34,6 @@ function rule(primary) { if (node.type === 'word' && node.value && node.value.startsWith('#')) { let hexValue = node.value; - /* - length calculations for with "#" - */ if (!isStandardSyntaxValue(hexValue)) return; if (primary === 'always' && (hexValue.length === 5 || hexValue.length === 9)) { From c65de4b39832bbc2edcb93772de98a098817166b Mon Sep 17 00:00:00 2001 From: Sivanatarajan Date: Mon, 4 Jan 2021 08:34:50 +0530 Subject: [PATCH 17/20] Update lib/rules/color-hex-alpha/index.js Co-authored-by: Richard Hallows --- lib/rules/color-hex-alpha/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/color-hex-alpha/index.js b/lib/rules/color-hex-alpha/index.js index 10b6893357..c477279997 100644 --- a/lib/rules/color-hex-alpha/index.js +++ b/lib/rules/color-hex-alpha/index.js @@ -13,7 +13,7 @@ const ruleName = 'color-hex-alpha'; const messages = ruleMessages(ruleName, { expected: (hex) => `Expected alpha channel in "${hex}"`, - unexpected: (unfixed) => `Unexpected alpha channel in "${unfixed}"`, + unexpected: (hex) => `Unexpected alpha channel in "${hex}"`, }); function rule(primary) { From ad47041c9961c835a51585353437ed5729c82ba7 Mon Sep 17 00:00:00 2001 From: Sivanatarajan Date: Mon, 4 Jan 2021 18:46:08 +0530 Subject: [PATCH 18/20] Update lib/rules/color-hex-alpha/__tests__/index.js Co-authored-by: Richard Hallows --- lib/rules/color-hex-alpha/__tests__/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/color-hex-alpha/__tests__/index.js b/lib/rules/color-hex-alpha/__tests__/index.js index 52c6796760..0c108e735b 100644 --- a/lib/rules/color-hex-alpha/__tests__/index.js +++ b/lib/rules/color-hex-alpha/__tests__/index.js @@ -37,7 +37,7 @@ testRule({ code: 'a { background: linear-gradient(to left, #fffa, red 100%); }', }, { - code: 'a { background: url("/url/images/img.svg#img"); }', + code: 'a { background: url(#fff); }', }, ], reject: [ From 845d19f8bbc6b54ba60fae1d8211b13c32fd1002 Mon Sep 17 00:00:00 2001 From: Sivanatarajan Date: Mon, 4 Jan 2021 18:46:17 +0530 Subject: [PATCH 19/20] Update lib/rules/color-hex-alpha/index.js Co-authored-by: Richard Hallows --- lib/rules/color-hex-alpha/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/rules/color-hex-alpha/index.js b/lib/rules/color-hex-alpha/index.js index c477279997..f7c5ba1655 100644 --- a/lib/rules/color-hex-alpha/index.js +++ b/lib/rules/color-hex-alpha/index.js @@ -31,7 +31,8 @@ function rule(primary) { const parsedValue = valueParser(decl.value); parsedValue.walk((node) => { - if (node.type === 'word' && node.value && node.value.startsWith('#')) { + if (node.type !== 'word') return; + if (node.value.startsWith('#')) return; let hexValue = node.value; if (!isStandardSyntaxValue(hexValue)) return; From 401e6763be0b7f011dcf48510177c86083d1fb08 Mon Sep 17 00:00:00 2001 From: sivanatarjan Date: Tue, 5 Jan 2021 20:19:48 +0530 Subject: [PATCH 20/20] PR fixes --- lib/rules/color-hex-alpha/__tests__/index.js | 12 +++++++ lib/rules/color-hex-alpha/index.js | 33 ++++++++++---------- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/lib/rules/color-hex-alpha/__tests__/index.js b/lib/rules/color-hex-alpha/__tests__/index.js index 0c108e735b..892fcb0673 100644 --- a/lib/rules/color-hex-alpha/__tests__/index.js +++ b/lib/rules/color-hex-alpha/__tests__/index.js @@ -71,5 +71,17 @@ testRule({ code: 'a { color: #{ff}; }', description: 'scss interpolation', }, + { + code: 'a { $var: #fff;}', + description: 'scss interpolation', + }, + ], + reject: [ + { + code: 'a { $var: #fffa;}', + message: messages.expected('#fff'), + line: 1, + column: 8, + }, ], }); diff --git a/lib/rules/color-hex-alpha/index.js b/lib/rules/color-hex-alpha/index.js index f7c5ba1655..0cbff6e6b8 100644 --- a/lib/rules/color-hex-alpha/index.js +++ b/lib/rules/color-hex-alpha/index.js @@ -32,28 +32,29 @@ function rule(primary) { parsedValue.walk((node) => { if (node.type !== 'word') return; + if (node.value.startsWith('#')) return; - let hexValue = node.value; - if (!isStandardSyntaxValue(hexValue)) return; + let hexValue = node.value; - if (primary === 'always' && (hexValue.length === 5 || hexValue.length === 9)) { - return; - } + if (!isStandardSyntaxValue(hexValue)) return; - if (primary === 'never' && (hexValue.length === 4 || hexValue.length === 7)) { - return; - } + if (primary === 'always' && (hexValue.length === 5 || hexValue.length === 9)) { + return; + } - report({ - message: - primary === 'never' ? messages.unexpected(hexValue) : messages.expected(hexValue), - node: decl, - index: declarationValueIndex(decl) + node.sourceIndex, - result, - ruleName, - }); + if (primary === 'never' && (hexValue.length === 4 || hexValue.length === 7)) { + return; } + + report({ + message: + primary === 'never' ? messages.unexpected(hexValue) : messages.expected(hexValue), + node: decl, + index: declarationValueIndex(decl) + node.sourceIndex, + result, + ruleName, + }); }); }); };