From 005ef92cc1a8faa1f114d06d6f7280607641cc73 Mon Sep 17 00:00:00 2001 From: Nazar Gargol Date: Fri, 12 Apr 2019 15:04:50 +0800 Subject: [PATCH 1/5] Cleaned up syntax --- lib/format.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/format.js b/lib/format.js index fc516881..48340844 100644 --- a/lib/format.js +++ b/lib/format.js @@ -6,10 +6,7 @@ const levelWeights = { recommendation: 1 }; -let format; -let calcScore; - -calcScore = function calcScore(results, stats) { +const calcScore = (results, stats) => { var maxScore, actualScore, balancedScore; maxScore = _.reduce(levelWeights, function (max, weight, level) { @@ -51,18 +48,21 @@ const formatCLI = (theme) => { /** * TODO: This needs cleaning up, a lot */ -format = function format(theme, options = {}) { - options = _.extend({onlyFatalErrors: false, sortByFiles: false}, options); +const format = (theme, options = {}) => { + options = _.extend({ + onlyFatalErrors: false, + sortByFiles: false + }, options); + const checkVersion = _.get(options, 'checkVersion', 'latest'); const ruleSet = spec.get([checkVersion]); - - var processedCodes = [], - hasFatalErrors = false, - stats = { - error: 0, - warning: 0, - recommendation: 0 - }; + const processedCodes = []; + let hasFatalErrors = false; + const stats = { + error: 0, + warning: 0, + recommendation: 0 + }; theme.results.error = []; theme.results.warning = []; From 4af2c2c3b9f01f4046b082eb2b7149dd86b60ed9 Mon Sep 17 00:00:00 2001 From: Nazar Gargol Date: Fri, 12 Apr 2019 15:56:31 +0800 Subject: [PATCH 2/5] Added fatal error group --- bin/cli.js | 45 ++++++++++++++++++++++++++++++++++----------- lib/format.js | 13 ++++++++----- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/bin/cli.js b/bin/cli.js index eaddb792..635711d2 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -97,34 +97,49 @@ function outputResult(result) { } function getSummary(theme) { - let summaryText = ''; - const errorCount = theme.results.error.length; - const warnCount = theme.results.warning.length; const pluralize = require('pluralize'); const checkSymbol = '\u2713'; + // NOTE: had to subtract the number of 'invisible' formating symbols + // needs update if formatting above changes + let hiddenSymbols = 0; + let summaryText = ''; - if (errorCount === 0 && warnCount === 0) { + if (theme.results.fatal.length === 0 + && theme.results.error.length === 0 + && theme.results.warning.length === 0) { summaryText = `${chalk.green(checkSymbol)} Your theme is compatible with Ghost ${theme.checkedVersion}`; } else { summaryText = `Your theme has`; - if (!_.isEmpty(theme.results.error)) { - summaryText += chalk.red.bold(` ${pluralize('error', theme.results.error.length, true)}`); + if (!_.isEmpty(theme.results.fatal)) { + summaryText += chalk.red.bold(` ${pluralize('fatal error', theme.results.fatal.length, true)}`); + hiddenSymbols += 19; } - if (!_.isEmpty(theme.results.error) && !_.isEmpty(theme.results.warning)) { - summaryText += ' and'; + if (!_.isEmpty(theme.results.error)) { + if (!_.isEmpty(theme.results.fatal)){ + if (!_.isEmpty(theme.results.warning)){ + summaryText += ','; + } else { + summaryText += ' and'; + } + } + + summaryText += chalk.red.bold(` ${pluralize('error', theme.results.error.length, true)}`); + hiddenSymbols += 19; } if (!_.isEmpty(theme.results.warning)) { + if (!_.isEmpty(theme.results.error) || !_.isEmpty(theme.results.fatal)){ + summaryText += ' and'; + } + summaryText += chalk.yellow.bold(` ${pluralize('warning', theme.results.warning.length, true)}`); + hiddenSymbols += 19; } summaryText += '!'; - // NOTE: had to subtract the number of 'invisible' formating symbols - // needs update if formatting above changes - const hiddenSymbols = 38; summaryText += '\n' + _.repeat('-', (summaryText.length - hiddenSymbols)); } @@ -138,6 +153,14 @@ function outputResults(theme, options) { ui.log('\n' + getSummary(theme)); + if (!_.isEmpty(theme.results.fatal)) { + ui.log(chalk.red.bold('\nFatal errors')); + ui.log(chalk.red.bold('------------')); + ui.log(chalk.red('Must be fixed to activate the theme.\n')); + + _.each(theme.results.fatal, outputResult); + } + if (!_.isEmpty(theme.results.error)) { ui.log(chalk.red.bold('\nErrors')); ui.log(chalk.red.bold('------')); diff --git a/lib/format.js b/lib/format.js index 48340844..21d62af8 100644 --- a/lib/format.js +++ b/lib/format.js @@ -61,9 +61,11 @@ const format = (theme, options = {}) => { const stats = { error: 0, warning: 0, - recommendation: 0 + recommendation: 0, + fatal: 0 }; + theme.results.fatal = []; theme.results.error = []; theme.results.warning = []; theme.results.recommendation = []; @@ -75,10 +77,13 @@ const format = (theme, options = {}) => { if (rule.fatal && options.onlyFatalErrors || options.onlyFatalErrors === false) { if (rule.fatal) { hasFatalErrors = true; + theme.results.fatal.push(_.extend({}, _.merge({}, {fatal: false}, rule), info, {code: code})) + stats.fatal += 1; + } else { + theme.results[rule.level].push(_.extend({}, _.merge({}, {fatal: false}, rule), info, {code: code})); + stats[rule.level] += 1; } - theme.results[rule.level].push(_.extend({}, _.merge({}, {fatal: false}, rule), info, {code: code})); - stats[rule.level] += 1; processedCodes.push(code); } }); @@ -168,8 +173,6 @@ const format = (theme, options = {}) => { all: theme.results.warning, byFiles: warningsByFile }; - } else { - theme.results.error = _.orderBy(theme.results.error, 'fatal', 'desc'); } if (options.format) { From 9198d6a85e5b6adc0e85280c4cfebd61fddf00e6 Mon Sep 17 00:00:00 2001 From: Nazar Gargol Date: Tue, 16 Apr 2019 10:00:48 +0200 Subject: [PATCH 3/5] Added fatal grouping per filename --- lib/format.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/format.js b/lib/format.js index 21d62af8..606c2069 100644 --- a/lib/format.js +++ b/lib/format.js @@ -108,9 +108,26 @@ const format = (theme, options = {}) => { // CASE 2: (default): sort by rules if (options.sortByFiles) { const recommendationsByFile = {}; + const fatalByFile = {}; const errorsByFile = {}; const warningsByFile = {}; + theme.results.fatal.forEach((error) => { + const failures = error.failures; + + if (!failures || !failures.length) { + return; + } + + failures.forEach((failure) => { + if (!errorsByFile.hasOwnProperty(failure.ref)) { + fatalByFile[failure.ref] = []; + } + + fatalByFile[failure.ref].push(error); + }); + }); + theme.results.error.forEach((error) => { const failures = error.failures; @@ -164,6 +181,11 @@ const format = (theme, options = {}) => { byFiles: recommendationsByFile }; + theme.results.fatal = { + all: theme.results.fatal, + byFiles: fatalByFile + }; + theme.results.error = { all: theme.results.error, byFiles: errorsByFile From 235a9afe295d8b6e1256ddbfb9d6a4b680624f0e Mon Sep 17 00:00:00 2001 From: Nazar Gargol Date: Tue, 16 Apr 2019 11:29:26 +0200 Subject: [PATCH 4/5] Fixed linting --- lib/format.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/format.js b/lib/format.js index 606c2069..3167eb66 100644 --- a/lib/format.js +++ b/lib/format.js @@ -77,7 +77,7 @@ const format = (theme, options = {}) => { if (rule.fatal && options.onlyFatalErrors || options.onlyFatalErrors === false) { if (rule.fatal) { hasFatalErrors = true; - theme.results.fatal.push(_.extend({}, _.merge({}, {fatal: false}, rule), info, {code: code})) + theme.results.fatal.push(_.extend({}, _.merge({}, {fatal: false}, rule), info, {code: code})); stats.fatal += 1; } else { theme.results[rule.level].push(_.extend({}, _.merge({}, {fatal: false}, rule), info, {code: code})); @@ -120,7 +120,7 @@ const format = (theme, options = {}) => { } failures.forEach((failure) => { - if (!errorsByFile.hasOwnProperty(failure.ref)) { + if (!fatalByFile.hasOwnProperty(failure.ref)) { fatalByFile[failure.ref] = []; } From 8b3e0176142c360016f64a1d6e99487530bc444d Mon Sep 17 00:00:00 2001 From: Nazar Gargol Date: Tue, 16 Apr 2019 11:33:02 +0200 Subject: [PATCH 5/5] Adjusted test suites to new fatal grouping --- test/general.test.js | 63 +++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/test/general.test.js b/test/general.test.js index 8974512c..3dc706e5 100644 --- a/test/general.test.js +++ b/test/general.test.js @@ -408,27 +408,11 @@ describe('format', function () { checker(themePath('005-compile/invalid')).then((theme) => { theme = format(theme); - theme.results.error.length.should.eql(16); - theme.results.error[0].fatal.should.eql(true); - // theme.results.error[1].fatal.should.eql(true); - // theme.results.error[2].fatal.should.eql(true); - theme.results.error[3].fatal.should.eql(false); - // theme.results.error[10].fatal.should.eql(false); + theme.results.error.length.should.eql(15); + theme.results.fatal.length.should.eql(1); - done(); - }).catch(done); - }); - - it('assert sorting', function (done) { - checker(themePath('is-empty')).then((theme) => { - theme = format(theme); - - theme.results.error[0].fatal.should.eql(true); - theme.results.error[1].fatal.should.eql(true); - theme.results.error[4].fatal.should.eql(false); - theme.results.error[10].fatal.should.eql(false); - theme.results.error[11].fatal.should.eql(false); - theme.results.error[12].fatal.should.eql(false); + theme.results.error[0].fatal.should.eql(false); + theme.results.fatal[0].fatal.should.eql(true); done(); }).catch(done); @@ -446,13 +430,16 @@ describe('format', function () { theme.results.warning.all.length.should.eql(3); theme.results.warning.byFiles['default.hbs'].length.should.eql(2); - theme.results.error.all.length.should.eql(16); + theme.results.error.all.length.should.eql(15); + theme.results.fatal.all.length.should.eql(1); // 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.fatal.byFiles['author.hbs'].length.should.eql(1); + theme.results.fatal.byFiles['page.hbs'].length.should.eql(1); + theme.results.fatal.byFiles['post.hbs'].length.should.eql(1); + theme.results.fatal.byFiles['index.hbs'].length.should.eql(1); + + theme.results.error.byFiles['default.hbs'].length.should.eql(1); theme.results.error.byFiles['package.json'].length.should.eql(9); done(); @@ -468,14 +455,24 @@ describe('format', function () { theme.results.recommendation.all.length.should.eql(2); theme.results.recommendation.byFiles['package.json'].length.should.eql(1); - theme.results.error.all.length.should.eql(88); + theme.results.fatal.all.length.should.eql(23); + theme.results.error.all.length.should.eql(65); theme.results.warning.all.length.should.eql(5); theme.results.error.byFiles['assets/my.css'].length.should.eql(3); - theme.results.error.byFiles['default.hbs'].length.should.eql(17); - theme.results.error.byFiles['post.hbs'].length.should.eql(54); - theme.results.error.byFiles['partials/mypartial.hbs'].length.should.eql(5); - theme.results.error.byFiles['index.hbs'].length.should.eql(9); + + theme.results.error.byFiles['default.hbs'].length.should.eql(13); + theme.results.fatal.byFiles['default.hbs'].length.should.eql(4); + + theme.results.error.byFiles['post.hbs'].length.should.eql(36); + theme.results.fatal.byFiles['post.hbs'].length.should.eql(18); + + theme.results.error.byFiles['partials/mypartial.hbs'].length.should.eql(2); + theme.results.fatal.byFiles['partials/mypartial.hbs'].length.should.eql(3); + + theme.results.error.byFiles['index.hbs'].length.should.eql(2); + theme.results.fatal.byFiles['index.hbs'].length.should.eql(7); + theme.results.error.byFiles['error.hbs'].length.should.eql(1); done(); @@ -486,10 +483,10 @@ describe('format', function () { return checker(themePath('001-deprecations/latest/invalid_all')).then((theme) => { theme = format(theme, {format: 'cli'}); - theme.results.error[0].rule.should.equal('Replace \u001b[36m{{pageUrl}}\u001b[39m with \u001b[36m{{page_url}}\u001b[39m'); + theme.results.fatal[0].rule.should.equal('Replace \u001b[36m{{pageUrl}}\u001b[39m with \u001b[36m{{page_url}}\u001b[39m'); - theme.results.error[0].details.should.startWith(`The helper \u001b[36m{{pageUrl}}\u001b[39m was replaced with \u001b[36m{{page_url}}\u001b[39m.\n`); - theme.results.error[0].details.should.endWith(`Find more information about the \u001b[36m{{page_url}}\u001b[39m helper here (https://themes.ghost.org/docs/page_url).`); + theme.results.fatal[0].details.should.startWith(`The helper \u001b[36m{{pageUrl}}\u001b[39m was replaced with \u001b[36m{{page_url}}\u001b[39m.\n`); + theme.results.fatal[0].details.should.endWith(`Find more information about the \u001b[36m{{page_url}}\u001b[39m helper here (https://themes.ghost.org/docs/page_url).`); }); }); });