Skip to content

Commit 5410da0

Browse files
authored
Add TypeScript error formatting (facebook#5529)
1 parent 8165714 commit 5410da0

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

packages/react-dev-utils/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"openChrome.applescript",
3737
"printBuildError.js",
3838
"printHostingInstructions.js",
39+
"typescriptFormatter.js",
3940
"WatchMissingNodeModulesPlugin.js",
4041
"WebpackDevServerUtils.js",
4142
"webpackHotDevClient.js"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
'use strict';
9+
10+
const os = require('os');
11+
const codeFrame = require('@babel/code-frame').codeFrameColumns;
12+
const chalk = require('chalk');
13+
const fs = require('fs');
14+
15+
function formatter(message, useColors) {
16+
const colors = new chalk.constructor({ enabled: useColors });
17+
const messageColor = message.isWarningSeverity() ? colors.yellow : colors.red;
18+
19+
const source =
20+
message.getFile() &&
21+
fs.existsSync(message.getFile()) &&
22+
fs.readFileSync(message.getFile(), 'utf-8');
23+
let frame = '';
24+
25+
if (source) {
26+
frame = codeFrame(
27+
source,
28+
{ start: { line: message.line, column: message.character } },
29+
{ highlightCode: useColors }
30+
)
31+
.split('\n')
32+
.map(str => ' ' + str)
33+
.join(os.EOL);
34+
}
35+
36+
return [
37+
messageColor.bold(`Type ${message.getSeverity().toLowerCase()}: `) +
38+
message.getContent() +
39+
' ' +
40+
messageColor.underline(`TS${message.code}`),
41+
'',
42+
frame,
43+
].join(os.EOL);
44+
}
45+
46+
module.exports = formatter;

packages/react-scripts/config/webpack.config.dev.js

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const paths = require('./paths');
2424
const ManifestPlugin = require('webpack-manifest-plugin');
2525
const ModuleNotFoundPlugin = require('react-dev-utils/ModuleNotFoundPlugin');
2626
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin-alt');
27+
const typescriptFormatter = require('react-dev-utils/typescriptFormatter');
2728
// @remove-on-eject-begin
2829
const getCacheIdentifier = require('react-dev-utils/getCacheIdentifier');
2930
// @remove-on-eject-end
@@ -419,6 +420,8 @@ module.exports = {
419420
checkSyntacticErrors: true,
420421
tsconfig: paths.appTsConfig,
421422
watch: paths.appSrc,
423+
silent: true,
424+
formatter: typescriptFormatter,
422425
}),
423426
].filter(Boolean),
424427

packages/react-scripts/config/webpack.config.prod.js

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const paths = require('./paths');
2828
const getClientEnvironment = require('./env');
2929
const ModuleNotFoundPlugin = require('react-dev-utils/ModuleNotFoundPlugin');
3030
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin-alt');
31+
const typescriptFormatter = require('react-dev-utils/typescriptFormatter');
3132
// @remove-on-eject-begin
3233
const getCacheIdentifier = require('react-dev-utils/getCacheIdentifier');
3334
// @remove-on-eject-end
@@ -539,6 +540,8 @@ module.exports = {
539540
checkSyntacticErrors: true,
540541
tsconfig: paths.appTsConfig,
541542
watch: paths.appSrc,
543+
silent: true,
544+
formatter: typescriptFormatter,
542545
}),
543546
].filter(Boolean),
544547
// Some libraries import Node modules but don't use them in the browser.

0 commit comments

Comments
 (0)