Skip to content

Commit bac0287

Browse files
johnnyreillyianschmitz
authored andcommitted
Enable click to go to error in console for TypeScript (#6502)
* reattempt changes * formatter fixes * fix missing colon in path * revert path tweaking amends as per discussion with @ianschmitz
1 parent c54e36d commit bac0287

File tree

3 files changed

+35
-38
lines changed

3 files changed

+35
-38
lines changed

packages/react-dev-utils/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ Creates a Webpack compiler instance for WebpackDevServer with built-in helpful m
332332
The `args` object accepts a number of properties:
333333

334334
- **appName** `string`: The name that will be printed to the terminal.
335-
- **config** `Object`: The webpack configuration options to be provided to the webpack constructor.
335+
- **config** `Object`: The webpack configuration options to be provided to the webpack constructor.
336336
- **devSocket** `Object`: Required if `useTypeScript` is `true`. This object should include `errors` and `warnings` which are functions accepting an array of errors or warnings emitted by the type checking. This is useful when running `fork-ts-checker-webpack-plugin` with `async: true` to report errors that are emitted after the webpack build is complete.
337337
- **urls** `Object`: To provide the `urls` argument, use `prepareUrls()` described below.
338338
- **useYarn** `boolean`: If `true`, yarn instructions will be emitted in the terminal instead of npm.

packages/react-dev-utils/formatWebpackMessages.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function isLikelyASyntaxError(message) {
1616

1717
// Cleans up webpack error messages.
1818
// eslint-disable-next-line no-unused-vars
19-
function formatMessage(message, isError) {
19+
function formatMessage(message) {
2020
let lines = message.split('\n');
2121

2222
// Strip Webpack-added headers off errors/warnings

packages/react-dev-utils/typescriptFormatter.js

+33-36
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,45 @@ const codeFrame = require('@babel/code-frame').codeFrameColumns;
1212
const chalk = require('chalk');
1313
const fs = require('fs');
1414

15+
const types = { diagnostic: 'TypeScript', lint: 'TSLint' };
16+
1517
function formatter(message, useColors) {
16-
const hasGetters = typeof message.getFile === 'function';
18+
const { type, severity, file, line, content, code, character } =
19+
typeof message.getFile === 'function'
20+
? {
21+
type: message.getType(),
22+
severity: message.getSeverity(),
23+
file: message.getFile(),
24+
line: message.getLine(),
25+
content: message.getContent(),
26+
code: message.getCode(),
27+
character: message.getCharacter(),
28+
}
29+
: message;
30+
1731
const colors = new chalk.constructor({ enabled: useColors });
1832
const messageColor = message.isWarningSeverity() ? colors.yellow : colors.red;
19-
20-
let source;
21-
22-
if (hasGetters) {
23-
source =
24-
message.getFile() &&
25-
fs.existsSync(message.getFile()) &&
26-
fs.readFileSync(message.getFile(), 'utf-8');
27-
} else {
28-
source =
29-
message.file &&
30-
fs.existsSync(message.file) &&
31-
fs.readFileSync(message.file, 'utf-8');
32-
}
33-
34-
let frame = '';
35-
36-
if (source) {
37-
frame = codeFrame(
38-
source,
39-
{ start: { line: message.line, column: message.character } },
40-
{ highlightCode: useColors }
41-
)
42-
.split('\n')
43-
.map(str => ' ' + str)
44-
.join(os.EOL);
45-
}
46-
47-
const severity = hasGetters ? message.getSeverity() : message.severity;
48-
const types = { diagnostic: 'TypeScript', lint: 'TSLint' };
33+
const fileAndNumberColor = colors.bold.cyan;
34+
35+
const source = file && fs.existsSync(file) && fs.readFileSync(file, 'utf-8');
36+
const frame = source
37+
? codeFrame(
38+
source,
39+
{ start: { line: line, column: character } },
40+
{ highlightCode: useColors }
41+
)
42+
.split('\n')
43+
.map(str => ' ' + str)
44+
.join(os.EOL)
45+
: '';
4946

5047
return [
51-
messageColor.bold(`${types[message.type]} ${severity.toLowerCase()}: `) +
52-
(hasGetters ? message.getContent() : message.content) +
48+
messageColor.bold(`${types[type]} ${severity.toLowerCase()} in `) +
49+
fileAndNumberColor(`${file}(${line},${character})`) +
50+
messageColor(':'),
51+
content +
5352
' ' +
54-
messageColor.underline(
55-
(message.type === 'lint' ? 'Rule: ' : 'TS') + message.code
56-
),
53+
messageColor.underline((type === 'lint' ? 'Rule: ' : 'TS') + code),
5754
'',
5855
frame,
5956
].join(os.EOL);

0 commit comments

Comments
 (0)