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

[styled-components] Using expression inside template literal along with attribute selector results in invalid CSS syntax. #4520

Closed
skawinski opened this issue May 21, 2018 · 11 comments
Labels
area:multiparser Issues with printing one language inside another, like CSS-in-JS lang:css/scss/less Issues affecting CSS, Less or SCSS locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. priority:high Code is printed in a way that alters the AST, breaks syntax, or is a significant regression. Urgent! type:bug Issues identifying ugly output, or a defect in the program

Comments

@skawinski
Copy link

Styling component based on the attribute state of its neighbour element results in invalid CSS markup. Space is being added between expression and attribute selector. It happens only when styled-components are being used with their default "styled" alias. Using any other name seem to cause prettier ignore insides of template literals.

It' quite a border use case, but unfortunately it results in broken CSS syntax not recognised by browser engine.

Prettier 1.12.1
Playground link

Input:

const x = styled.div``;

const a = anything.anything`
  ${x}[value=true] + & {
  }
`;

const b = styled.anything`
  ${x}[value=true] + & {
  }
`;

Output:

const x = styled.div``;

const a = anything.anything`
  ${x}[value=true] + & {
  }
`;

const b = styled.anything`
  ${x} [value=true] + & {
  }
`;

Expected behavior:

const x = styled.div``;

const a = anything.anything`
  ${x}[value=true] + & {
  }
`;

const b = styled.anything`
  ${x}[value=true] + & {
  }
`;
@skawinski skawinski changed the title [styled-components] Using expression along with attribute selector results in invalid CSS syntax. [styled-components] Using expression inside template literal along with attribute selector results in invalid CSS syntax. May 21, 2018
@duailibe
Copy link
Member

duailibe commented May 21, 2018

Hi @moreawarefly it's a bit of a known issue, we currently can't have expressions everywhere on CSS, just a few places. #2961 was an attempt to support this but it's a difficult issue.

What I think we should do here is try to detect edge cases and bail (i.e. print them as it was originally written).

In the meantime you can use // prettier-ignore to prevent breaking your code.

@duailibe
Copy link
Member

And I think this might be a duplicate of #2883

@skawinski
Copy link
Author

Sure, I've already disabled prettier for this line with ignore comment, thanks!

I didn't find these related tickets since I've been looking for styled keyword, since the issue seem to be triggered only if styled name is being used.

#2883 seems to be quite related, but not a duplicate tho - it's got attribute selector in the example, but it doesn't result in breaking its syntax.

@IanEdington
Copy link

I have a similar issue:

input:

const TableWrapper = styled.div
  ${TableCell}:nth-child(1) {
    flex-basis: 140px;
  }
  ${TableCell}:nth-child(2) {
    flex-basis: 140px;
  }
`;

output:

const TableWrapper = styled.div
  ${TableCell}:nth-child (1) {
    flex-basis: 140px;
  }
  ${TableCell}:nth-child (2) {
    flex-basis: 140px;
  }
`;

Notice the space in nth-child (2). This fails to apply the styles.

@ghost
Copy link

ghost commented Jul 25, 2018

Same issue as @IanEdington

input:

${FlexItem}:not(img) {

output

${FlexItem}:not (img) {

@j-f1 j-f1 added lang:css/scss/less Issues affecting CSS, Less or SCSS priority:high Code is printed in a way that alters the AST, breaks syntax, or is a significant regression. Urgent! type:bug Issues identifying ugly output, or a defect in the program labels Jul 25, 2018
@alexander-akait alexander-akait added the area:multiparser Issues with printing one language inside another, like CSS-in-JS label Nov 15, 2018
@nightpool
Copy link

prettier currently has a ton of open issues with css/styled-components—is there any way to disable prettier on all styled components until the feature is more stable?

@lydell
Copy link
Member

lydell commented Jun 23, 2019

@nightpool Unfortunately not :( (We plan to make all template literal formatting opt-in in v2.0.)

@nightpool
Copy link

@lydell is there an estimated release date or roadmap for when 2.0 will be available? the amount of critical bugs in the CSS parser is currently a huge blocker for us adopting Prettier as part of our workflow.

@lydell
Copy link
Member

lydell commented Jun 23, 2019

@nightpool There isn't. We should come up with a solution for v1.0. See also #5588.

Oh, and I thought of an ugly workaround: Rename "styled" to something else. Like import styled2 from "styled-components".

@esetnik
Copy link

esetnik commented Apr 4, 2020

Just upgraded to prettier v2, ran a fix and saw this bug for the first time:

Input:

const FilterSection = styled.section`
  ${Filter}:not(:last-child) {
    margin-bottom: 1rem;
  }
`;

Output:

```js
const FilterSection = styled.section`
  ${Filter}:not (:last-child) {
    margin-bottom: 1rem;
  }
`;

@thorn0
Copy link
Member

thorn0 commented Apr 5, 2020

Fixed by #7883

@thorn0 thorn0 closed this as completed Apr 5, 2020
@github-actions github-actions bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Jul 5, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area:multiparser Issues with printing one language inside another, like CSS-in-JS lang:css/scss/less Issues affecting CSS, Less or SCSS locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. priority:high Code is printed in a way that alters the AST, breaks syntax, or is a significant regression. Urgent! type:bug Issues identifying ugly output, or a defect in the program
Projects
None yet
Development

No branches or pull requests

9 participants