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(js_parser): handle edge cases for arrow expression in tenary #1113

Merged
merged 6 commits into from
Dec 8, 2023

Conversation

kalleep
Copy link
Contributor

@kalleep kalleep commented Dec 7, 2023

Summary

When looking at what caused the issues describe in #1077 I first noticed that the consequent block was parsed as a arrow expression because when we try to parse an arrow function we also check for typescript return parameter.

E.g for a ? (b) : <p>{() => null}</p>; everything after : and up until => was parsed as the return parameter resulting in this being true.

My first try to fix this was to return an error when ambiguity.is_disallowed() was true and if the parsed typescript return parameter was bogus.

While this worked for this perticular case I started to play around with different tenary expressions that could generate the same result but with valid typescript syntax and found:

a ? (b) : a => {};

This expression failed both when parsing typescript and javascript but with different errors and checking for bogus typescript return type was not fixing this case.

So instead I created a specialized version of parse_assignment_expression_or_higher to use when parsing the consequent block of a tenary expression.

Playground link:
https://biomejs.dev/playground/?indentStyle=space&code=YQAgAD8AIAAoAGIAKQAgADoAIABhACAAPQA%2BACAAewB9ADsACgBhACAAPwAgACgAYgApACAAOgAgADwAcAA%2BAHsAKAApACAAPQA%2BACAAbgB1AGwAbAB9ADwALwBwAD4AOwA%3D

Closes: #1077

Test Plan

Added new test

Copy link

netlify bot commented Dec 7, 2023

Deploy Preview for biomejs canceled.

Name Link
🔨 Latest commit a36d1b5
🔍 Latest deploy log https://app.netlify.com/sites/biomejs/deploys/657242de7531130009749565

@github-actions github-actions bot added A-Parser Area: parser A-Website Area: website L-JavaScript Language: JavaScript and super languages A-Changelog Area: changelog labels Dec 7, 2023
@Conaclos Conaclos changed the title fix(js_parser): handle edge cases for arrow expression in tenary expression fix(js_parser): handle edge cases for arrow expression in tenary Dec 7, 2023
Copy link
Member

@Conaclos Conaclos left a comment

Choose a reason for hiding this comment

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

This looks good to me! Thanks for your contribution!

I just suggested renaming consequence and alternative to consequent and alternate. Are there some specific reasons you chose consequence and alternative?

crates/biome_js_parser/src/syntax/expr.rs Outdated Show resolved Hide resolved
crates/biome_js_parser/src/syntax/expr.rs Outdated Show resolved Hide resolved
@kalleep
Copy link
Contributor Author

kalleep commented Dec 7, 2023

No just mixing up some words, will regenerate test files and change naming of the other tests as well 👍

@ematipico ematipico requested review from denbezrukov and removed request for denbezrukov December 8, 2023 17:00
@ematipico ematipico merged commit f8ce594 into biomejs:main Dec 8, 2023
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Changelog Area: changelog A-Parser Area: parser A-Website Area: website L-JavaScript Language: JavaScript and super languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 Ternaries with parenthesized identifiers as consequents parse as Arrow Function Expressions
3 participants