Skip to content

Commit

Permalink
✨ Added ability to sort by files
Browse files Browse the repository at this point in the history
refs #122

- `gscan.format` now accepts a new option to return a different format
  - sort by errors & warnings files (sortByFiles: true)
  - sort by rules by default
- it's currently hard to sort the passed rules, because no rule is pushed with the file reference
  - too much effort for no usage
  • Loading branch information
kirrg001 committed Aug 9, 2018
1 parent ba6b511 commit 36bc38b
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 5 deletions.
79 changes: 74 additions & 5 deletions lib/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ calcScore = function calcScore(results, stats) {
/**
* TODO: This needs cleaning up, a lot
*/
format = function format(theme, options) {
options = _.extend({onlyFatalErrors: false}, options);
format = function format(theme, options = {}) {
options = _.extend({onlyFatalErrors: false, sortByFiles: false}, options);
const checkVersion = _.get(options, 'checkVersion', 'latest');
const ruleSet = spec.get([checkVersion]);

Expand Down Expand Up @@ -67,7 +67,6 @@ format = function format(theme, options) {

_.each(theme.results.pass, function (code, index) {
const rule = ruleSet.rules[code];

theme.results.pass[index] = _.extend({}, rule, {code: code});
stats[rule.level] += 1;
processedCodes.push(code);
Expand All @@ -80,8 +79,78 @@ format = function format(theme, options) {
theme.results.score = calcScore(theme.results, stats);
theme.results.hasFatalErrors = hasFatalErrors;

// SORT errors!
theme.results.error = _.orderBy(theme.results.error, 'fatal', 'desc');
// CASE 1: sort by files (@TODO: sort by passed rules is not possible, because we don't push the file reference)
// CASE 2: (default): sort by rules
if (options.sortByFiles) {
const recommendationsByFile = {};
const errorsByFile = {};
const warningsByFile = {};

theme.results.error.forEach((error) => {
const failures = error.failures;

if (!failures || !failures.length) {
return;
}

failures.forEach((failure) => {
if (!errorsByFile.hasOwnProperty(failure.ref)) {
errorsByFile[failure.ref] = [];
}

errorsByFile[failure.ref].push(error);
});
});

theme.results.warning.forEach((warning) => {
const failures = warning.failures;

if (!failures || !failures.length) {
return;
}

failures.forEach((failure) => {
if (!warningsByFile.hasOwnProperty(failure.ref)) {
warningsByFile[failure.ref] = [];
}

warningsByFile[failure.ref].push(warning);
});
});

theme.results.recommendation.forEach((passed) => {
const failures = passed.failures;

if (!failures || !failures.length) {
return;
}

failures.forEach((failure) => {
if (!recommendationsByFile.hasOwnProperty(failure.ref)) {
recommendationsByFile[failure.ref] = [];
}

recommendationsByFile[failure.ref].push(passed);
});
});

theme.results.recommendation = {
all: theme.results.recommendation,
byFiles: recommendationsByFile
};

theme.results.error = {
all: theme.results.error,
byFiles: errorsByFile
};

theme.results.warning = {
all: theme.results.warning,
byFiles: warningsByFile
};
} else {
theme.results.error = _.orderBy(theme.results.error, 'fatal', 'desc');
}

return theme;
};
Expand Down
48 changes: 48 additions & 0 deletions test/general.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ describe('format', function () {
checker(themePath('005-compile/invalid')).then((theme) => {
theme = format(theme);

theme.results.error.length.should.eql(10);
theme.results.error[0].fatal.should.eql(true);
theme.results.error[1].fatal.should.eql(false);
theme.results.error[8].fatal.should.eql(false);
Expand All @@ -420,4 +421,51 @@ describe('format', function () {
done();
});
});

it('sort by files', function (done) {
checker(themePath('005-compile/invalid')).then((theme) => {
theme = format(theme, {sortByFiles: true});

theme.results.hasFatalErrors.should.be.true();

theme.results.recommendation.all.length.should.eql(1);
theme.results.recommendation.byFiles['package.json'].length.should.eql(1);

theme.results.warning.all.length.should.eql(2);
theme.results.warning.byFiles['default.hbs'].length.should.eql(2);

theme.results.error.all.length.should.eql(10);

// 1 rule has file references
theme.results.error.byFiles['author.hbs'].length.should.eql(1);
theme.results.error.byFiles['page.hbs'].length.should.eql(1);
theme.results.error.byFiles['post.hbs'].length.should.eql(1);
theme.results.error.byFiles['index.hbs'].length.should.eql(1);
theme.results.error.byFiles['package.json'].length.should.eql(9);

done();
});
});

it('sort by files', function (done) {
checker(themePath('001-deprecations/invalid')).then((theme) => {
theme = format(theme, {sortByFiles: true});

theme.results.hasFatalErrors.should.be.true();

theme.results.recommendation.all.length.should.eql(1);
theme.results.recommendation.byFiles['package.json'].length.should.eql(1);

theme.results.error.all.length.should.eql(36);
theme.results.warning.all.length.should.eql(0);

theme.results.error.byFiles['assets/my.css'].length.should.eql(5);
theme.results.error.byFiles['default.hbs'].length.should.eql(6);
theme.results.error.byFiles['post.hbs'].length.should.eql(17);
theme.results.error.byFiles['partials/mypartial.hbs'].length.should.eql(4);
theme.results.error.byFiles['index.hbs'].length.should.eql(7);

done();
});
});
});

0 comments on commit 36bc38b

Please sign in to comment.