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

[4.7-beta] Parsing failure for arrow function expr in conditional expr #48733

Closed
sosukesuzuki opened this issue Apr 17, 2022 · 9 comments Β· Fixed by #48940
Closed

[4.7-beta] Parsing failure for arrow function expr in conditional expr #48733

sosukesuzuki opened this issue Apr 17, 2022 · 9 comments Β· Fixed by #48940
Assignees
Labels
Fix Available A PR has been opened for this issue Needs Investigation This issue needs a team member to investigate its status.

Comments

@sosukesuzuki
Copy link

sosukesuzuki commented Apr 17, 2022

Bug Report

πŸ”Ž Search Terms

  • conditional
  • 4.7 beta
  • syntax error

πŸ•— Version & Regression Information

  • This is a crash
  • This changed between versions 4.6.3 and 4.7.0-beta

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

(false ? (param): string => param :  null);

πŸ™ Actual behavior

Parsing failure for arrow function expr that has type annotations for return type, but doesn't have type annotations for parameters, in conditional expression.

πŸ™‚ Expected behavior

Parsing successfull.

@MartinJohns
Copy link
Contributor

This is a crash

I can't reproduce this. Neither locally nor in the playground it crashes.

@sosukesuzuki
Copy link
Author

I can't reproduce this. Neither locally nor in the playground it crashes.

Sorry I forgot remove this from template.

@jmcscript
Copy link

jmcscript commented Apr 18, 2022

The OP's statement is accurate as the playground proves that there are no parsing errors in the version he mentions. However, if it helps resolve the problem, changing his code as follows resolves the error in the current version, leaving you with just the any type inference for param.

TS Playground Example

(false ? ((param): string => param) :  null);

@RyanCavanaugh RyanCavanaugh added Working as Intended The behavior described is the intended behavior; this is not a bug and removed Working as Intended The behavior described is the intended behavior; this is not a bug labels Apr 18, 2022
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.8.0 milestone Apr 18, 2022
@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Apr 18, 2022
@RyanCavanaugh
Copy link
Member

I'd like @jakebailey to confirm but this seems like it might be intentional. Your code might terminate at the legal JS expression

false ? (param) : string

so parsing the : string as a type annotation might require "too much" lookahead.

@jakebailey
Copy link
Member

jakebailey commented Apr 19, 2022

No doubt this is #16241 / #47550, yes. The reason why the workaround in typescript-eslint/typescript-eslint#4829 works is because the parser can see the : inside of the parens and says "that's definitely a parameter", and allows the colon before the return type.

When the (what I can only describe as) heuristics don't "prove" that it's an arrow function, my change disallows the : when parsing, preferring it to end the conditional.

The test case in the issue is similar to one I wrote in my PR, but then throws an extra case on the end.

I'd have to think about how I might fix this; the "heuristic" I described previously mainly checks things to do with parameters. It doesn't go and parse all the way past an error expression.

The gotcha with lookahead is when conditionals are nested, though I can't seem to produce an example that actually parses in JS. I thought the following would be a counter example, but this fails to parse when I run it in node.

true ? false ? (a) : b => c : null : null

(If anyone can come up with an example, let me know. This is tricky.)

@jakebailey
Copy link
Member

Regardless, I think all of this is going to have to be figured out for the types-in-JS proposal, since all of these parsing choices will have to be made and our parser changed to fix any differences.

@sosukesuzuki
Copy link
Author

@jakebailey
Copy link
Member

@sosukesuzuki Can you try taking a peek using the build on my PR here? #48788 (comment)

@jakebailey
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fix Available A PR has been opened for this issue Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
7 participants