Skip to content

Commit 51236e9

Browse files
committed
Add click-to-open support for lint errors and refactor parser
1 parent 830c4e9 commit 51236e9

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

packages/react-error-overlay/src/utils/parseCompileError.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// @flow
2+
import Anser from 'anser';
23

34
export type ErrorLocation = {|
45
filePath: string,
@@ -7,9 +8,16 @@ export type ErrorLocation = {|
78

89
const filePathRegex = /^\.(\/[^\/\n ]+)+\.[^\/\n ]+$/;
910

10-
// Based on syntax error formating of babylon parser
11-
// https://github.com/babel/babylon/blob/v7.0.0-beta.22/src/parser/location.js#L19
12-
const lineNumberRegex = /^.*\((\d+):(\d+)\)$/;
11+
const lineNumberRegexes = [
12+
// Babel syntax errors
13+
// Based on syntax error formating of babylon parser
14+
// https://github.com/babel/babylon/blob/v7.0.0-beta.22/src/parser/location.js#L19
15+
/^.*\((\d+):(\d+)\)$/,
16+
17+
// ESLint errors
18+
// Based on eslintFormatter in react-dev-utils
19+
/^Line (\d+):.+$/,
20+
];
1321

1422
// Based on error formatting of webpack
1523
// https://github.com/webpack/webpack/blob/v3.5.5/lib/Stats.js#L183-L217
@@ -19,18 +27,21 @@ function parseCompileError(message: string): ?ErrorLocation {
1927
let lineNumber: number = 0;
2028

2129
for (let i = 0; i < lines.length; i++) {
22-
const line: string = lines[i];
30+
const line: string = Anser.ansiToText(lines[i]).trim();
2331
if (!line) continue;
2432

2533
if (!filePath && line.match(filePathRegex)) {
2634
filePath = line;
2735
}
2836

29-
if (!lineNumber) {
30-
const match: ?Array<string> = line.match(lineNumberRegex);
37+
let k = 0;
38+
while (k < lineNumberRegexes.length) {
39+
const match: ?Array<string> = line.match(lineNumberRegexes[k]);
3140
if (match) {
3241
lineNumber = parseInt(match[1]);
42+
break;
3343
}
44+
k++;
3445
}
3546

3647
if (filePath && lineNumber) break;

0 commit comments

Comments
 (0)