Skip to content

Commit 478a970

Browse files
authored
Improve errors location tracking (#14130)
1 parent a6ca39c commit 478a970

File tree

77 files changed

+2140
-1141
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+2140
-1141
lines changed

eslint/babel-eslint-plugin-development-internal/src/rules/dry-error-messages.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export default {
146146
"CallExpression[callee.type='MemberExpression'][callee.object.type='ThisExpression'][callee.property.name='raise'][arguments.length>=2]"(
147147
node,
148148
) {
149-
const [, errorMsgNode] = node.arguments;
149+
const [errorMsgNode] = node.arguments;
150150
const nodesToCheck = findIdNodes(errorMsgNode);
151151

152152
if (

eslint/babel-eslint-plugin-development-internal/test/rules/dry-error-messages.js

Lines changed: 72 additions & 72 deletions
Large diffs are not rendered by default.

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

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// @flow
22
/* eslint sort-keys: "error" */
3-
import { getLineInfo, type Position } from "../util/location";
3+
import { type Position, indexes } from "../util/location";
44
import CommentsParser from "./comments";
55
import { type ErrorCode, ErrorCodes } from "./error-codes";
6+
import { type Node } from "../types";
67

78
// This function is used to raise exceptions on parse errors. It
89
// takes an offset integer (into the current `input`) to indicate
@@ -28,7 +29,14 @@ export type ErrorTemplates = {
2829
[key: string]: ErrorTemplate,
2930
};
3031

31-
type SyntaxPlugin = "flow" | "typescript" | "jsx" | typeof undefined;
32+
type Origin = {| node: Node |} | {| at: Position |};
33+
34+
type SyntaxPlugin =
35+
| "flow"
36+
| "typescript"
37+
| "jsx"
38+
| "placeholders"
39+
| typeof undefined;
3240

3341
function keepReasonCodeCompat(reasonCode: string, syntaxPlugin: SyntaxPlugin) {
3442
if (!process.env.BABEL_8_BREAKING) {
@@ -64,31 +72,26 @@ export {
6472
SourceTypeModuleErrorMessages as SourceTypeModuleErrors,
6573
} from "./error-message";
6674

67-
export type raiseFunction = (number, ErrorTemplate, ...any) => void;
75+
export type raiseFunction = (ErrorTemplate, Origin, ...any) => void;
76+
export type ErrorData = {| message: ErrorTemplate, loc: Position |};
6877

6978
export default class ParserError extends CommentsParser {
7079
// Forward-declaration: defined in tokenizer/index.js
7180
/*::
7281
+isLookahead: boolean;
7382
*/
7483

75-
getLocationForPosition(pos: number): Position {
76-
let loc;
77-
if (pos === this.state.start) loc = this.state.startLoc;
78-
else if (pos === this.state.lastTokStart) loc = this.state.lastTokStartLoc;
79-
else if (pos === this.state.end) loc = this.state.endLoc;
80-
else if (pos === this.state.lastTokEnd) loc = this.state.lastTokEndLoc;
81-
else loc = getLineInfo(this.input, pos);
82-
83-
return loc;
84-
}
85-
8684
raise(
87-
pos: number,
8885
{ code, reasonCode, template }: ErrorTemplate,
86+
origin: Origin,
8987
...params: any
9088
): Error | empty {
91-
return this.raiseWithData(pos, { code, reasonCode }, template, ...params);
89+
return this.raiseWithData(
90+
origin.node ? origin.node.loc.start : origin.at,
91+
{ code, reasonCode },
92+
template,
93+
...params,
94+
);
9295
}
9396

9497
/**
@@ -104,11 +107,12 @@ export default class ParserError extends CommentsParser {
104107
* @memberof ParserError
105108
*/
106109
raiseOverwrite(
107-
pos: number,
110+
loc: Position,
108111
{ code, template }: ErrorTemplate,
109112
...params: any
110113
): Error | empty {
111-
const loc = this.getLocationForPosition(pos);
114+
// $FlowIgnore[incompatible-type] We know this exists, so it can't be undefined.
115+
const pos: number = indexes.get(loc);
112116
const message =
113117
template.replace(/%(\d+)/g, (_, i: number) => params[i]) +
114118
` (${loc.line}:${loc.column})`;
@@ -127,15 +131,15 @@ export default class ParserError extends CommentsParser {
127131
}
128132

129133
raiseWithData(
130-
pos: number,
134+
loc: Position,
131135
data?: {
132136
missingPlugin?: Array<string>,
133137
code?: string,
134138
},
135139
errorTemplate: string,
136140
...params: any
137141
): Error | empty {
138-
const loc = this.getLocationForPosition(pos);
142+
const pos = indexes.get(loc);
139143
const message =
140144
errorTemplate.replace(/%(\d+)/g, (_, i: number) => params[i]) +
141145
` (${loc.line}:${loc.column})`;

0 commit comments

Comments
 (0)