Skip to content

Instantly share code, notes, and snippets.

@shimondoodkin
Last active October 12, 2021 17:32
Show Gist options
  • Save shimondoodkin/4be6fbe63efe77abfc37a1d3670c6f1b to your computer and use it in GitHub Desktop.
Save shimondoodkin/4be6fbe63efe77abfc37a1d3670c6f1b to your computer and use it in GitHub Desktop.
react-scripts start print errors with full filename search <<< in this file to know the changes
// file: node_modules/react-dev-utils/eslintFormatter.js
//
// I had thousands of errors it is hard to visit each., here is a simple fix, to have
// filename with line number for each error
//
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
const chalk = require('chalk');
const stripAnsi = require('strip-ansi');
const table = require('text-table');
const path = require('path'); // <<<add this require
function getRelativePath(_file) { // <<<add this function
let file = path.relative(process.cwd(), _file);
if (file.startsWith('..')) {
file = _file;
} else if (!file.startsWith('.')) {
file = '.' + path.sep + file;
}
return file;
}
function isError(message) {
if (message.fatal || message.severity === 2) {
return true;
}
return false;
}
function formatter(results) {
// try { throw new Error() } catch (e){console.log(e.stack)}
let output = '\n';
let hasErrors = false;
let reportContainsErrorRuleIDs = false;
results.forEach(result => {
let messages = result.messages;
if (messages.length === 0) {
return;
}
messages = messages.map(message => {
let messageType;
if (isError(message)) {
messageType = 'error';
hasErrors = true;
if (message.ruleId) {
reportContainsErrorRuleIDs = true;
}
} else {
messageType = 'warn';
}
let line = message.line || 0;
if (message.column) {
line += ':' + message.column;
}
let position = chalk.bold('Line ' + line + ':');
// <<<add this line:
let filestr= chalk.bold(getRelativePath(result.filePath).replace(/\\/g,'/') + ':' + (message.line || 0) + (message.column ? ':' + message.column : ''));
return [
'',
filestr, // position, // <<<replace this line
messageType,
message.message.replace(/\.$/, ''),
chalk.underline(message.ruleId || ''),
];
});
// if there are error messages, we want to show only errors
if (hasErrors) {
messages = messages.filter(m => m[2] === 'error');
}
// add color to rule keywords
messages.forEach(m => {
m[4] = m[2] === 'error' ? chalk.red(m[4]) : chalk.yellow(m[4]);
m.splice(2, 1);
});
let outputTable = table(messages, {
align: ['l', 'l', 'l'],
stringLength(str) {
return stripAnsi(str).length;
},
});
output += `${outputTable}\n\n`;
});
if (reportContainsErrorRuleIDs) {
// Unlike with warnings, we have to do it here.
// We have similar code in react-scripts for warnings,
// but warnings can appear in multiple files so we only
// print it once at the end. For errors, however, we print
// it here because we always show at most one error, and
// we can only be sure it's an ESLint error before exiting
// this function.
output +=
'Search for the ' +
chalk.underline(chalk.red('keywords')) +
' to learn more about each error.';
}
return output;
}
module.exports = formatter;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment