Skip to content

Trailing comma added after rest parameters  #3311

Closed

Description

@tmaciej

Prettier 1.8.2
Playground link

--parser typescript
--trailing-comma es5

Input:

const {
  longKeySoThisWillGoOnMultipleLines,
  longKeySoThisWillGoOnMultipleLines2,
  longKeySoThisWillGoOnMultipleLines3,
  ...rest
} = something;

Output:

const {
  longKeySoThisWillGoOnMultipleLines,
  longKeySoThisWillGoOnMultipleLines2,
  longKeySoThisWillGoOnMultipleLines3,
  ...rest,
} = something;

Expected behavior: Trailing comma shouldn't be added after rest parameters. This problem only happens for Typescript files (parser typescript), which breaks builds if someone for example is using Typescript together with Babel 7

Activity

  1. ikatyang commented on Nov 23, 2017

    @ikatyang
    Member

    TypeScript allowed trailing comma being there, so it shouldn't be a problem.

    What did you mean breaks builds? Are you using --parser typescript for js files?

  2. added
    status:awaiting response
    on Nov 23, 2017
  3. tmaciej commented on Nov 23, 2017

    @tmaciej
    Author

    @ikatyang No, I let prettier decide which parser to use but parsing .ts files with prettier results in trailing comma (as described above) which in turn when used with webpack -> ts-loader -> babel-loader (a pretty standard approach imo) results in errors thrown when using Babel 7 as its prohibits trailing commas after rest parameters. The only way around it is to either go with --trailingComma none (which breaks our linting) or pollute code with // prettier-ignore comments everywhere we use rest parameters.

    I think that prettier should remove / add those trailing commas consistently no matter which parser is used (the trailing comma is for example properly removed / not added when using --parser babylon in js files).

  4. duailibe commented on Nov 23, 2017

    @duailibe
    Collaborator

    This looks like a bug to me because it's inconsistent with arrays and function arguments (see playground).

    Nonetheless, I'm confused (I have never used TypeScript). Shouldn't ts-loader compile your .ts files and output JS to babel-loader? I don't understand why it would break since the syntax is valid TypeScript.

  5. tmaciej commented on Nov 23, 2017

    @tmaciej
    Author

    @duailibe

    Shouldn't ts-loader compile your .ts files and output JS to babel-loader

    But it does just that, exactly. So trailing commas are added by prettier which ts-loader ignores because it's valid typescript but then the output from ts-loader is passed to babel-loader which throws error because it's invalid syntax (Babel 7 only)

  6. ikatyang commented on Nov 23, 2017

    @ikatyang
    Member

    The breaks builds part looks like a TS emitter bug:

    (example.ts)

    const {
      longKeySoThisWillGoOnMultipleLines,
      longKeySoThisWillGoOnMultipleLines2,
      longKeySoThisWillGoOnMultipleLines3,
      ...rest,
    } = something;
    tsc example.ts --target esnext

    (example.js)

    const { longKeySoThisWillGoOnMultipleLines, longKeySoThisWillGoOnMultipleLines2, longKeySoThisWillGoOnMultipleLines3, ...rest, } = something;

    since the current stage 3 proposal does not allow trailing comma being there, TS emitter should not emit the trailing comma.

  7. duailibe commented on Nov 23, 2017

    @duailibe
    Collaborator

    @tmaciej Yeah I meant exactly what @ikatyang said, ts-loader should output valid JS, meaning it should remove the comma, not ignore it.

  8. tmaciej commented on Nov 23, 2017

    @tmaciej
    Author

    @duailibe This still in my opinion doesn't justify inconsistent behavior of prettier because code formatting happens before transpilation and i don't think those trailing commas should be removed when using one parser and left using another.

  9. duailibe commented on Nov 23, 2017

    @duailibe
    Collaborator

    @tmaciej I wasn't arguing that, I was just trying to figure out if this should be high priority or not (it should be if prettier was printing invalid syntax).

  10. added
    lang:typescript
    status:has pr
    and removed
    status:awaiting response
    on Nov 23, 2017
  11. tmaciej commented on Nov 23, 2017

    @tmaciej
    Author

    @duailibe

    I wasn't arguing that, I was just trying to figure out if this should be high priority or not

    Sorry, i misunderstood then.

    Anyways, thanks for lighting fast response and PR @duailibe @ikatyang 👍

  12. duailibe commented on Nov 23, 2017

    @duailibe
    Collaborator

    @tmaciej Thanks for the report!

  13. added this to the 1.9 milestone on Nov 28, 2017
  14. added
    locked-due-to-inactivity
    on Jul 6, 2018
  15. locked as resolved and limited conversation to collaborators on Jul 6, 2018
  16. added theissue type on Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    lang:typescriptlocked-due-to-inactivitystatus:has pr

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions