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

SWC produces invalid code if types have a trailing coma #6589

Closed
skutyepov opened this issue Dec 6, 2022 · 4 comments · Fixed by #6610
Closed

SWC produces invalid code if types have a trailing coma #6589

skutyepov opened this issue Dec 6, 2022 · 4 comments · Fixed by #6610
Labels
Milestone

Comments

@skutyepov
Copy link

skutyepov commented Dec 6, 2022

Describe the bug

If types contain a trailing coma, swc adds to the actual code, which is a syntax error.

For reference, TSC handles it correctly. TSC playground ref.

Input code

export function func({
  a,
  b,
  ...rest
}: {
  a: string,
  b: string,
}) {
 console.log(a,b, rest)
}

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": false
    },
    "target": "es2022"
  }
}

Playground link

https://play.swc.rs/?version=1.3.21&code=H4sIAAAAAAAAA0utKMgvKlFIK81LLsnMzwMzNKq5FBQSdYBEEojQ09MrSi0u4aq1UgBLWCkUlxRl5qWDFSA4tZog6eT8vOL8nFS9nPx0jUSdJB0FkFZNrloAL7Tel2gAAAA%3D&config=H4sIAAAAAAAAAxWKQQqAMAwE%2F7JnD5JjfxNKFEWkZHNoKfm79TYzzMTNijLR1Gn%2BE8cb2lEQoxmrXy2wIbjSoQ8tl6ifFmsxyi6CzA%2Br99KJSAAAAA%3D%3D

Expected behavior

SWC should not add trailing coma after ...rest

Actual behavior

SWC adds trailing coma: { a, b, ...rest,}, which isn't valid syntax

Version

1.3.21

Additional context

The issue goes away if trailing coma from types is removed.

export function func({
  a,
  b,
  ...rest
}: {
  a: string,
  b: string // (no coma here)
}) {
 console.log(a,b, rest)
}
@skutyepov skutyepov added the C-bug label Dec 6, 2022
@skutyepov skutyepov changed the title SWC produces invalid code if "...rest" isn't typed SWC produces invalid code if types have a trailing coma Dec 6, 2022
@alexander-akait
Copy link
Collaborator

ts playground does the same

@kdy1 kdy1 added this to the Planned milestone Dec 7, 2022
@skutyepov
Copy link
Author

skutyepov commented Dec 7, 2022

@alexander-akait in your example TSC produces the following error:

A rest parameter or binding pattern may not have a trailing comma.

Also, input in your example is invalid, the issue with SWC is a bit different. Given valid input, SWC produces invalid output.
Below is a screenshot that shows a difference between SWC (top) and TSC (bottom) outputs.

swc-vs-tsc

@alexander-akait
Copy link
Collaborator

Yeah, I see, let's fix it 👍

@swc-bot
Copy link
Collaborator

swc-bot commented Jan 13, 2023

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Jan 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

4 participants