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

Selectors within template literal CSS get newlined and cause parsing error #13961

Open
kylemh opened this issue Dec 8, 2022 · 4 comments
Open
Labels
area:multiparser Issues with printing one language inside another, like CSS-in-JS lang:css/scss/less Issues affecting CSS, Less or SCSS type:bug Issues identifying ugly output, or a defect in the program

Comments

@kylemh
Copy link

kylemh commented Dec 8, 2022

Prettier 2.7.1
Playground link

--parser babel

Input:

const SomeStyledSpan = styled.span``;
const SomeStyledDiv = styled.div``;
const SomeStyledSection = styled.section``;

const SomeStyledComponent = styled.span`
   & ${SomeStyledSpan}:empty ~ ${SomeStyledDiv} ${SomeStyledSection},
   & ${SomeStyledSpan}:active ~ ${LabelContainer} ${SomeStyledSection} {
     transform: none;
   }
`;

Output:

const SomeStyledSpan = styled.span``;
const SomeStyledDiv = styled.div``;
const SomeStyledSection = styled.section``;

const SomeStyledComponent = styled.span`
  &
    ${SomeStyledSpan}:empty
    ~ ${SomeStyledDiv}
    ${SomeStyledSection},
    &
    ${SomeStyledSpan}:active
    ~ ${LabelContainer}
    ${SomeStyledSection} {
    transform: none;
  }
`;

Expected behavior:
When using styled-components strings can't wrap if they're part of a single selector otherwise we get a CSS parsing error.

@so1ve
Copy link
Contributor

so1ve commented Apr 27, 2023

…What does the title mean

@kachkaev kachkaev added the status:awaiting response Issues that require answers to questions from maintainers before action can be taken label Apr 27, 2023
@kylemh kylemh changed the title 1 Selectors within template string literal CSS parsers get newlined and break Apr 27, 2023
@kylemh
Copy link
Author

kylemh commented Apr 27, 2023

😂 that is a GREAT question. Fixed the title @so1ve / @kachkaev

@github-actions github-actions bot removed the status:awaiting response Issues that require answers to questions from maintainers before action can be taken label Apr 27, 2023
@kylemh kylemh changed the title Selectors within template string literal CSS parsers get newlined and break Selectors within template string literal CSS parsers get newlined and cause parsing error Apr 27, 2023
@kylemh kylemh changed the title Selectors within template string literal CSS parsers get newlined and cause parsing error Selectors within template literal CSS get newlined and cause parsing error Apr 27, 2023
@kachkaev
Copy link
Member

kachkaev commented Apr 27, 2023

Thanks for updating the title @kylemh! I guess this bug is similar to #6392 and #5086

In the meantime, you can use // prettier-ignore as a workaround:

Prettier 2.8.8
Playground link

--parser babel

Input:

const SomeStyledSpan = styled.span``;
const SomeStyledDiv = styled.div``;
const SomeStyledSection = styled.section``;

// prettier-ignore
const SomeStyledComponent = styled.span`
   & ${SomeStyledSpan}:empty ~ ${SomeStyledDiv} ${SomeStyledSection},
   & ${SomeStyledSpan}:active ~ ${LabelContainer} ${SomeStyledSection} {
     transform: none;
   }
`;

Output:

const SomeStyledSpan = styled.span``;
const SomeStyledDiv = styled.div``;
const SomeStyledSection = styled.section``;

// prettier-ignore
const SomeStyledComponent = styled.span`
   & ${SomeStyledSpan}:empty ~ ${SomeStyledDiv} ${SomeStyledSection},
   & ${SomeStyledSpan}:active ~ ${LabelContainer} ${SomeStyledSection} {
     transform: none;
   }
`;

@kachkaev kachkaev added type:bug Issues identifying ugly output, or a defect in the program lang:css/scss/less Issues affecting CSS, Less or SCSS area:multiparser Issues with printing one language inside another, like CSS-in-JS labels Apr 27, 2023
@kylemh
Copy link
Author

kylemh commented Apr 27, 2023

Yep! I've been using // prettier-ignore to resolve or just abstracting the selector to a hoisted variable.

One big difference between the linked issues is that the formatting in this issue causes CSS parser errors. Prettier breaks other tools here.

The other two issues are just about subjective desires regarding when/how to do line breaks after comma separations. Prettier doesn't break other tools regardless of those issues being worked on or ignored.

In other words, I think this issue is a much bigger deal than #6392 or #5086

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:multiparser Issues with printing one language inside another, like CSS-in-JS lang:css/scss/less Issues affecting CSS, Less or SCSS type:bug Issues identifying ugly output, or a defect in the program
Projects
None yet
Development

No branches or pull requests

3 participants