Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to ignore some vars #24

Merged
merged 2 commits into from
Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 12 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
XhmikosR marked this conversation as resolved.
Show resolved Hide resolved
.filter((arg, i, list) => i !== 0 && list[i - 1] === '--ignore')
.join(',')
.split(',');
XhmikosR marked this conversation as resolved.
Show resolved Hide resolved

const log = console.log;
let success = true;

Expand All @@ -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.`);

Expand Down
16 changes: 14 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (Boolean(options.ignore) && !Array.isArray(options.ignore)) {
throw new TypeError('`ignore` should be an Array');
}

// 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!`);
}
Expand All @@ -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) => {
Expand Down
1 change: 1 addition & 0 deletions tests/_variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ $white: #fff !default;
$a: 10px;
$b : 20px;
$unused : #000;
$ignored-variable: #ace;
4 changes: 3 additions & 1 deletion tests/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ const expectedUnused = [
'$unused'
];

const ignore = ['$ignored-variable'];
XhmikosR marked this conversation as resolved.
Show resolved Hide resolved

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})`);
Expand Down