8000 refactor: split locationParser into ParserErrors and error message (#… · babel/babel@b5c4a46 · GitHub
[go: up one dir, main page]

Skip to content

Commit b5c4a46

Browse files
authored
refactor: split locationParser into ParserErrors and error message (#11653)
1 parent 15d6da0 commit b5c4a46

File tree

13 files changed

+82
-78
lines changed

13 files changed

+82
-78
lines changed

packages/babel-parser/src/parser/location.js renamed to packages/babel-parser/src/parser/error-message.js

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,8 @@
11
// @flow
22
/* eslint sort-keys: "error" */
3-
import { getLineInfo, type Position } from "../util/location";
4-
import CommentsParser from "./comments";
5-
6-
// This function is used to raise exceptions on parse errors. It
7-
// takes an offset integer (into the current `input`) to indicate
8-
// the location of the error, attaches the position to the end
9-
// of the error message, and then raises a `SyntaxError` with that
10-
// message.
11-
12-
type ErrorContext = {
13-
pos: number,
14-
loc: Position,
15-
missingPlugin?: Array<string>,
16-
code?: string,
17-
};
183

194
// The Errors key follows https://cs.chromium.org/chromium/src/v8/src/common/message-template.h unless it does not exist
20-
export const Errors = Object.freeze({
5+
export const ErrorMessages = Object.freeze({
216
ArgumentsDisallowedInInitializer:
227
"'arguments' is not allowed in class field initializer",
238
AsyncFunctionInSingleStatementContext:
@@ -205,53 +190,3 @@ export const Errors = Object.freeze({
205190
ZeroDigitNumericSeparator:
206191
"Numeric separator can not be used after leading 0",
207192
});
208-
209-
export default class LocationParser extends CommentsParser {
210-
// Forward-declaration: defined in tokenizer/index.js
211-
/*::
212-
+isLookahead: boolean;
213-
*/
214-
215-
getLocationForPosition(pos: number): Position {
216-
let loc;
217-
if (pos === this.state.start) loc = this.state.startLoc;
218-
else if (pos === this.state.lastTokStart) loc = this.state.lastTokStartLoc;
219-
else if (pos === this.state.end) loc = this.state.endLoc;
220-
else if (pos === this.state.lastTokEnd) loc = this.state.lastTokEndLoc;
221-
else loc = getLineInfo(this.input, pos);
222-
223-
return loc;
224-
}
225-
226-
raise(pos: number, errorTemplate: string, ...params: any): Error | empty {
227-
return this.raiseWithData(pos, undefined, errorTemplate, ...params);
228-
}
229-
230-
raiseWithData(
231-
pos: number,
232-
data?: {
233-
missingPlugin?: Array<string>,
234-
code?: string,
235-
},
236-
errorTemplate: string,
237-
...params: any
238-
): Error | empty {
239-
const loc = this.getLocationForPosition(pos);
240-
const message =
241-
errorTemplate.replace(/%(\d+)/g, (_, i: number) => params[i]) +
242-
` (${loc.line}:${loc.column})`;
243-
return this._raise(Object.assign(({ loc, pos }: Object), data), message);
244-
}
245-
246-
_raise(errorContext: ErrorContext, message: string): Error | empty {
247-
// $FlowIgnore
248-
const err: SyntaxError & ErrorContext = new SyntaxError(message);
249-
Object.assign(err, errorContext);
250-
if (this.options.errorRecovery) {
251-
if (!this.isLookahead) this.state.errors.push(err);
252-
return err;
253-
} else {
254-
throw err;
255-
}
256-
}
257-
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// @flow
2+
/* eslint sort-keys: "error" */
3+
import { getLineInfo, type Position } from "../util/location";
4+
import CommentsParser from "./comments";
5+
6+
// This function is used to raise exceptions on parse errors. It
7+
// takes an offset integer (into the current `input`) to indicate
8+
// the location of the error, attaches the position to the end
9+
// of the error message, and then raises a `SyntaxError` with that
10+
// message.
11+
12+
type ErrorContext = {
13+
pos: number,
14+
loc: Position,
15+
missingPlugin?: Array<string>,
16+
code?: string,
17+
};
18+
19+
export { ErrorMessages as Errors } from "./error-message.js";
20+
21+
export default class ParserError extends CommentsParser {
22+
// Forward-declaration: defined in tokenizer/index.js
23+
/*::
24+
+isLookahead: boolean;
25+
*/
26+
27+
getLocationForPosition(pos: number): Position {
28+
let loc;
29+
if (pos === this.state.start) loc = this.state.startLoc;
30+
else if (pos === this.state.lastTokStart) loc = this.state.lastTokStartLoc;
31+
else if (pos === this.state.end) loc = this.state.endLoc;
32+
else if (pos === this.state.lastTokEnd) loc = this.state.lastTokEndLoc;
33+
else loc = getLineInfo(this.input, pos);
34+
35+
return loc;
36+
}
37+
38+
raise(pos: number, errorTemplate: string, ...params: any): Error | empty {
39+
return this.raiseWithData(pos, undefined, errorTemplate, ...params);
40+
}
41+
42+
raiseWithData(
43+
pos: number,
44+
data?: {
45+
missingPlugin?: Array<string>,
46+
code?: string,
47+
},
48+
errorTemplate: string,
49+
...params: any
50+
): Error | empty { A93C
51+
const loc = this.getLocationForPosition(pos);
52+
const message =
53+
errorTemplate.replace(/%(\d+)/g, (_, i: number) => params[i]) +
54+
` (${loc.line}:${loc.column})`;
55+
return this._raise(Object.assign(({ loc, pos }: Object), data), message);
56+
}
57+
58+
_raise(errorContext: ErrorContext, message: string): Error | empty {
59+
// $FlowIgnore
60+
const err: SyntaxError & ErrorContext = new SyntaxError(message);
61+
Object.assign(err, errorContext);
62+
if (this.options.errorRecovery) {
63+
if (!this.isLookahead) this.state.errors.push(err);
64+
return err;
65+
} else {
66+
throw err;
67+
}
68+
}
69+
}

packages/babel-parser/src/parser/expression.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import {
4848
PARAM,
4949
functionFlags,
5050
} from "../util/production-parameter";
51-
import { Errors } from "./location";
51+
import { Errors } from "./error";
5252

5353
export default class ExpressionParser extends LValParser {
5454
// Forward-declaration: defined in statement.js

packages/babel-parser/src/parser/lval.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
import { NodeUtils } from "./node";
2323
import { type BindingTypes, BIND_NONE } from "../util/scopeflags";
2424
import { ExpressionErrors } from "./util";
25-
import { Errors } from "./location";
25+
import { Errors } from "./error";
2626

2727
const unwrapParenthesizedExpression = (node: Node) => {
2828
return node.type === "ParenthesizedExpression"

packages/babel-parser/src/parser/statement.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import * as N from "../types";
44
import { types as tt, type TokenType } from "../tokenizer/types";
55
import ExpressionParser from "./expression";
6-
import { Errors } from "./location";
6+
import { Errors } from "./error";
77
import {
88
isIdentifierChar,
99
isIdentifierStart,

packages/babel-parser/src/parser/util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { Node } from "../types";
77
import { lineBreak } from "../util/whitespace";
88
import { isIdentifierChar } from "../util/identifier";
99
import * as charCodes from "charcodes";
10-
import { Errors } from "./location";
10+
import { Errors } from "./error";
1111

1212
type TryParse<Node, Error, Thrown, Aborted, FailState> = {
1313
node: Node,

packages/babel-parser/src/plugins/estree.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { ExpressionErrors } from "../parser/util";
66
import * as N from "../types";
77
import type { Position } from "../util/location";
88
import { type BindingTypes, BIND_NONE } from "../util/scopeflags";
9-
import { Errors } from "../parser/location";
9+
import { Errors } from "../parser/error";
1010

1111
function isSimpleProperty(node: N.Node): boolean {
1212
return (

packages/babel-parser/src/plugins/flow.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
SCOPE_OTHER,
2323
} from "../util/scopeflags";
2424
import type { ExpressionErrors } from "../parser/util";
25-
import { Errors } from "../parser/location";
25+
import { Errors } from "../parser/error";
2626

2727
const reservedTypes = new Set([
2828
"_",

packages/babel-parser/src/plugins/jsx/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import * as N from "../../types";
1111
import { isIdentifierChar, isIdentifierStart } from "../../util/identifier";
1212
import type { Position } from "../../util/location";
1313
import { isNewLine } from "../../util/whitespace";
14-
import { Errors } from "../../parser/location";
14+
import { Errors } from "../../parser/error";
1515

1616
const HEX_NUMBER = /^[\da-fA-F]+$/;
1717
const DECIMAL_NUMBER = /^\d+$/;

packages/babel-parser/src/plugins/typescript/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import TypeScriptScopeHandler from "./scope";
2727
import * as charCodes from "charcodes";
2828
import type { ExpressionErrors } from "../../parser/util";
2929
import { PARAM } from "../../util/production-parameter";
30-
import { Errors } from "../../parser/location";
30+
import { Errors } from "../../parser/error";
3131

3232
type TsModifier =
3333
| "readonly"

0 commit comments

Comments
 (0)
0