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

Formatting incorrect using styled-component component selector pattern #7826

Closed
marclemagne opened this issue Mar 22, 2020 · 5 comments · Fixed by #7842 or #7883
Closed

Formatting incorrect using styled-component component selector pattern #7826

marclemagne opened this issue Mar 22, 2020 · 5 comments · Fixed by #7842 or #7883
Labels
area:multiparser Issues with printing one language inside another, like CSS-in-JS lang:css/scss/less Issues affecting CSS, Less or SCSS lang:javascript Issues affecting JS locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:bug Issues identifying ugly output, or a defect in the program

Comments

@marclemagne
Copy link

Prettier 2.0.1
Playground link

--parser babel

Input:

const Icon = styled.div`
  flex: none;
  transition: fill 0.25s;
  width: 48px;
  height: 48px;

  ${Link}:hover {
    fill: rebeccapurple;
  }
`;

Output:

const Icon = styled.div`
  flex: none;
  transition: fill 0.25s;
  width: 48px;
  height: 48px;

  ${Link}: hover{
    fill: rebeccapurple;
  }
`;

Expected behavior:

const Icon = styled.div`
  flex: none;
  transition: fill 0.25s;
  width: 48px;
  height: 48px;

  ${Link}:hover {
    fill: rebeccapurple;
  }
`;

When using the component selector pattern from styled-components, I am seeing an issue with the formatting of the output CSS. This above is a slightly modified example from the documentation. Note that if I included an & to the selector (before the curly brace) it will format correctly (e.g., ${Link}:hover &).

The examples I am seeing in my own application are being formatted:

${DataCell}: empty: before{
${Tab}: first-child{
${Tab}:nth-child (2) {

All of which I would expect to break styling behavior.

Love the new update, though! The Prettier team must have worked incredibly hard to make so many impressive improvements.

Thank you for your time.

@j-f1 j-f1 added area:multiparser Issues with printing one language inside another, like CSS-in-JS lang:css/scss/less Issues affecting CSS, Less or SCSS lang:javascript Issues affecting JS type:bug Issues identifying ugly output, or a defect in the program labels Mar 22, 2020
@Jessidhia
Copy link

Pseudo-element selectors also break similarly:

-  ${media.smallDown}::before {
+  ${media.smallDown}: : before{

@marclemagne
Copy link
Author

Thank you for addressing so quickly!

@marclemagne
Copy link
Author

Upgraded today and it looks like a bunch of use-cases are covered now which is awesome. There are a couple of use-cases that I think are still not covered, though. Below are some examples (using the playground for PR #7842):

const Icon = styled.div`
  flex: none;
  transition: fill 0.25s;
  width: 48px;
  height: 48px;

  ${Link}:nth-child (2) {
    fill: rebeccapurple;
  }
`;
const Icon = styled.div`
  flex: none;
  transition: fill 0.25s;
  width: 48px;
  height: 48px;

  ${Link}:empty: before{
    fill: rebeccapurple;
  }
`;

And also:

const Icon = styled.div`
  flex: none;
  transition: fill 0.25s;
  width: 48px;
  height: 48px;

  ${Link}:not (:first-child) {
    fill: rebeccapurple;
  }
`;

I know it was mentioned that this was a kind of "dirty" fix to address regressions--would these use-cases be able to be fixed similarly?

Thanks again for your time!

@thorn0 thorn0 reopened this Mar 25, 2020
@thorn0
Copy link
Member

thorn0 commented Mar 25, 2020

@evilebottnawi As another quick and dirty solution, can we switch to the scss parser for embedded CSS? Seems like it doesn't have all these problems.

@alexander-akait
Copy link
Member

@thorn0 yes, I think it will save the situation, that’s why I didn’t want to update postcss-less, as a contributor stylelint I have already raised more than once the question of forking a project and rewriting, maybe we should rush it

@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 Jun 30, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 30, 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 lang:javascript Issues affecting JS locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:bug Issues identifying ugly output, or a defect in the program
Projects
None yet
5 participants