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 8, 2018
1 parent ba6b511 commit 066b8d1
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 4 deletions.
44 changes: 40 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,43 @@ format = function format(theme, options) {
theme.results.hasFatalErrors = hasFatalErrors;

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

const errorsWithNoFiles = _.filter(theme.results.error, function (err) {
return !err.failures;
});

const warningsWithNoFiles = _.filter(theme.results.warning, function (err) {
return !err.failures;
});

allFiles.forEach(function (fileName) {
errorsByFile[fileName] = _.filter(theme.results.error, function (err) {
return _.find(err.failures, {ref: fileName});
});

warningsByFile[fileName] = _.filter(theme.results.warning, function (err) {
return _.find(err.failures, {ref: fileName});
});
});

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

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

return theme;
};
Expand Down
53 changes: 53 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,56 @@ 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.noFiles.length.should.eql(2);

theme.results.warning.byFiles['author.hbs'].length.should.eql(0);
theme.results.warning.byFiles['index.hbs'].length.should.eql(0);
theme.results.warning.byFiles['default.hbs'].length.should.eql(0);
theme.results.warning.byFiles['page.hbs'].length.should.eql(0);
theme.results.warning.byFiles['post.hbs'].length.should.eql(0);

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

// 9 rules have no file reference
theme.results.error.noFiles.length.should.eql(9);

// 1 rule has file references
theme.results.error.byFiles['author.hbs'].length.should.eql(1);
theme.results.error.byFiles['default.hbs'].length.should.eql(0);
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);

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.warning.noFiles.length.should.eql(0);

theme.results.error.byFiles['assets/my.css'].length.should.eql(3);
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 066b8d1

Please sign in to comment.