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

False positives with > #22

Open
bensaufley opened this issue Dec 3, 2024 · 0 comments
Open

False positives with > #22

bensaufley opened this issue Dec 3, 2024 · 0 comments

Comments

@bensaufley
Copy link

bensaufley commented Dec 3, 2024

Problem

I've got PostCSS that looks like this with the nesting plugin, making use of the > child combinator:

.foo {
  > button {
    color: green;
  }

  &.warn > button {
    color: red;

    &:hover,
    &:focus {
      color: blue;
    }
  }
}

.bar {
  button {
    color: yellow;
  }

  > button {
    color: blue;
  }
}

I don't see a deeper way to nest these, but this plugin wants me to.

Expected

No violation, no change. Output of CSS when nesting is resolved should look like:

.foo > button {
  color: green;
}

.foo.warn > button {
  color: red
}

.foo.warn > button:hover,
.foo.warn > button:focus {
  color: blue;
}

.bar button {
  color: yellow;
}

.bar > button {
  color: blue;
}

Encountered

As mentioned above, I see a rule violation on &.warn > button and the latter > button. When I use auto-fix, this is what I get:

.foo {
  > button {
    color: green;

    &.warn & {
      color: red;

      &:hover,
      &:focus {
        color: blue;
      }
    }
  }
}

.bar {
  button {
    color: yellow;

    > & {
      color: blue;
    }
  }
}

This is obviously not an equivalent structure. This will boil down to:

.foo > button {
  color: green
}
.foo > button.warn .foo > button {
  color: red
}
.foo > button.warn .foo > button:hover,
.foo > button.warn .foo > button:focus {
  color: blue;
}

.bar button {
  color: yellow
}
> .bar button { /* I don't think this is even valid CSS? */
  color: blue;
}

Proposed solution

I can't say exactly what's going on under the hood here but there seems to be a fundamental misunderstanding of the > combinator. Obviously I would expect this to be a violation and correctly fixed:

.foo { color: green; }
.foo > .bar { color: red; }

/* becomes: */
.foo {
  color: green;

  > .bar {
    color: red;
  }
}

But it seems like something extra is happening and I'm not exactly sure what needs to change there so that correct nesting can still happen while this false positive does not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant