From c51ee507516319430c044d5c63a3e01ef9c5963f Mon Sep 17 00:00:00 2001 From: Daniel <50356015+danny007in@users.noreply.github.com> Date: Sat, 30 Jan 2021 04:57:24 +0530 Subject: [PATCH] can tracable variable Breaking Change Co-Authored-By: Georg Gentzen <60478031+gentzeng@users.noreply.github.com> --- cli.js | 9 ++++++++- index.js | 21 +++++++++++++++++++-- lib/parse-variable.js | 8 +++++++- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/cli.js b/cli.js index 925c0e1..4088ed0 100644 --- a/cli.js +++ b/cli.js @@ -32,7 +32,14 @@ function main(args) { console.log(`${chalk.cyan.bold(unusedVars.total)} total variables.`); unusedVars.unused.forEach(unusedVar => { - console.log(`Variable ${chalk.bold(unusedVar)} is not being used!`); + if (unusedVar.filePath !== '') { + console.log(`\n${chalk.underline(unusedVar.filePath)}`); + } + + console.log( + ` ${unusedVar.line}:${unusedVar.column}\t` + + `Variable ${chalk.bold(unusedVar.name)} is not being used!` + ); }); unusedList = unusedList.concat(unusedVars.unused); diff --git a/index.js b/index.js index 26c1d45..a6f6a03 100644 --- a/index.js +++ b/index.js @@ -39,11 +39,28 @@ function findUnusedVars(strDir, opts) { sassFilesString = sassFilesString.replace(/---/g, ''); } - const variables = parse(sassFilesString, options.ignore); + let variables = []; + const sassVarInfo = []; + let strSass = ''; + sassFiles.forEach((file, i) => { + strSass = fs.readFileSync(file, 'utf8'); + if (strSass.includes('---')) { + strSass = strSass.replace(/---/g, ''); + } + + sassVarInfo[i] = parse(strSass, options.ignore); + + sassVarInfo.forEach((sassVar, j) => { + if (sassVar[j].filePath === '') { + sassVar[j].filePath = file; + } + }); + variables = [].concat(...sassVarInfo); + }); // Store unused vars from all files and loop through each variable const unusedVars = variables.filter(variable => { - const re = new RegExp(`(${escapeRegex(variable)})\\b(?!-)`, 'g'); + const re = new RegExp(`(${escapeRegex(variable.name)})\\b(?!-)`, 'g'); return sassFilesString.match(re).length === 1; }); diff --git a/lib/parse-variable.js b/lib/parse-variable.js index 0173b80..ff62e2e 100644 --- a/lib/parse-variable.js +++ b/lib/parse-variable.js @@ -19,7 +19,13 @@ function findVars(node, result, ignoreList) { } if (node instanceof Declaration && node.prop.startsWith('$') && !ignoreList.includes(node.prop) && fusvEnabled) { - result.push(node.prop); + // result.push(node); + result.push({ + line: node.source.start.line, + column: node.source.start.column, + name: node.prop, + filePath: '' + }); return; }