Skip to content

Commit

Permalink
fix: add tokens when tokens: true is passed to parseExpression (#12939)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Mar 1, 2021
1 parent 9844eee commit efdca01
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
5 changes: 4 additions & 1 deletion packages/babel-parser/src/parser/expression.js
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down
10 changes: 7 additions & 3 deletions packages/babel-parser/src/types.js
Expand Up @@ -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:
Expand Down Expand Up @@ -129,16 +130,19 @@ export type BigIntLiteral = NodeBase & {
value: number,
};

export type ParserOutput = {
comments: $ReadOnlyArray<Comment>,
errors: Array<ParsingError>,
tokens?: $ReadOnlyArray<Token | Comment>,
};
// Programs

export type BlockStatementLike = Program | BlockStatement;

export type File = NodeBase & {
type: "File",
program: Program,
comments: $ReadOnlyArray<Comment>,
tokens: $ReadOnlyArray<Token | Comment>,
};
} & ParserOutput;

export type Program = NodeBase & {
type: "Program",
Expand Down

0 comments on commit efdca01

Please sign in to comment.