Skip to content

odd output for template literals #786

Closed
renovatebot/renovate
#198

Description

@chrisdrackett

given the following code:

insertRule(`*, *:before, *:after {
  box-sizing: inherit;
}`)

I get the following output:

insertRule(
  `*, *:before, *:after {
  box-sizing: inherit;
}`,
)

The formatting of the last two lines seems odd to me. I'm not sure why a comma is added or why the line with the closing bracket is on the same indentation as the closing parenthesis.

Activity

  1. added
    status:needs discussion
    on Feb 23, 2017
  2. vjeux commented on Feb 23, 2017

    @vjeux
    Contributor

    Template literals are tricky because the indentation is part of the string itself. If we change the indentation, this means that the string itself will change and this may (or may not, like in this case) change the program behavior, which we don't want to.

    In this case, in the original version, } was indented by 0 spaces so we respect that. As for the comma, this is because you've got the trailing comma option enabled.

  3. jlongster commented on Feb 23, 2017

    @jlongster
    Member

    That code should not be breaking in the first place though... it doesn't reach 80 chars so not sure why it breaks the function call. In cases where it does break though, yes, this is expected behavior and you'll have to tweak the literal to make it align correctly.

  4. vjeux commented on Mar 1, 2017

    @vjeux
    Contributor
    ./bin/prettier.js test.js --debug-print-doc
    [
      "insertRule",
      wrappedGroup([
        "(",
        indent(2, [
          softline,
          "`",
          "*, *:before, *:after {",
          hardline,
          breakParent,
          "  box-sizing: inherit;",
          hardline,
          breakParent,
          "}",
          "`"
        ]),
        ifBreak(""),
        softline,
        ")"
      ]),
      ";",
      hardline,
      breakParent
    ];

    Looks like template literal \n force the parent to break.

  5. vjeux commented on Mar 1, 2017

    @vjeux
    Contributor

    But even if I remove breakParent from templateline, it still triggers the parent to break.

  6. reopened this on Mar 1, 2017
  7. added and removed
    status:needs discussion
    on Mar 1, 2017
  8. vjeux commented on Mar 3, 2017

    @vjeux
    Contributor

    I wonder if many people write it as

    insertRule(
      `this is a long string
       that is very long`
    );

    if not, then we could special case single-argument calls that are templates to not indent.

  9. added a commit that references this issue on Mar 3, 2017
    acb2363
  10. vjeux commented on Mar 3, 2017

    @vjeux
    Contributor

    I just sent #873 which fixes the original issue but I'm not sure this is the right thing to do.

  11. techniq commented on Mar 4, 2017

    @techniq

    I thought the formatting for an optional styled-component rule was a little odd (although not terrible), but thought I'd throw another template literal example here to consider. This one has nested template literals.

    const Wrapper = styled.div`
      padding-top: 64px;
      padding-left: 8px;
      padding-right: 8px;
      transition: margin .3s;
    
      ${({sidebar}) => sidebar.docked && sidebar.open && css`
        margin-left: ${sidebar.width}px
      `}
    `;

    formats to

    const Wrapper = styled.div`
      padding-top: 64px;
      padding-left: 8px;
      padding-right: 8px;
      transition: margin .3s;
    
      ${({ sidebar }) => sidebar.docked &&
      sidebar.open &&
      css`
        margin-left: ${sidebar.width}px
      `}
    `;
  12. knowbody commented on Mar 9, 2017

    @knowbody
    Contributor

    I've also seen that styled-components get weird formatting, probably all related to this issue:

    const Container = styled.div`
      margin: ${props => props.margin || 0};
      background: ${props => props.color || 'transparent'};
      margin-bottom: ${props => {
        if (props.hasStickyFooter) {
          return '5rem';
        } else if (!props.margin) {
          return '0rem';
        }
      }};
      cursor: ${props => props.pointer ? 'pointer' : 'auto'};
    `;

    formatted to:

    const Container = styled.div`
      margin: ${props => props.margin || 0};
      background: ${props => props.color || 'transparent'};
      margin-bottom: ${props => {
      if (props.hasStickyFooter) {
        return '5rem';
      } else if (!props.margin) {
        return '0rem';
      }
    }};
      cursor: ${props => props.pointer ? 'pointer' : 'auto'};
    `;
  13. added
    status:needs discussion
    and removed on Mar 19, 2017
  14. gustavnikolaj commented on Mar 20, 2017

    @gustavnikolaj

    I have a similar case and , which is triggered by tabWidth. Notice how the second argument to the this.query call in loadEntity is indented incorrectly in the tabWidth 4 case.

    1. tabWidth 2
    2. tabWidth 4
  15. added a commit that references this issue on Apr 22, 2017
    3017cae
  16. added a commit that references this issue on May 1, 2017
    c521406
  17. axhamre commented on Apr 8, 2018

    @axhamre

    As this was closed - was there eventually any solution to this or are we out of luck if we for example use styled-components?

  18. j-f1 commented on Apr 8, 2018

    @j-f1
    Member

    @hundsim This issue should be fixed. If you’re having formatting issues, please open a new issue and fill out the template so we can more easily track them.

  19. added
    locked-due-to-inactivity
    on Jul 7, 2018
  20. locked as resolved and limited conversation to collaborators on Jul 7, 2018
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

    locked-due-to-inactivitystatus:needs discussion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions