UNPKG

stylint-stylish

Version:

A stylish reporter for Stylint, heavily inspired by jshint-stylish

92 lines (90 loc) 3.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = _default; var _path = _interopRequireDefault(require("path")); var _chalk = _interopRequireDefault(require("chalk")); var _logSymbols = _interopRequireDefault(require("log-symbols")); var _textTable = _interopRequireDefault(require("text-table")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } let currFile; let currTable = []; let filenames = []; let options; let optionsRead = false; function resetState() { currFile = undefined; currTable = []; filenames = []; options = undefined; optionsRead = false; } function createSummary(errs, warns, total, maxErrors, maxWarnings) { if (total === 0) { return 'No violations'; } let output = ''; if (errs > 0) { output += ` ${_logSymbols.default.error} ${errs} ${errs > 1 ? 'errors' : 'error'}`; if (typeof maxErrors === 'number') { output += ` (Max Errors: ${maxErrors})`; } output += '\n'; } if (warns > 0) { output += ` ${_logSymbols.default.warning} ${warns} ${warns > 1 ? 'warnings' : 'warning'}`; if (typeof maxWarnings === 'number') { output += ` (Max Warnings: ${maxWarnings})`; } output += '\n'; } return output; } function doneHandler(kill) { const errs = this.cache.errs.length; const warns = this.cache.warnings.length; const total = errs + warns; const formattedMessage = `${(0, _textTable.default)(currTable).split('\n').map((msg, i) => filenames[i] ? `\n${filenames[i]}\n${msg}` : msg).join('\n')}\n\n`; this.cache.msg = `${formattedMessage}${createSummary(errs, warns, total, this.config.maxErrors, this.config.maxWarnings)}`; if (kill === 'kill') { this.cache.msg += '\nStylint: Over Error or Warning Limit.'; } else if (total === 0) { this.cache.msg = ''; } resetState(); return this.done(); } function _default(msg, done, kill) { if (done === 'done') { return doneHandler.call(this, kill); } if (!optionsRead) { optionsRead = true; const _ref = this.config.reporterOptions || {}, absolutePath = _ref.absolutePath, verbose = _ref.verbose, ruleName = _ref.ruleName; options = { absolutePath, verbose, ruleName }; } const isWarning = this.state.severity === 'Warning'; if (currFile !== this.cache.file) { currFile = this.cache.file; let filename; if (options.absolutePath) { filename = _path.default.isAbsolute(currFile) ? currFile : _path.default.resolve(currFile); } else { filename = _path.default.isAbsolute(currFile) ? _path.default.relative(process.cwd(), currFile) : currFile; } filenames[currTable.length] = _chalk.default.underline(filename); } const column = typeof this.cache.col === 'number' ? this.cache.col : -1; const rule = options.ruleName ? _chalk.default.gray(this.cache.rule || '') : ''; currTable.push(['', _chalk.default.gray(`line ${this.cache.lineNo}`), _chalk.default.gray(column >= 0 ? `col ${column}` : '-'), `${isWarning ? _chalk.default.yellow(msg) : _chalk.default.red(msg)} ${rule}`.trim(), options.verbose ? _chalk.default.gray(this.cache.origLine.trim()) : '']); return ''; } module.exports = exports.default;