From e09df5e209e5595d9309c4d1f470cccf7bdcee88 Mon Sep 17 00:00:00 2001 From: Robin Winslow Date: Wed, 31 Aug 2016 18:50:54 +0100 Subject: [PATCH] Reimplement max-warnings as a proper option --- bin/sass-lint.js | 47 ++++++++++++++--------------------------------- index.js | 14 ++++++++++++-- 2 files changed, 26 insertions(+), 35 deletions(-) diff --git a/bin/sass-lint.js b/bin/sass-lint.js index 787e9330..bd3908e5 100755 --- a/bin/sass-lint.js +++ b/bin/sass-lint.js @@ -10,12 +10,6 @@ var configPath, configOptions = {}, exitCode = 0; -var tooManyWarnings = function (detects) { - var warningCount = lint.warningCount(detects).count; - - return warningCount > 0 && warningCount > program.maxWarnings; -}; - var detectPattern = function (pattern) { var detects; @@ -25,12 +19,12 @@ var detectPattern = function (pattern) { lint.outputResults(detects, configOptions, configPath); } - if (lint.errorCount(detects).count || tooManyWarnings(detects)) { + if (lint.errorCount(detects).count) { exitCode = 1; } if (program.exit) { - lint.failOnError(detects); + lint.failOnError(detects, program.maxWarnings); } }; @@ -47,21 +41,20 @@ program .option('--max-warnings [integer]', 'Number of warnings to trigger nonzero exit code') .parse(process.argv); +// Create "options" and "files" dictionaries if they don't exist +if (! configOptions.hasOwnProperty('files')) { + configOptions.files = {} +} +if (! configOptions.hasOwnProperty('options')) { + configOptions.options = {} +} if (program.config && program.config !== true) { configPath = program.config; } if (program.ignore && program.ignore !== true) { - ignores = program.ignore.split(', '); - if (configOptions.hasOwnProperty('files')) { - configOptions.files.ignore = ignores; - } - else { - configOptions.files = { - 'ignore': ignores - }; - } + configOptions.files.ignore = program.ignore.split(', '); } if (program.syntax && ['sass', 'scss'].indexOf(program.syntax) > -1) { @@ -69,25 +62,11 @@ if (program.syntax && ['sass', 'scss'].indexOf(program.syntax) > -1) { } if (program.format && program.format !== true) { - if (configOptions.hasOwnProperty('options')) { - configOptions.options.formatter = program.format; - } - else { - configOptions.options = { - 'formatter': program.format - }; - } + configOptions.options.formatter = program.format; } if (program.output && program.output !== true) { - if (configOptions.hasOwnProperty('options')) { - configOptions.options['output-file'] = program.output; - } - else { - configOptions.options = { - 'output-file': program.output - }; - } + configOptions.options['output-file'] = program.output; } if (program.args.length === 0) { @@ -99,6 +78,8 @@ else { }); } +debugger; + process.on('exit', function () { process.exit(exitCode); // eslint-disable-line no-process-exit }); diff --git a/index.js b/index.js index decbc923..93d974e6 100644 --- a/index.js +++ b/index.js @@ -287,12 +287,22 @@ sassLint.outputResults = function (results, options, configPath) { * @param {object} results our results object * @returns {void} */ -sassLint.failOnError = function (results) { - var errorCount = this.errorCount(results); +sassLint.failOnError = function (results, maxWarnings) { + var errorCount = this.errorCount(results), + warningCount = this.warningCount(results), + options = this.getConfig().options; + + if (options.hasOwnProperty('max-warnings')) { + maxWarnings = typeof maxWarnings == 'undefined' ? options['max-warnings'] : maxWarnings; + } if (errorCount.count > 0) { throw new Error(errorCount.count + ' errors were detected in \n- ' + errorCount.files.join('\n- ')); } + + if (typeof maxWarnings != 'undefined' && warningCount.count > maxWarnings) { + throw new Error('Number of warnings (' + warningCount.count + ') exceeds the allowed maximum of ' + maxWarnings + '.\n'); + } }; module.exports = sassLint;