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

Comment at end of function in JSX prop causes parse error #1695

Open
nicholaides opened this issue Feb 5, 2025 · 1 comment
Open

Comment at end of function in JSX prop causes parse error #1695

nicholaides opened this issue Feb 5, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@nicholaides
Copy link

❌ The following will cause parse errors:

<button
  onClick={=> 
    alert('hi') 
    // ❌ this comment causes parse error
  }
>
<button
  onClick={=> 
    alert('hi') //  ❌ this comment causes parse error
  }
>
<button
  onClick={=> 
    alert('hi') 
    /* ❌ this comment causes parse error */
  }
>
<button
  onClick={=> 
    alert('hi') /* ❌ this comment causes parse error */
  }
>

✅ The following will not cause parse errors:

<button
  onClick={=> 
    alert('hi') 
    // ✅ this works
    alert('hi again')
  }
>
<button
  onClick={=> 
    alert('hi') // ✅ this works
    alert('hi again')
  }
>
<button
  onClick={=> 
    alert('hi') 
    /* ✅ this works */
    alert('hi again')
  }
>
<button
  onClick={=> 
    alert('hi') /* ✅ this works */
    alert('hi again')
  }
>

Workarounds:

  1. Don't put a comment at the end of a the function

  2. Put semicolon at the end (if you don't need to return anything)

<button
  onClick={=> 
    alert('hi') /* ✅ this works */
    ;
  }
>
  1. Add |> return to the end of your function
<button
  onClick={=> 
    getUser() /* ✅ this works */
    |> return
  }
>
@edemaine edemaine added the bug Something isn't working label Feb 5, 2025
@edemaine
Copy link
Collaborator

edemaine commented Feb 5, 2025

Interesting! Thanks for the report. This example gives a little insight into what might be going wrong here:

=>
  alert('hi')
  // foo

becomes

() => {
  return alert('hi')
}
  // foo

In addition to the JSX application, it might be nice for the comment to be brought into the block (at least when it's properly indented like this).

Yet another example that fails:

<button
  onClick=
    => 
      alert('hi') 
      // ❌ this comment causes parse error
>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants