Skip to content
This repository has been archived by the owner on Nov 15, 2018. It is now read-only.

Commit

Permalink
fix a bug with unused dependencies report.
Browse files Browse the repository at this point in the history
Addresses #19
  • Loading branch information
helio-frota committed Sep 16, 2016
1 parent c2b21a8 commit af26a66
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
13 changes: 7 additions & 6 deletions lib/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const fs = require('fs');

module.exports = {
report: report,
fileReport: fileReport
fileReport: fileReport,
unused: unused
};

function flatResult (result) {
Expand Down Expand Up @@ -71,23 +72,23 @@ function strFileLines (grouped) {
}

function unused (declarations, dependencies) {
const unused = [];
const unused = new Set();
if (declarations.length) {
dependencies.forEach(dep => {
declarations.forEach(d => {
if (d) {
if (!d.includes(dep)) {
unused.push(dep);
if (!d.toUpperCase().includes(dep.toUpperCase())) {
unused.add(dep);
}
}
});
});
} else {
dependencies.forEach(dep => {
unused.push(dep);
unused.add(dep);
});
}
return unused;
return Array.from(unused);
}

function jsonReporter (result, dependencies) {
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"test": "n/a"
},
"dependencies": {
"roi": "^0.2.'"
"roi": "^0.2.'",
"fidelity": "0.1.2"
},
"license": "Apache-2.0",
"devDependencies": {
Expand Down
10 changes: 10 additions & 0 deletions test/szero-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,13 @@ test('Should report to file.', t => {
}
t.end();
});

test('Should show unused dependencies from report.', t => {
const packageJsonLines = reader.read(path.join(__dirname, '/fixtures/package.json'));
const dependencies = searcher.searchDependencies(packageJsonLines);
const javascriptLines = reader.read(path.join(__dirname, '/fixtures/foo/x.js'));
const declarations = searcher.searchDeclarations(javascriptLines, dependencies);
const unused = reporter.unused(declarations, dependencies);
t.equal(unused.toString(), 'fidelity');
t.end();
});

0 comments on commit af26a66

Please sign in to comment.