diff --git a/lib/format.js b/lib/format.js index e8542e224..a067c2830 100644 --- a/lib/format.js +++ b/lib/format.js @@ -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]); @@ -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); @@ -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; }; diff --git a/test/general.test.js b/test/general.test.js index 5196d6525..be376946f 100644 --- a/test/general.test.js +++ b/test/general.test.js @@ -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); @@ -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(); + }); + }); });