lightningcss icon indicating copy to clipboard operation
lightningcss copied to clipboard

feat: Implement pure mode lints for CSS Modules

Open kdy1 opened this issue 1 year ago • 5 comments

Relevant code: https://github.com/css-modules/postcss-modules-local-by-default/blob/5eeb3053bc40061022ebc6f41cd207a405484de1/src/index.js#L518-L520

kdy1 avatar Aug 14 '24 07:08 kdy1

Could we please also add tests for nesting? Because that's buggy right now in https://github.com/css-modules/postcss-modules-local-by-default

should not error:

div {
  .my-class {
     color: red; 
  }
}
div {
  .my-class {
     span {
       color: red; 
     }
  }
}

should error:

div {
  .my-class {
     color: red; 
  }
  color: blue
}

jantimon avatar Aug 20 '24 07:08 jantimon

What should happen for cases like .my-class:is(a)? Currently that errors, presumably while parsing the inner selector inside the :is. If this selector had been written as a.my-class it would not error so seems like it probably shouldn't in the :is case also?

devongovett avatar Aug 28 '24 00:08 devongovett

I think html { .foo { span { color: red; } } } is nearly impossible to handle with the current model

kdy1 avatar Aug 29 '24 03:08 kdy1

We are detecting from selector passer, not AST visitor

kdy1 avatar Aug 29 '24 03:08 kdy1

I see - checking only the selector has worked very well for many years
Unfortunately css nesting changed that..

this test case works in nextjs using postcss but never will in lightningcss https://stackblitz.com/edit/stackblitz-starters-ywc6pu?file=app%2Fpage.module.css

jantimon avatar Aug 30 '24 07:08 jantimon

I think this should not be allowed:

html {
  .foo {
    /* ... */
  }
}

because it's easy to start adding declarations to the root html rule itself which is not pure.

However the inverse seems ok:

.foo {
  div {
    /* ... */
  }
}

devongovett avatar Aug 31 '24 13:08 devongovett

Updated to implement this in the transform pass rather than in the selector parser, which enables handling of nesting as shown above.

devongovett avatar Aug 31 '24 14:08 devongovett

thanks @kdy1 and @devongovett for all your work!

jantimon avatar Aug 31 '24 14:08 jantimon

Thanks @kdy1 @devongovett @jantimon, great collaboration 😌

timneutkens avatar Sep 03 '24 09:09 timneutkens