Skip to content

Commit

Permalink
Add eslint config, changes in plugin main file
Browse files Browse the repository at this point in the history
  • Loading branch information
EdMSL committed Oct 30, 2019
1 parent adc3dec commit 0724be9
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 44 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Ignore built files except build/index.js
/.vscode
node_modules
19 changes: 19 additions & 0 deletions .eslintrc-full.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
parserOptions: {
ecmaVersion: 2018,
},
env: {
node: true,
es6: true,
mocha: true,
},
extends: [
"eslint-config-postcss",
],
rules: {
"max-len": [1, 100],
"semi": [2, "always"],

"prefer-let/prefer-let": 0,
},
}
19 changes: 19 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
parserOptions: {
ecmaVersion: 2018,
},
env: {
node: true,
es6: true,
mocha: true,
},
extends: [
"eslint-config-postcss",
],
rules: {
"max-len": [1, 100],
"semi": [1, "always"],

"prefer-let/prefer-let": 0,
},
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ yarn-error.log
*.css
postcss.config.js
test/test.js
styles/
47 changes: 31 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,51 @@
const postcss = require('postcss')
const valueParser = require('postcss-values-parser')
const convert = require('color-convert')
const postcss = require('postcss');
const valueParser = require('postcss-values-parser');
const convert = require('color-convert');

const regexpHEX = /#([a-f\d]{3}|[a-f\d]{6})/i
const regexpHEX = /#([a-f\d]{3}|[a-f\d]{6})$/i;
const regexpHEXAlpha = /#([a-f\d]{4}|[a-f\d]{8})$/i;
const regexpRGB = /rgb\(/;
const regexpHSL = /hsl\(/;

const defaultOptions = {
syntax: '',
outputColorFormat: ''
}
};

module.exports = postcss.plugin('postcss-color-converter', (opts = {}) => {
let currentOptions = {
...defaultOptions,
...opts
}
};

return style => {
style.walkDecls(decl => {
let value = decl.value
let value = decl.value;

if (value) {
let newColor = valueParser.parse(value)
let newValue = valueParser.parse(value);

if (regexpHEX.test(value) && currentOptions.outputColorFormat === 'rgb') {
newColor.walk(node => {
if (node.type === 'word') {
node.value = `rgb(${ convert.hex.rgb(node.value).join(', ') })`
newValue.walk(node => {
if (node.type === 'word' && node.isColor && node.isHex) {
node.value = `rgb(${ convert.hex.rgb(node.value).join(', ') })`;
}
})
});
}
decl.value = newColor.toString()

if (regexpRGB.test(value) && currentOptions.outputColorFormat === 'hex') {
newValue.walk(node => {
if (node.type === 'func' && node.isColor) {
const newNode = node.clone({ type: 'word' });
newNode.value = `#${ convert.rgb.hex(node.nodes[0], node.nodes[2], node.nodes[4]) }`;
node.replaceWith(newNode);
// console.log(newNode)
}
});
}

decl.value = newValue.toString();
}
})
}
})
});
};
});
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
"converter"
],
"scripts": {
"test": "mocha ./test/index.test.js",
"postcss": "postcss test.css --config ./postcss.config.js -r"
"test": "mocha ./test/*.test.js",
"lint": "eslint --config ./.eslintrc-full.js **/*.js --fix",
"postcss": "postcss ./styles/test.{css,scss} --config ./postcss.config.js -r"
},
"author": "EdMSL <eduard2011rus@gmail.com>",
"license": "MIT",
Expand Down
50 changes: 50 additions & 0 deletions test/hex.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const postcss = require('postcss');
const { assert } = require('chai');

const plugin = require('../index');

/* eslint-disable prefer-arrow-callback, func-names */

describe('postcss-color-converter', function () {
function transform (source, opts) {
return postcss([plugin(opts)]).process(source).css;
}

it('Input color should not be converted', function () {
assert.equal(transform(
'body { color: #ffffff; }',
{}
), 'body { color: #ffffff; }');
assert.equal(transform(
'body { color: #ffffff; }',
{ outputColorFormat: 'hex' }
), 'body { color: #ffffff; }');
});

it('Input color must be converted to rgb', function () {
assert.equal(transform(
'body { color: #ffffff; }',
{ outputColorFormat: 'rgb' }
), 'body { color: rgb(255, 255, 255); }');
assert.equal(transform(
`ul {
background: linear-gradient(
to bottom,
#456789 0%,
#fff555 100%,
);
}`,
{ outputColorFormat: 'rgb' }
), `ul {
background: linear-gradient(
to bottom,
rgb(69, 103, 137) 0%,
rgb(255, 245, 85) 100%,
);
}`);
assert.equal(transform(
'body { color: #555; }',
{ outputColorFormat: 'rgb' }
), 'body { color: rgb(85, 85, 85); }');
});
});
26 changes: 0 additions & 26 deletions test/index.test.js

This file was deleted.

46 changes: 46 additions & 0 deletions test/rgb.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const postcss = require('postcss');
const { assert } = require('chai');

const plugin = require('../index');

/* eslint-disable prefer-arrow-callback, func-names */

describe('postcss-color-converter', function () {
function transform (source, opts) {
return postcss([plugin(opts)]).process(source).css;
}

it('Input color should not be converted', function () {
assert.equal(transform(
'body { color: rgb(255, 255, 255); }',
{ outputColorFormat: 'rgb' }
), 'body { color: rgb(255, 255, 255); }');
assert.equal(transform(
'body { color: rgb(255, 255, 255); }',
{}
), 'body { color: rgb(255, 255, 255); }');
});

it('Input color must be converted to hex', function () {
assert.equal(transform(
'body { color: rgb(255, 255, 255); }',
{ outputColorFormat: 'hex' }
), 'body { color: #ffffff; }');
assert.equal(transform(
`ul {
background: linear-gradient(
to bottom,
rgb(69, 103, 137) 0%,
rgb(255, 245, 85) 100%,
);
}`,
{ outputColorFormat: 'hex' }
), `ul {
background: linear-gradient(
to bottom,
#456789 0%,
#fff555 100%,
);
}`);
});
});

0 comments on commit 0724be9

Please sign in to comment.