Skip to content

Commit

Permalink
🚧 Add inclusion and exclusion rules for selector, rules, units
Browse files Browse the repository at this point in the history
  • Loading branch information
kghugo committed Aug 4, 2020
1 parent 5ee6463 commit d3a23fe
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 71 deletions.
52 changes: 33 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,45 @@
let postcss = require('postcss')

const {
requiredParam
filterIdenticalValues,
hasWrappedInRFS,
isIncluded,
shouldBeTransformed
} = require('./utilities/helper.js')

module.exports = postcss.plugin('postcss-rfs-autopilot', ({ rulesToTransform, unitToTransform, rulesToIgnore, selectorToIgnore}) => {
module.exports = postcss.plugin('postcss-rfs-autopilot', ({
includedRules,
excludedRules,
includedSelectors,
excludedSelectors,
includedUnits,
excludedUnits
}) => {
const options = {
rulesToTransform: rulesToTransform || requiredParam('rulesToTransform'),
unitToTransform: unitToTransform || requiredParam('unitToTransform'),
selectorToIgnore: selectorToIgnore,
rulesToIgnore: rulesToIgnore
includedRules: includedRules || [
'*'
],
excludedRules: excludedRules || [],
includedSelectors: includedSelectors || [
'*'
],
excludedSelectors: excludedSelectors || [],
includedUnits: includedUnits || [ 'px', 'rem' ],
excludedUnits: excludedUnits || []
}
// Work with options here
//Filter includedRules here with excludedRules
// options.includedRules = filterIdenticalValues(options.includedRules, options.excludedRules)
// options.includedSelectors = filterIdenticalValues(options.includedSelectors, options.excludedSelectors)
// options.includedUnits = filterIdenticalValues(options.includedUnits, options.excludedUnits)

return (root, result) => {
root.walkRules((rule) => {
options.rulesToTransform.forEach((desiredRuleToTransform) => {
//Find all rules the user want to apply RFS to
rule.walkDecls(desiredRuleToTransform, (decl) => {
//Ignore this rule if it is found in rulesToIgnore array

//Check if rfs() has already applied to the value
if( ! /^rfs/g.test(decl)){
console.log('RFS declaration not found. Apply transformation here.')
console.log(decl);
}
})
})
root.walkDecls((decl) => {

if ( shouldBeTransformed(decl, options) ) {
decl.value = `rfs(${decl.value})`
}

})
}
})
46 changes: 46 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "postcss-rfs-autopilot",
"version": "0.1.0",
"description": "A PostCSS plugin that will automagically mark your CSS up with rfs() for RFS",
"keywords": [
"postcss",
"css",
"postcss-plugin",
"rfs",
"rfs-auto-pilot"
],
"scripts": {
"test": "jest && eslint ."
},
"author": "Hugo Sum <hugosum.dev@protonmail.com>",
"license": "MIT",
"repository": "winston0410/postcss-rfs-autopilot",
"dependencies": {
"postcss": "^7.0.18"
},
"devDependencies": {
"@logux/eslint-config": "^33.0.0",
"eslint": "^6.5.1",
"eslint-config-postcss": "^3.0.7",
"eslint-config-standard": "^14.1.0",
"eslint-plugin-es5": "^1.4.1",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-jest": "^22.19.0",
"eslint-plugin-node": "^10.0.0",
"eslint-plugin-prefer-let": "^1.0.1",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-security": "^1.4.0",
"eslint-plugin-standard": "^4.0.1",
"eslint-plugin-unicorn": "^12.1.0",
"jest": "^24.9.0"
},
"eslintConfig": {
"extends": "eslint-config-postcss"
},
"engines": {
"node": ">=8.0.0"
},
"jest": {
"testEnvironment": "node"
}
}
46 changes: 0 additions & 46 deletions package.json.tpl

This file was deleted.

58 changes: 52 additions & 6 deletions utilities/helper.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,57 @@
const requiredParam = (param) => {
const requiredParamError = new Error(`Required parameter, "${param}" is missing.`)
if (typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(requiredParamError, requiredParam)
const {
options
} = require('../index.js')

const hasWrappedInRFS = (decl) => {
if (/^rfs/g.test(decl.value)) {
return true
}
return false
}

const isIncluded = (decl, inclusionRules) => {
if (inclusionRules.includes('*')) {
return true;
}

if (inclusionRules.some(unit => RegExp(unit).test(decl))) {
return true
}
throw requiredParamError
return false
}

const filterIdenticalValues = (sourceArray, arrayToCheckAgainst) => {
return sourceArray.filter(item => !arrayToCheckAgainst.includes(item))
}

const shouldBeTransformed = (decl, options) => {

if (hasWrappedInRFS(decl)) {
return false
}

if (!isIncluded(decl.prop, options.includedRules) || isIncluded(decl.prop, options.excludedRules)) {
console.log(`${decl.prop} excluded`);
return false
}

if (!isIncluded(decl.parent.selector, options.includedSelectors) || isIncluded(decl.parent.selector, options.excludedSelectors)) {
console.log(`${decl.parent.selector} excluded`);
return false
}

if (!isIncluded(decl.value, options.includedUnits) || isIncluded(decl.value, options.excludedUnits)) {
console.log(`${decl.value} excluded`);
return false
}

console.log(`${decl.parent.selector}{${decl.prop}: ${decl.value}} has been wrapped with rfs()`);
return true
}

module.exports = {
requiredParam
hasWrappedInRFS,
isIncluded,
filterIdenticalValues,
shouldBeTransformed
}

0 comments on commit d3a23fe

Please sign in to comment.