Skip to content

Commit

Permalink
can tracable variable
Browse files Browse the repository at this point in the history
Breaking Change

Co-Authored-By: Georg Gentzen <60478031+gentzeng@users.noreply.github.com>
  • Loading branch information
danny007in and gentzeng committed Jan 29, 2021
1 parent 8532ae8 commit c51ee50
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
9 changes: 8 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 19 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down
8 changes: 7 additions & 1 deletion lib/parse-variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit c51ee50

Please sign in to comment.