Skip to content

fix: preserve tokensLength in tryParse#13326

Merged
JLHwung merged 2 commits intobabel:mainfrom
JLHwung:preserve-tokens-length-on-error-recovery
May 17, 2021
Merged

fix: preserve tokensLength in tryParse#13326
JLHwung merged 2 commits intobabel:mainfrom
JLHwung:preserve-tokens-length-on-error-recovery

Conversation

@JLHwung
Copy link
Copy Markdown
Contributor

@JLHwung JLHwung commented May 17, 2021

Q                       A
Fixed Issues? Fixes #13322
Patch: Bug Fix? Y
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

In this PR we preserve state.tokensLength (derived from this.tokens) on raising errors when errorRecovery: true, because the parser will parse remaining tokens. We do not reset the parser state when tryParse successfully returns, so when errorRecovery is true, we should not reset state.tokensLength, too.

Technically #13256 does not introduce this regression but it reveals the fact that the parser unexpectedly eats tokens when errors are thrown in recovery mode.

@JLHwung JLHwung added PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: parser labels May 17, 2021
@@ -0,0 +1 @@
<div propA={[key: value]} />
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Currently the tokens of parsed AST is a length-14 array

[ tt.jsxTagStart, tt.name, undefined, undefined, ... , tt.eof]

babel7Compat thus throws because the tokens unexpectedly includes undefined.

@babel-bot
Copy link
Copy Markdown
Collaborator

babel-bot commented May 17, 2021

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/46182/

@codesandbox-ci
Copy link
Copy Markdown

codesandbox-ci Bot commented May 17, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 6f84912:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

Comment thread packages/babel-parser/src/parser/util.js Outdated
Comment thread packages/babel-parser/src/parser/util.js Outdated
Co-authored-by: Brian Ng <bng412@gmail.com>
@JLHwung JLHwung merged commit 0d0950f into babel:main May 17, 2021
@JLHwung JLHwung deleted the preserve-tokens-length-on-error-recovery branch May 17, 2021 19:32
@github-actions github-actions Bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Aug 17, 2021
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Aug 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser PR: Bug Fix 🐛 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Unexpected error thrown

4 participants