From a7ded68993f1a0212a32b3ea869f67944965d177 Mon Sep 17 00:00:00 2001 From: Martijn Cuppens Date: Sat, 22 Sep 2018 17:51:28 +0300 Subject: [PATCH 1/2] Add the ability to ignore variables. --- README.md | 15 +++++++++++++++ cli.js | 14 ++++++++++++-- index.js | 16 ++++++++++++++-- tests/_variables.scss | 1 + tests/integration.js | 4 +++- 5 files changed, 45 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 037d6da..bf7d81b 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,21 @@ console.log(unused.total); * `dir`: string * Returns an object with `unused` and `total`. `unused` has the array of unused variables and `total` their count. +### Ignore variables + +```shell +fusv folder --ignore '$my-var,$my-second-var' +``` +Or + +```js +const fusv = require('find-unused-sass-variables') +const ignoredVars = ['$my-var', '$my-second-var'] + +const unused = fusv.find('scss', { ignoredVars }) +``` + + ## Notes * The tool's logic is pretty "dumb"; if you use the same name for a variable in different files or namespaces, diff --git a/cli.js b/cli.js index 8292d3d..bbec244 100644 --- a/cli.js +++ b/cli.js @@ -7,7 +7,17 @@ const chalk = require('chalk'); const ora = require('ora'); const fusv = require('./index.js'); -const args = process.argv.slice(2); // The first and second args are: path/to/node script.js +// The first and second args are: path/to/node script.js +// If an argument starts with --, exclude the argument and the next argument. +const args = process.argv.slice(2) + .filter((arg, i, list) => !arg.startsWith('--') && (i === 0 || !list[i - 1].startsWith('--'))); + +// Ignored variables, comma separated. +const ignore = process.argv.slice(2) + .filter((arg, i, list) => i !== 0 && list[i - 1] === '--ignore') + .join(',') + .split(','); + const log = console.log; let success = true; @@ -30,7 +40,7 @@ args.forEach((arg) => { spinner.info(`Finding unused variables in "${infoClr.bold(dir)}"...`); - const unusedVars = fusv.find(dir); + const unusedVars = fusv.find(dir, { ignore }); spinner.info(`${infoClr.bold(unusedVars.total)} total variables.`); diff --git a/index.js b/index.js index dd660f4..b349c02 100644 --- a/index.js +++ b/index.js @@ -5,15 +5,26 @@ const path = require('path'); const glob = require('glob'); const scssParser = require('postcss-scss/lib/scss-parse'); const Declaration = require('postcss/lib/declaration'); +const defaultOption = { + ignore: [] +}; // Blame TC39... https://github.com/benjamingr/RegExp.escape/issues/37 function regExpQuote(str) { return str.replace(/[-\\^$*+?.()|[\]{}]/g, '\\$&'); } -function findUnusedVars(strDir) { +function findUnusedVars(strDir, opts) { + const options = Object.assign(defaultOption, opts); const dir = path.isAbsolute(strDir) ? strDir : path.resolve(strDir); + if (typeof options.ignore === 'string') { + options.ignore = options.ignore.split(','); + } + + // Trim list of ignored variables + options.ignore = options.ignore.map((val) => val.trim()); + if (!(fs.existsSync(dir) && fs.statSync(dir).isDirectory())) { throw new Error(`"${dir}": Not a valid directory!`); } @@ -35,7 +46,8 @@ function findUnusedVars(strDir) { const parsedScss = scssParser(sassFilesString); const variables = parsedScss.nodes .filter((node) => node instanceof Declaration) - .map((declaration) => declaration.prop); + .map((declaration) => declaration.prop) + .filter((variable) => !options.ignore.includes(variable)); // Store unused vars from all files and loop through each variable const unusedVars = variables.filter((variable) => { diff --git a/tests/_variables.scss b/tests/_variables.scss index 7a90969..57acbba 100644 --- a/tests/_variables.scss +++ b/tests/_variables.scss @@ -2,3 +2,4 @@ $white: #fff !default; $a: 10px; $b : 20px; $unused : #000; +$ignored-variable: #ace; diff --git a/tests/integration.js b/tests/integration.js index 4dca74c..6ce18ca 100644 --- a/tests/integration.js +++ b/tests/integration.js @@ -9,9 +9,11 @@ const expectedUnused = [ '$unused' ]; +const ignore = ['$ignored-variable']; + console.log('Run integration tests...'); -const result = fusv.find('./'); +const result = fusv.find('./', { ignore }); if (result.total === expectedUnused.length) { console.info(`All tests passed (${result.total})`); From ebd4f9f421c268ac98927fe074a5c9cc8b37826e Mon Sep 17 00:00:00 2001 From: Johann-S Date: Sun, 21 Oct 2018 18:56:26 +0300 Subject: [PATCH 2/2] Make sure ignore is an array. --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index b349c02..3dc4349 100644 --- a/index.js +++ b/index.js @@ -18,8 +18,8 @@ function findUnusedVars(strDir, opts) { const options = Object.assign(defaultOption, opts); const dir = path.isAbsolute(strDir) ? strDir : path.resolve(strDir); - if (typeof options.ignore === 'string') { - options.ignore = options.ignore.split(','); + if (Boolean(options.ignore) && !Array.isArray(options.ignore)) { + throw new TypeError('`ignore` should be an Array'); } // Trim list of ignored variables