summaryrefslogtreecommitdiff
path: root/tools/node_modules/eslint/lib/formatters/codeframe.js
diff options
context:
space:
mode:
authorMichaël Zasso <targos@protonmail.com>2017-12-22 16:53:42 +0100
committerMichaël Zasso <targos@protonmail.com>2018-01-11 09:48:05 +0100
commit3dc30632755713179f345f4af024bd904c6162d0 (patch)
treef28c4f6dd6dfc5992edf301449d1a371d229755b /tools/node_modules/eslint/lib/formatters/codeframe.js
parenta2c7085dd4a8e60d1a47572aca8bb6fcb7a32f88 (diff)
downloadandroid-node-v8-3dc30632755713179f345f4af024bd904c6162d0.tar.gz
android-node-v8-3dc30632755713179f345f4af024bd904c6162d0.tar.bz2
android-node-v8-3dc30632755713179f345f4af024bd904c6162d0.zip
tools: move eslint from tools to tools/node_modules
This is required because we need to add the babel-eslint dependency and it has to be able to resolve "eslint". babel-eslint is required to support future ES features such as async iterators and import.meta. Refs: https://github.com/nodejs/node/pull/17755 PR-URL: https://github.com/nodejs/node/pull/17820 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Diffstat (limited to 'tools/node_modules/eslint/lib/formatters/codeframe.js')
-rw-r--r--tools/node_modules/eslint/lib/formatters/codeframe.js138
1 files changed, 138 insertions, 0 deletions
diff --git a/tools/node_modules/eslint/lib/formatters/codeframe.js b/tools/node_modules/eslint/lib/formatters/codeframe.js
new file mode 100644
index 0000000000..0b97a0d818
--- /dev/null
+++ b/tools/node_modules/eslint/lib/formatters/codeframe.js
@@ -0,0 +1,138 @@
+/**
+ * @fileoverview Codeframe reporter
+ * @author Vitor Balocco
+ */
+"use strict";
+
+const chalk = require("chalk");
+const codeFrame = require("babel-code-frame");
+const path = require("path");
+
+//------------------------------------------------------------------------------
+// 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 {number} 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`);
+}
+
+/**
+ * Gets a formatted relative file path from an absolute path and a line/column in the file.
+ * @param {string} filePath The absolute file path to format.
+ * @param {number} line The line from the file to use for formatting.
+ * @param {number} column The column from the file to use for formatting.
+ * @returns {string} The formatted file path.
+ */
+function formatFilePath(filePath, line, column) {
+ let relPath = path.relative(process.cwd(), filePath);
+
+ if (line && column) {
+ relPath += `:${line}:${column}`;
+ }
+
+ return chalk.green(relPath);
+}
+
+/**
+ * Gets the formatted output for a given message.
+ * @param {Object} message The object that represents this message.
+ * @param {Object} parentResult The result object that this message belongs to.
+ * @returns {string} The formatted output.
+ */
+function formatMessage(message, parentResult) {
+ const type = (message.fatal || message.severity === 2) ? chalk.red("error") : chalk.yellow("warning");
+ const msg = `${chalk.bold(message.message.replace(/([^ ])\.$/, "$1"))}`;
+ const ruleId = message.fatal ? "" : chalk.dim(`(${message.ruleId})`);
+ const filePath = formatFilePath(parentResult.filePath, message.line, message.column);
+ const sourceCode = parentResult.output ? parentResult.output : parentResult.source;
+
+ const firstLine = [
+ `${type}:`,
+ `${msg}`,
+ ruleId ? `${ruleId}` : "",
+ sourceCode ? `at ${filePath}:` : `at ${filePath}`
+ ].filter(String).join(" ");
+
+ const result = [firstLine];
+
+ if (sourceCode) {
+ result.push(
+ codeFrame(sourceCode, message.line, message.column, { highlightCode: false })
+ );
+ }
+
+ return result.join("\n");
+}
+
+/**
+ * Gets the formatted output summary for a given number of errors and warnings.
+ * @param {number} errors The number of errors.
+ * @param {number} warnings The number of warnings.
+ * @param {number} fixableErrors The number of fixable errors.
+ * @param {number} fixableWarnings The number of fixable warnings.
+ * @returns {string} The formatted output summary.
+ */
+function formatSummary(errors, warnings, fixableErrors, fixableWarnings) {
+ const summaryColor = errors > 0 ? "red" : "yellow";
+ const summary = [];
+ const fixablesSummary = [];
+
+ if (errors > 0) {
+ summary.push(`${errors} ${pluralize("error", errors)}`);
+ }
+
+ if (warnings > 0) {
+ summary.push(`${warnings} ${pluralize("warning", warnings)}`);
+ }
+
+ if (fixableErrors > 0) {
+ fixablesSummary.push(`${fixableErrors} ${pluralize("error", fixableErrors)}`);
+ }
+
+ if (fixableWarnings > 0) {
+ fixablesSummary.push(`${fixableWarnings} ${pluralize("warning", fixableWarnings)}`);
+ }
+
+ let output = chalk[summaryColor].bold(`${summary.join(" and ")} found.`);
+
+ if (fixableErrors || fixableWarnings) {
+ output += chalk[summaryColor].bold(`\n${fixablesSummary.join(" and ")} potentially fixable with the \`--fix\` option.`);
+ }
+
+ return output;
+}
+
+//------------------------------------------------------------------------------
+// Public Interface
+//------------------------------------------------------------------------------
+
+module.exports = function(results) {
+ let errors = 0;
+ let warnings = 0;
+ let fixableErrors = 0;
+ let fixableWarnings = 0;
+
+ const resultsWithMessages = results.filter(result => result.messages.length > 0);
+
+ let output = resultsWithMessages.reduce((resultsOutput, result) => {
+ const messages = result.messages.map(message => `${formatMessage(message, result)}\n\n`);
+
+ errors += result.errorCount;
+ warnings += result.warningCount;
+ fixableErrors += result.fixableErrorCount;
+ fixableWarnings += result.fixableWarningCount;
+
+ return resultsOutput.concat(messages);
+ }, []).join("\n");
+
+ output += "\n";
+ output += formatSummary(errors, warnings, fixableErrors, fixableWarnings);
+
+ return (errors + warnings) > 0 ? output : "";
+};