Skip to content

Inconsistent parsing of injected expressions in styled-components' css #8095

Closed

Description

@helios1138

note the presence/absence of ; after injected expression

const Header = styled.div`
  ${something()};
margin:4px;
`

is parsed and formatted as

const Header = styled.div`
  ${something()};
  margin: 4px;
`;

const Header = styled.div`
  ${something()}
margin:4px;
`

is parsed and formatted as

const Header = styled.div`
  ${something()}
  margin:4px;
`;

const Header = styled.div`
  ${something()};
  & > ${Child}:not(:first-child) {
margin-left:5px;
}
`

is parsed and formatted as

const Header = styled.div`
  ${something()};
  & > ${Child}:not(:first-child) {
    margin-left: 5px;
  }
`;

const Header = styled.div`
  ${something()}
  & > ${Child}:not(:first-child) {
margin-left:5px;
}
`

is not parsed

Error: Couldn't insert all the expressions
    at transformCssDoc (https://prettier.io/lib/standalone.js:22171:11)
    at Object.embed$4 [as embed] (https://prettier.io/lib/standalone.js:22011:18)
    at Object.printSubtree (https://prettier.io/lib/standalone.js:15781:31)
    at callPluginPrintFunction (https://prettier.io/lib/standalone.js:15898:29)
    at https://prettier.io/lib/standalone.js:15863:16
    at Object.printComments (https://prettier.io/lib/standalone.js:15586:17)
    at printGenerically (https://prettier.io/lib/standalone.js:15862:22)
    at FastPath.call (https://prettier.io/lib/standalone.js:15700:16)
    at printPathNoParens (https://prettier.io/lib/standalone.js:25178:91)
    at Object.genericPrint$3 [as print] (https://prettier.io/lib/standalone.js:23541:28)

Activity

  1. thorn0 commented on Apr 20, 2020

    @thorn0
    Member

    Note that this error is shown only on the playground for debugging purposes. In production, Prettier just skips formatting this template literal. And I'd say this is the right thing to do because how can we know what something() can be? It can be a part of the selector or a separate rule set, so it's not clear how to format it. Leaving it as is the safest option.

  2. added
    area:multiparser
    lang:css/scss/less
    lang:javascript
    type:question
    status:needs discussion
    and removed
    type:question
    on Apr 20, 2020
  3. thecodrr commented on Apr 20, 2020

    @thecodrr
    Contributor

    The error occurs when you have multiple string concatenations. If you remove ${Child} or ${something()}, there's no error. As such, this works without an issue:

    const Header = styled.div`
      ${something()}
      & > $Child:not(:first-child) {
    margin-left:5px;
    }
    `
  4. added and removed
    status:needs discussion
    on Apr 20, 2020
  5. thecodrr commented on Apr 20, 2020

    @thecodrr
    Contributor

    @thorn0 Can I claim this?

  6. thorn0 commented on Apr 20, 2020

    @thorn0
    Member

    @thecodrr I'm already looking into it.

  7. thecodrr commented on Apr 20, 2020

    @thecodrr
    Contributor

    Oops. Okay good luck.

  8. added
    locked-due-to-inactivity
    on Aug 6, 2020
  9. locked as resolved and limited conversation to collaborators on Aug 6, 2020
  10. 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

    area:multiparserlang:css/scss/lesslang:javascriptlocked-due-to-inactivity

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions