Skip to content

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

Description

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.

Activity

  1. MartinJohns commented on Apr 17, 2022

    @MartinJohns
    Contributor

    This is a crash

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

  2. sosukesuzuki commented on Apr 17, 2022

    @sosukesuzuki
    Author

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

    Sorry I forgot remove this from template.

  3. jmcscript commented on Apr 18, 2022

    @jmcscript

    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);
  4. added
    Working as IntendedThe behavior described is the intended behavior; this is not a bug
    and removed
    Working as IntendedThe behavior described is the intended behavior; this is not a bug
    on Apr 18, 2022
  5. RyanCavanaugh commented on Apr 18, 2022

    @RyanCavanaugh
    Member

    I'd like Jake Bailey (@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.

  6. jakebailey commented on Apr 19, 2022

    @jakebailey
    Member

    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.)

  7. jakebailey commented on Apr 19, 2022

    @jakebailey
    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.

  8. sosukesuzuki commented on Apr 19, 2022

    @sosukesuzuki
    Author
  9. jakebailey commented on Apr 20, 2022

    @jakebailey
    Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Fix AvailableA PR has been opened for this issueNeeds InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions