Skip to content
This repository has been archived by the owner on May 26, 2023. It is now read-only.

Commit

Permalink
Add formatters for cli output
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt VanNuys authored and vannuysm committed Sep 1, 2017
1 parent fb30256 commit 641d082
Show file tree
Hide file tree
Showing 10 changed files with 445 additions and 9 deletions.
29 changes: 29 additions & 0 deletions lib/cli-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,35 @@ class CLIEngine {
warningCount: stats.warningCount
};
}

/**
* Returns the formatter representing the given format or null if no formatter
* with the given name can be found.
* @param {string} [format] The name of the format to load or the path to a
* custom formatter.
* @returns {Function} The formatter function or null if not found.
*/
getFormatter(format) {
// default is stylish
format = format || 'stylish';

// only strings are valid formatters
if (typeof format !== 'string') {
return null;
}

const formatterPath = `./formatters/${format}`;

try {
return require(formatterPath);
}
catch (e) {
e.message = `There was a problem loading formatter: ${formatterPath}\nError: ${e.message}`;
throw e;
}
}
}

CLIEngine.getFormatter = CLIEngine.prototype.getFormatter;

module.exports = CLIEngine;
41 changes: 36 additions & 5 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,43 @@ const options = require('./options'),
CLIEngine = require("./cli-engine"),
log = require('./logging');

/**
* Outputs the results of the linting.
* @param {CLIEngine} engine The CLIEngine to use.
* @param {LintResult[]} results The results to print.
* @param {string} format The name of the formatter to use or the path to the formatter.
* @returns {boolean} True if the printing succeeds, false if not.
* @private
*/
function printResults(engine, results, format) {
let formatter;

try {
formatter = engine.getFormatter(format);
}
catch (e) {
log.error(e.message);
return false;
}

const output = formatter(results);

if (output) {
log.info(output);
}

return true;
}

const cli = {
execute(args) {
let currentOptions;

try {
currentOptions = options.parse(args);
} catch (error) {
console.error(error.message);
}
catch (e) {
console.error(e.message);
return 1;
}

Expand All @@ -21,12 +50,14 @@ const cli = {
log.info(options.generateHelp());
}
else {

const engine = new CLIEngine(currentOptions);
const report = engine.executeOnFiles(files);

log.info(JSON.stringify(report.results));
return report.errorCount > 0 ? 1 : 0;
if (printResults(engine, report.results, currentOptions.format)) {
return report.errorCount ? 1 : 0;
}

return 1;
}

return 0;
Expand Down
60 changes: 60 additions & 0 deletions lib/formatters/compact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @fileoverview Compact reporter
* @author Nicholas C. Zakas
*/
"use strict";

//------------------------------------------------------------------------------
// Helper Functions
//------------------------------------------------------------------------------

/**
* Returns the severity of warning or error
* @param {Object} message message object to examine
* @returns {string} severity level
* @private
*/
function getMessageType(message) {
if (message.fatal || message.severity === 2) {
return "Error";
}
return "Warning";

}


//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------

module.exports = function(results) {

let output = "",
total = 0;

results.forEach(result => {

const messages = result.messages;

total += messages.length;

messages.forEach(message => {

output += `${result.filePath}: `;
output += `line ${message.line || 0}`;
output += `, col ${message.column || 0}`;
output += `, ${getMessageType(message)}`;
output += ` - ${message.message}`;
output += message.ruleId ? ` (${message.ruleId})` : "";
output += "\n";

});

});

if (total > 0) {
output += `\n${total} problem${total !== 1 ? "s" : ""}`;
}

return output;
};
13 changes: 13 additions & 0 deletions lib/formatters/json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @fileoverview JSON reporter
* @author Burak Yigit Kaya aka BYK
*/
"use strict";

//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------

module.exports = function(results) {
return JSON.stringify(results);
};
112 changes: 112 additions & 0 deletions lib/formatters/stylish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
Copyright JS Foundation and other contributors, https://js.foundation
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

/*
* This code is taken from the eslint repo
* (https://github.com/eslint/eslint)
* and modified to meet our needs.
*/

'use strict';

const chalk = require('chalk'),
stripAnsi = require('strip-ansi'),
table = require('text-table');

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

/**
* Given a word and a count, append an s if count is not one.
* @param {string} word A word in its singular form.
* @param {int} count A number controlling whether word should be pluralized.
* @returns {string} The original word with an s on the end if count is not one.
*/
function pluralize(word, count) {
return (count === 1 ? word : `${word}s`);
}

//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------

module.exports = function(results) {

let output = '\n',
errorCount = 0,
warningCount = 0,
summaryColor = 'yellow';

results.forEach(result => {
const messages = result.messages;

if (messages.length === 0) {
return;
}

errorCount += result.errorCount;
warningCount += result.warningCount;

output += `${chalk.underline(result.filePath)}\n`;

output += `${table(
messages.map(message => {
let messageType;
if (message.fatal || message.severity === 2) {
messageType = chalk.red('error');
summaryColor = 'red';
} else {
messageType = chalk.yellow('warning');
}
return [
'',
message.line || 0,
message.column || 0,
messageType,
message.message.replace(/\.$/, ''),
chalk.dim(message.ruleId || '')
];
}),
{
align: ['', 'r', 'l'],
stringLength(str) {
return stripAnsi(str).length;
}
}
).split('\n').map(el => el.replace(/(\d+)\s+(\d+)/, (m, p1, p2) => chalk.dim(`${p1}:${p2}`))).join('\n')}\n\n`;
});

const total = errorCount + warningCount;

if (total > 0) {
output += chalk[summaryColor].bold([
'\u2716 ', total, pluralize(' problem', total),
' (', errorCount, pluralize(' error', errorCount), ', ',
warningCount, pluralize(' warning', warningCount), ')\n'
].join(''));
}

return total > 0 ? output : '';
};
58 changes: 58 additions & 0 deletions lib/formatters/unix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* @fileoverview unix-style formatter.
* @author oshi-shinobu
*/
"use strict";

//------------------------------------------------------------------------------
// Helper Functions
//------------------------------------------------------------------------------

/**
* Returns a canonical error level string based upon the error message passed in.
* @param {Object} message Individual error message provided by eslint
* @returns {string} Error level string
*/
function getMessageType(message) {
if (message.fatal || message.severity === 2) {
return "Error";
}
return "Warning";

}


//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------

module.exports = function(results) {

let output = "",
total = 0;

results.forEach(result => {

const messages = result.messages;

total += messages.length;

messages.forEach(message => {

output += `${result.filePath}:`;
output += `${message.line || 0}:`;
output += `${message.column || 0}:`;
output += ` ${message.message} `;
output += `[${getMessageType(message)}${message.ruleId ? `/${message.ruleId}` : ""}]`;
output += "\n";

});

});

if (total > 0) {
output += `\n${total} problem${total !== 1 ? "s" : ""}`;
}

return output;
};
Loading

0 comments on commit 641d082

Please sign in to comment.