Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
As suggested by @Timer, appPath is now excluded from reported error
  • Loading branch information
johnnyreilly committed Nov 3, 2018
commit 372bc87aa83dc7f8e8142cec87e078459b476c60
73 changes: 40 additions & 33 deletions packages/react-dev-utils/typescriptFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,47 @@ const codeFrame = require('@babel/code-frame').codeFrameColumns;
const chalk = require('chalk');
const fs = require('fs');

function formatter(message, useColors) {
const colors = new chalk.constructor({ enabled: useColors });
const messageColor = message.isWarningSeverity() ? colors.yellow : colors.red;
const fileAndNumberColor = colors.bold.cyan;

const source =
message.getFile() &&
fs.existsSync(message.getFile()) &&
fs.readFileSync(message.getFile(), 'utf-8');
let frame = '';
function makeFormatter(appPath) {
return function formatter(message, useColors) {
const colors = new chalk.constructor({ enabled: useColors });
const messageColor = message.isWarningSeverity()
? colors.yellow
: colors.red;
const fileAndNumberColor = colors.bold.cyan;

if (source) {
frame = codeFrame(
source,
{ start: { line: message.line, column: message.character } },
{ highlightCode: useColors }
)
.split('\n')
.map(str => ' ' + str)
.join(os.EOL);
}
const source =
message.getFile() &&
fs.existsSync(message.getFile()) &&
fs.readFileSync(message.getFile(), 'utf-8');
let frame = '';

return [
messageColor.bold(`Type ${message.getSeverity().toLowerCase()} in `) +
fileAndNumberColor(
`${message.getFile()}(${message.getLine()},${message.getCharacter()})`
) +
messageColor(':'),
message.getContent() +
' ' +
messageColor.underline(`TS${message.code}`),
'',
frame,
].join(os.EOL);
if (source) {
frame = codeFrame(
source,
{ start: { line: message.line, column: message.character } },
{ highlightCode: useColors }
)
.split('\n')
.map(str => ' ' + str)
.join(os.EOL);
}

return [
messageColor.bold(`Type ${message.getSeverity().toLowerCase()} in `) +
fileAndNumberColor(
`${message
.getFile()
.replace(
appPath,
''
)}(${message.getLine()},${message.getCharacter()})`
) +
messageColor(':'),
message.getContent() + ' ' + messageColor.underline(`TS${message.code}`),
'',
frame,
].join(os.EOL);
};
}

module.exports = formatter;
module.exports = makeFormatter;
3 changes: 2 additions & 1 deletion packages/react-scripts/config/webpack.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,8 @@ module.exports = {
],
watch: paths.appSrc,
silent: true,
formatter: typescriptFormatter,
// Create formatter which displays filepath with appPath excluded
formatter: typescriptFormatter(paths.appPath.replace(/\\/g, '/')),
}),
].filter(Boolean),

Expand Down