Skip to content
This repository has been archived by the owner on Feb 15, 2021. It is now read-only.

Commit

Permalink
Polished things in real-world scenario (#5)
Browse files Browse the repository at this point in the history
- Fixed case where we can't parse the color
- Ignored `rgba` because color can't work with alpha (in hex)
  • Loading branch information
jukben authored Mar 31, 2019
1 parent 4524fcf commit ea26151
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 20 deletions.
23 changes: 15 additions & 8 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ Example configuration. 👇
```js
{
rules: {
"@productboardlabs/smart-color-replacement": {
"@snowWhite": "#f4f5e2"
}
"@productboard/smart-color-replacement": [
{
"@snowWhite": "#f4f5e2"
}
]
}
}
```
Expand All @@ -43,11 +45,16 @@ You can also run this rule in **strict mode** which means that there is no other

```js
{
rules: {
"@productboardlabs/smart-color-replacement": [{
"@snowWhite": "#f4f5e2"
}, "strictMode"]
}
"rules": {
"@productboard/smart-color-replacement": [
{
"@white": "#ffffff"
},
{
"strictMode": true
}
]
}
}
```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@productboard/stylelint-pb",
"version": "1.0.0",
"main": "index.js",
"main": "src/index.js",
"license": "MIT",
"scripts": {
"test": "jest"
Expand Down
13 changes: 12 additions & 1 deletion src/rules/__tests__/smart-color-replacement.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ testRule(rule, {
{
code: "a { color: #333333; }",
description: "Color out of the config should be ignored by default"
},
{
code: "a { box-shadow: 0 1px 2px rgb(broken, 0.3); }",
description: "non-parsable colors should be ignored"
}
],

Expand Down Expand Up @@ -86,10 +90,17 @@ testRule(rule, {
{
"@snowWhite": "#f4f5e2"
},
"strictMode"
{ strictMode: true }
],
fix: true,

accept: [
{
code: "a { box-shadow: 0 1px 2px rgb(broken, 0.3); }",
description: "non-parsable colors should be ignored"
}
],

reject: [
{
code: "a { color: #333333; }",
Expand Down
29 changes: 19 additions & 10 deletions src/rules/smart-color-replacement.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const stylelint = require("stylelint");
const color = require("color");

const HEX_REGEX = /(#[a-f0-9]{3,6})/gi;
const RGB_REGEX = /(rgb|rgba)\([^\)]*\)/gi;
const RGB_REGEX = /rgb\([^\)]*\)/gi;
const HSL_REGEX = /hsl\((\d+),\s*([\d.]+)%,\s*([\d.]+)%\)/gi;

const ruleName = "@productboard/smart-color-replacement";
Expand All @@ -14,7 +14,7 @@ const messages = stylelint.utils.ruleMessages(ruleName, {
.join(" ")
});

const rule = function(configuration, strictMode, context) {
const rule = function(configuration, { strictMode = false } = {}, context) {
return (root, result) => {
if (typeof configuration !== "object" || !configuration) return;

Expand All @@ -40,18 +40,27 @@ const rule = function(configuration, strictMode, context) {
return acc;
}, []);

const results = colors.map(used => {
const sanitized = color(used).hex();
return {
const results = colors.reduce((acc, v, i) => {
let sanitized;
try {
sanitized = color(v).hex();
} catch (e) {
// ignore non-parsable colors
return acc;
}

acc.push({
valid: lookUpObject[sanitized]
? !lookUpObject[sanitized]
: strictMode !== "strictMode",
: !strictMode,
suggested: lookUpObject[sanitized],
used
};
});
used: v
});

return acc;
}, []);

const failedColors = results.filter(res => !res.valid);
const failedColors = results.filter(({ valid }) => !valid);

if (failedColors.length) {
stylelint.utils.report({
Expand Down

0 comments on commit ea26151

Please sign in to comment.