Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add tokens when tokens: true is passed to parseExpression #12939

Merged
merged 1 commit into from Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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>,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When tokens: false is passed, tokens does not exist in the output.

};
// 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