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

Trailing comma incorrectly kept when stripping types for ...rest param #1573

Closed
danopia opened this issue Apr 13, 2021 · 4 comments · Fixed by #1572
Closed

Trailing comma incorrectly kept when stripping types for ...rest param #1573

danopia opened this issue Apr 13, 2021 · 4 comments · Fixed by #1572
Assignees
Labels
Milestone

Comments

@danopia
Copy link

danopia commented Apr 13, 2021

Describe the bug

When accepting an options argument in a function, often times I want certain options to be kept separate. I achieve this by setting up an object splat, or whatever they're called, and putting a rest param afterwards to catch the rest.

The issue that I see is that swc is lifting out the trailing comma, if any, from within the TypeScript type clause for the overall parameter, and dropping it into the splat which creates a syntax error.

Input code

function myFunc({...data}: {
  field: string,
}) {
}

Output code

// Uncaught SyntaxError: Rest element must be last element
function myFunc({ ...data,  }) {
}

Note that removing the type's inner trailing comma removes it from the output as well, producing valid code:

Input code

function myFunc({...data}: {
  field: string
}) {
}

Output code

// All good!
function myFunc({ ...data  }) {
}

Config

I'm nominally using Deno, so to recreate this bug I wrote a very basic .swcrc:

{
  "jsc": {
    "parser": {
      "syntax": "typescript"
    },
    "target": "es2020"
  }
}

Expected behavior
The inner comma probably shouldn't get lifted out of the type signatures in general. I don't see when that would be useful

Version

repro@1.0.0
├── @swc/cli@0.1.36
└── @swc/core@1.2.52

Additional context
It seems that using semicolons instead of commas inside the type is a valid workaround.

@Liamolucko
Copy link
Contributor

It seems like this is the cause:

// Write a trailing comma, if requested.
let has_trailing_comma = format.contains(ListFormat::AllowTrailingComma) && {
if parent_node.is_dummy() {
false
} else {
match self.cm.span_to_snippet(parent_node) {
Ok(snippet) => {
if snippet.len() < 3 {
false
} else {
snippet[..snippet.len() - 1].trim().ends_with(',')
}
}
_ => false,
}
}
};

It tries to emit a trailing comma if the input has a trailing comma, but this picks up the trailing comma of the type annotation. Should this functionality just be removed?

@kdy1 kdy1 added this to the v1.2.53 milestone Apr 14, 2021
@danopia
Copy link
Author

danopia commented Apr 14, 2021

It looks like a fix for this e3b64a0 is rolled into this PR #1572

@kdy1
Copy link
Member

kdy1 commented Apr 14, 2021

@danopia You are correct, thanks! I'll update the pr.

@kdy1 kdy1 self-assigned this Apr 14, 2021
@kdy1 kdy1 mentioned this issue Apr 14, 2021
12 tasks
kdy1 added a commit that referenced this issue Apr 14, 2021
swc_bundler:
 - Ensure that denoland/deno#10141 is fixed. 
 - Run deno tests on ci.
 - Support nested `export *`. (denoland/deno#10153, denoland/deno#10174)

swc_ecma_codegen:
 - Remove `,` after rest elements. (#1573, denoland/deno#10167)

swc_ecma_transforms_optimization:
 - Don't drop items used by the discriminant of a switch.

swc_ecma_transforms_typescript:
 - Remove constructors without a body.
@swc-bot
Copy link
Collaborator

swc-bot commented Oct 24, 2022

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 Oct 24, 2022
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