Skip to content

Commit

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

- gscan.format accepts a new flag to return a different format
  - sort by errors & warnings files
- @todo: sort recommendations
  • Loading branch information
kirrg001 committed Aug 9, 2018
1 parent ba6b511 commit 03bd40b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 4 deletions.
47 changes: 43 additions & 4 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,7 @@ 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 @@ -81,7 +81,46 @@ format = function format(theme, options) {
theme.results.hasFatalErrors = hasFatalErrors;

// SORT errors!
theme.results.error = _.orderBy(theme.results.error, 'fatal', 'desc');
if (options.sortByFiles) {
const errorsByFile = {};
const warningsByFile = {};

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

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;

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

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

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
42 changes: 42 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,45 @@ 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.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.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 03bd40b

Please sign in to comment.