refactor: raise AwaitNotInAsyncContext when an AwaitExpression will be parsed (#12716)
* refactor: raise AwaitNotInAsyncContext when an AwaitExpression will be parsed * tweak logic * early exit when errors are before thrown error position * fix: always return true in assert.throws callback See https://nodejs.org/api/assert.html#assert_assert_throws_fn_error_message * update test fixtures * fix flow error
This commit is contained in:
@@ -16,6 +16,8 @@ type ErrorContext = {
|
||||
code?: string,
|
||||
};
|
||||
|
||||
export type ParsingError = SyntaxError & ErrorContext;
|
||||
|
||||
export { ErrorMessages as Errors } from "./error-message.js";
|
||||
|
||||
export default class ParserError extends CommentsParser {
|
||||
@@ -39,6 +41,41 @@ export default class ParserError extends CommentsParser {
|
||||
return this.raiseWithData(pos, undefined, errorTemplate, ...params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Raise a parsing error on given postion pos. If errorRecovery is true,
|
||||
* it will first search current errors and overwrite the error thrown on the exact
|
||||
* position before with the new error message. If errorRecovery is false, it
|
||||
* fallbacks to `raise`.
|
||||
*
|
||||
* @param {number} pos
|
||||
* @param {string} errorTemplate
|
||||
* @param {...any} params
|
||||
* @returns {(Error | empty)}
|
||||
* @memberof ParserError
|
||||
*/
|
||||
raiseOverwrite(
|
||||
pos: number,
|
||||
errorTemplate: string,
|
||||
...params: any
|
||||
): Error | empty {
|
||||
const loc = this.getLocationForPosition(pos);
|
||||
const message =
|
||||
errorTemplate.replace(/%(\d+)/g, (_, i: number) => params[i]) +
|
||||
` (${loc.line}:${loc.column})`;
|
||||
if (this.options.errorRecovery) {
|
||||
const errors = this.state.errors;
|
||||
for (let i = errors.length - 1; i >= 0; i--) {
|
||||
const error = errors[i];
|
||||
if (error.pos === pos) {
|
||||
return Object.assign(error, { message });
|
||||
} else if (error.pos < pos) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return this._raise({ loc, pos }, message);
|
||||
}
|
||||
|
||||
raiseWithData(
|
||||
pos: number,
|
||||
data?: {
|
||||
|
||||
@@ -534,19 +534,19 @@ export default class ExpressionParser extends LValParser {
|
||||
|
||||
const expr = this.parseUpdate(node, update, refExpressionErrors);
|
||||
|
||||
const startsExpr = this.hasPlugin("v8intrinsic")
|
||||
? this.state.type.startsExpr
|
||||
: this.state.type.startsExpr && !this.match(tt.modulo);
|
||||
if (isAwait && startsExpr && !this.isAmbiguousAwait()) {
|
||||
if (!this.state.invalidAwaitErrors.has(startPos)) {
|
||||
this.raise(
|
||||
if (isAwait) {
|
||||
const startsExpr = this.hasPlugin("v8intrinsic")
|
||||
? this.state.type.startsExpr
|
||||
: this.state.type.startsExpr && !this.match(tt.modulo);
|
||||
if (startsExpr && !this.isAmbiguousAwait()) {
|
||||
this.raiseOverwrite(
|
||||
startPos,
|
||||
this.hasPlugin("topLevelAwait")
|
||||
? Errors.AwaitNotInAsyncContext
|
||||
: Errors.AwaitNotInAsyncFunction,
|
||||
);
|
||||
return this.parseAwait(startPos, startLoc);
|
||||
}
|
||||
return this.parseAwait(startPos, startLoc);
|
||||
}
|
||||
|
||||
return expr;
|
||||
@@ -2348,17 +2348,7 @@ export default class ExpressionParser extends LValParser {
|
||||
: isStrictReservedWord;
|
||||
|
||||
if (reservedTest(word, this.inModule)) {
|
||||
if (!this.prodParam.hasAwait && word === "await") {
|
||||
this.raise(
|
||||
startLoc,
|
||||
this.hasPlugin("topLevelAwait")
|
||||
? Errors.AwaitNotInAsyncContext
|
||||
: Errors.AwaitNotInAsyncFunction,
|
||||
);
|
||||
this.state.invalidAwaitErrors.add(startLoc);
|
||||
} else {
|
||||
this.raise(startLoc, Errors.UnexpectedReservedWord, word);
|
||||
}
|
||||
this.raise(startLoc, Errors.UnexpectedReservedWord, word);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import { Position } from "../util/location";
|
||||
|
||||
import { types as ct, type TokContext } from "./context";
|
||||
import { types as tt, type TokenType } from "./types";
|
||||
import type { ParsingError } from "../parser/error";
|
||||
|
||||
type TopicContextState = {
|
||||
// When a topic binding has been currently established,
|
||||
@@ -37,7 +38,7 @@ export default class State {
|
||||
this.startLoc = this.endLoc = this.curPosition();
|
||||
}
|
||||
|
||||
errors: SyntaxError[] = [];
|
||||
errors: ParsingError[] = [];
|
||||
|
||||
// Used to signify the start of a potential arrow function
|
||||
potentialArrowAt: number = -1;
|
||||
@@ -155,9 +156,6 @@ export default class State {
|
||||
// Tokens length in token store
|
||||
tokensLength: number = 0;
|
||||
|
||||
// Positions of invalid await errors
|
||||
invalidAwaitErrors: Set<number> = new Set();
|
||||
|
||||
curPosition(): Position {
|
||||
return new Position(this.curLine, this.pos - this.lineStart);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user