diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index f3a434127aee..e976098f9298 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -148,7 +148,7 @@ export default class ExpressionParser extends LValParser { } // Convenience method to parse an Expression only - getExpression(): N.Expression { + getExpression(): N.Expression & N.ParserOutput { let paramFlags = PARAM; if (this.hasPlugin("topLevelAwait") && this.inModule) { paramFlags |= PARAM_AWAIT; @@ -162,6 +162,9 @@ export default class ExpressionParser extends LValParser { } expr.comments = this.state.comments; expr.errors = this.state.errors; + if (this.options.tokens) { + expr.tokens = this.tokens; + } return expr; } diff --git a/packages/babel-parser/src/types.js b/packages/babel-parser/src/types.js index 97d8445c0a4e..43addde542ff 100644 --- a/packages/babel-parser/src/types.js +++ b/packages/babel-parser/src/types.js @@ -4,6 +4,7 @@ import type { SourceType } from "./options"; import type { Token } from "./tokenizer"; import type { SourceLocation } from "./util/location"; import type { PlaceholderTypes } from "./plugins/placeholders"; +import type { ParsingError } from "./parser/error"; /* * If making any changes to the AST, update: @@ -129,6 +130,11 @@ export type BigIntLiteral = NodeBase & { value: number, }; +export type ParserOutput = { + comments: $ReadOnlyArray, + errors: Array, + tokens?: $ReadOnlyArray, +}; // Programs export type BlockStatementLike = Program | BlockStatement; @@ -136,9 +142,7 @@ export type BlockStatementLike = Program | BlockStatement; export type File = NodeBase & { type: "File", program: Program, - comments: $ReadOnlyArray, - tokens: $ReadOnlyArray, -}; +} & ParserOutput; export type Program = NodeBase & { type: "Program",