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(babel-parser): avoid state.clone() to clone the whole token store #11029

Merged
merged 5 commits into from Jan 20, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion packages/babel-parser/src/parser/statement.js
Expand Up @@ -67,7 +67,7 @@ export default class StatementParser extends ExpressionParser {
file.program = this.finishNode(program, "Program");
file.comments = this.state.comments;

if (this.options.tokens) file.tokens = this.state.tokens;
if (this.options.tokens) file.tokens = this.tokens;

return this.finishNode(file, "File");
}
Expand Down
20 changes: 18 additions & 2 deletions packages/babel-parser/src/tokenizer/index.js
@@ -1,6 +1,7 @@
// @flow

import type { Options } from "../options";
import * as N from "../types";
import type { Position } from "../util/location";
import * as charCodes from "charcodes";
import { isIdentifierStart, isIdentifierChar } from "../util/identifier";
Expand Down Expand Up @@ -114,6 +115,9 @@ export default class Tokenizer extends LocationParser {

isLookahead: boolean;

// Token store.
tokens: Array<Token | N.Comment> = [];

constructor(options: Options, input: string) {
super();
this.state = new State();
Expand All @@ -123,13 +127,25 @@ export default class Tokenizer extends LocationParser {
this.isLookahead = false;
}

pushToken(token: Token | N.Comment) {
// Pop out invalid tokens trapped by try-catch parsing.
// Those parsing branches are mainly created by typescript and flow plugins.
while (
this.tokens.length > 0 &&
this.tokens[this.tokens.length - 1].end > token.start
) {
this.tokens.pop();
}
3cp marked this conversation as resolved.
Show resolved Hide resolved
this.tokens.push(token);
3cp marked this conversation as resolved.
Show resolved Hide resolved
}

// Move to the next token

next(): void {
if (!this.isLookahead) {
this.checkKeywordEscapes();
if (this.options.tokens) {
this.state.tokens.push(new Token(this.state));
this.pushToken(new Token(this.state));
}
}

Expand Down Expand Up @@ -242,7 +258,7 @@ export default class Tokenizer extends LocationParser {
loc: new SourceLocation(startLoc, endLoc),
};

if (this.options.tokens) this.state.tokens.push(comment);
if (this.options.tokens) this.pushToken(comment);
this.state.comments.push(comment);
this.addComment(comment);
}
Expand Down
4 changes: 0 additions & 4 deletions packages/babel-parser/src/tokenizer/state.js
Expand Up @@ -5,7 +5,6 @@ import * as N from "../types";
import { Position } from "../util/location";

import { types as ct, type TokContext } from "./context";
import type { Token } from "./index";
import { types as tt, type TokenType } from "./types";

type TopicContextState = {
Expand Down Expand Up @@ -93,9 +92,6 @@ export default class State {
yieldPos: number = -1;
awaitPos: number = -1;

// Token store.
tokens: Array<Token | N.Comment> = [];

// Comment store.
comments: Array<N.Comment> = [];

Expand Down
@@ -0,0 +1,5 @@
{
"sourceType": "module",
"plugins": ["typescript"],
"tokens": true
}