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

Arrow functions and parentheticals on their own line #272

Closed
edemaine opened this issue Jan 22, 2023 · 6 comments · Fixed by #277
Closed

Arrow functions and parentheticals on their own line #272

edemaine opened this issue Jan 22, 2023 · 6 comments · Fixed by #277

Comments

@edemaine
Copy link
Collaborator

The following input transpiles to itself:

x
(z) => z

But JavaScript has trouble with it, trying to parse it as a function call x(z) and then failing with SyntaxError: Malformed arrow function parameter list.

Perhaps even grosser is that the following gets treated like a function call, but I don't think it should.

x
(z)

Perhaps we can automatically add semicolons in these cases?

@STRd6
Copy link
Contributor

STRd6 commented Jan 22, 2023

A simple-ish fix would be to add a look-ahead in StatementDelimiter like:

&( Samedent "(" ) -> ";"

Before the &EOS rule.

@STRd6
Copy link
Contributor

STRd6 commented Jan 22, 2023

Probably create and use an InsertSemicolon rule to keep token and location info.

@edemaine
Copy link
Collaborator Author

edemaine commented Jan 22, 2023

This approach works pretty well, though there are some issues:

  • If we end up adding an implicit return to the following statement, we don't need to have added a semicolon. (No harm done though.)
  • I worry about code like this which would end up adding parens that weren't in the input:
    x
    {y: z} = obj
    This code has a different problem right now though. (Function call without indentation #273)

For the second issue in particular, I wonder about doing this in a second pass. Maybe a processBlocks? Let me know what you think.

@STRd6
Copy link
Contributor

STRd6 commented Jan 22, 2023

Good points. Maybe it would make more sense to do this in processBlocks. I've also been thinking about doing even more processing later in the AST. We may even be able to have it handle expressionizing everything as well.

@STRd6
Copy link
Contributor

STRd6 commented Jan 22, 2023

If we want a quick hacky solution for the meantime:

&( Samedent ( "(" / "{" / "[" ) ) -> ";"

@edemaine
Copy link
Collaborator Author

Fair enough. Tomorrow I can submit a PR for the hacky solution I put together.

edemaine added a commit that referenced this issue Jan 23, 2023
Fixes #272.
Adds some extra semicolons, as it is based on input instead of generated code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants