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

ternary requires even more space now #1507

Open
JayXon opened this issue Oct 24, 2024 · 8 comments
Open

ternary requires even more space now #1507

JayXon opened this issue Oct 24, 2024 · 8 comments
Labels
javascript JavaScript compatibility related

Comments

@JayXon
Copy link

JayXon commented Oct 24, 2024

a ?b :c used to work, with #1498, it no longer parse, have to do a ?b : c now

@bbrk24
Copy link
Contributor

bbrk24 commented Oct 24, 2024

Is this necessarily a bug? I guess it is technically a regression but I don't know that it was ever officially supported.

@JayXon
Copy link
Author

JayXon commented Oct 24, 2024

it's documented here

* Ternary operator `x ?y :z` needs space before both `?` and `:`

@STRd6
Copy link
Contributor

STRd6 commented Oct 24, 2024

We could add a compiler flag to disable symbol shorthand.

@JayXon
Copy link
Author

JayXon commented Oct 24, 2024

The example I gave is not ambiguous because it currently doesn't parse, and there's really only one way to parse it.

But if we support parsing that, then we need to decide whether a ?b :c :d means a ? b(Symbol.for("c")) : d; or a ? b : c(Symbol.for("d"));

@edemaine
Copy link
Collaborator

edemaine commented Oct 25, 2024

Thanks for finding this!

Given the greedy nature of the parser, it's pretty annoying to fix this, unless we

  • add another flag, something like "don't process symbols because I'm waiting for a colon"
  • forbid symbols to start implicit function calls (maybe it's rare we want to pass symbols into functions? probably not)
  • forbid implicit function calls in ternary (seems u likely)
  • define this to be the new behavior and a breaking change, in which case we'd want a compiler flag to disable symbols

Probably worth seeing how much JS code has a space before but not after a colon in a ternary.

@STRd6
Copy link
Contributor

STRd6 commented Oct 25, 2024

Another thing we could try is maybe add snug ternaries as well like a?b:c

But it may require another parser flag to correctly handle complex cases even still.

@JayXon
Copy link
Author

JayXon commented Oct 25, 2024

by parser flag, do you mean like a config or parser internal state?

@edemaine
Copy link
Collaborator

edemaine commented Oct 25, 2024

  • add another flag, something like "don't process symbols because I'm waiting for a colon"

In this case, I meant "parser internal state". Sorry for the ambiguity.

It sounds like there might be interest in pursuing this. Snug ternaries would be nice to allow...

@edemaine edemaine added the javascript JavaScript compatibility related label Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
javascript JavaScript compatibility related
Projects
None yet
Development

No branches or pull requests

4 participants