eslint-plugin-css-modules icon indicating copy to clipboard operation
eslint-plugin-css-modules copied to clipboard

Add support for nested child selectors

Open satazor opened this issue 8 years ago • 20 comments

<div className={ `${styles.container} ${styles.isLoading}` }>
    This container is green
</div>
.container {
    &-green {
        background-color: green;
    }

    &.isLoading {
        background-color: grey;
    }
}

The linter does not recognise .container.isLoading, even though they exist.

See https://github.com/atfzl/eslint-plugin-css-modules/issues/15

satazor avatar Aug 26 '17 23:08 satazor

Any news regarding this problem? 😟

Thanks

vasco-santos avatar Oct 23 '17 12:10 vasco-santos

Commented about this on #15 as well, but even though I'm using version 2.7.5, still not recognising nested classes as per styles.isLoading example above. @amannn given the work you did on #20, can you shed any light perhaps?

Thanks for the great plugin!

nicollecastrog avatar Nov 21 '17 20:11 nicollecastrog

@satazor Hm, your code gives me the warning Unused classes found: container-green (css-modules/no-unused-class). But that's because you're not using that class.

E.g. this code works fine for me with version 2.7.5:

<div className={`${styles.container} ${styles['container-green']} ${styles.isLoading}`}>
  This container is green
</div>

Can you maybe submit a failing test as a PR?

amannn avatar Nov 22 '17 13:11 amannn

Doesn't work for me either. eslint-plugin-css-modules just pretends the nested .test class doesn't exist.

Webpack Loader

    use: [
      {
        loader: 'css-loader',
        options: {
          camelCase: true,
          importLoaders: 1,
          modules: true,
          localIdentName: '[name]__[local]--[hash:base64:5]'
        }
      },
      'postcss-loader?sourceMap'
    ]

styles.css

.wrapper {
  background: blue;
  color: white;

  & .test {
    color: red;
  }
}

React Component

export const Home = () => (
  <div className={styles.wrapper}>
    <h1 className={styles.test}>Doesn't work</h1>
  </div>
)

chapati23 avatar Dec 03 '17 14:12 chapati23

Hmm, if that's the case, that's an error.

However, do you even need that & sign here? Omitting it should produce the same result, right?

Anyway if you find an error, it would be helpful if you could submit a PR with a failing test case.

amannn avatar Dec 03 '17 18:12 amannn

It's postcss-nesting which is based on the official CSS Nesting Module Level 3 Spec.

So I guess I either have to quit using it and revert back to the more verbose:

.wrapper .test {}

or I can't use this great plugin

chapati23 avatar Dec 04 '17 15:12 chapati23

Any news regarding this problem? Thanks!

olgenn avatar Mar 05 '18 21:03 olgenn

@atfzl I am willing to help. Can you describe what needs to be done?

olgenn avatar Mar 15 '18 09:03 olgenn

@olgenn I tried reproducing the issue but could not reproduce it.

I tried the following setup and it is working correctly, may be I am missing something. Please help.

import s from './parentClassSelector1.scss';

export default Foo = () => (
  <div className={s.wrapper}>
    <h1 className={s.test}>foobar</h1>
  </div>
)
.wrapper {
  background: blue;
  color: white;

  &.test {
    color: red;
  }
}

atfzl avatar Mar 15 '18 14:03 atfzl

@atfzl The problem is different. I have this css styles

.wrapper {
  background: blue;
  color: white;

  & .test-text {
    color: red;
  }
}

import s from './css.css';

export default Foo = () => (
  <div className={s.wrapper}>
    <h1 className={s.testText}>foobar</h1>
  </div>
)

And the console displays an error that class'.test Text ' does not exist. But the camelCase option is enabled.

olgenn avatar Mar 15 '18 14:03 olgenn

Ok, so the problem exists only for dash-cases class names and not on camelCase. Can you please confirm.

atfzl avatar Mar 15 '18 14:03 atfzl

@atfzl Yep.

olgenn avatar Mar 15 '18 14:03 olgenn

I tried the example that you provided and it is still working, I have created a PR with new tests and they are passing. Link to PR: https://github.com/atfzl/eslint-plugin-css-modules/pull/32

Are you sure that you have the camelCase option enabled.

atfzl avatar Mar 15 '18 14:03 atfzl

@atfzl https://github.com/atfzl/eslint-plugin-css-modules/pull/32/commits/7b86ff148658fb1c0269e53fa38544c4b199d222#diff-0361dac614e957fd8929e88158cb5b9bR341 There must be className={s.fooBar}

olgenn avatar Mar 15 '18 14:03 olgenn

I have fixed the PR with s.fooBar.

atfzl avatar Mar 15 '18 14:03 atfzl

I think i have found the problem. The nested syntax works with scss but not css. Seems there is some issue in gonzales-pe parser and it does not recognise nesting in css but does in scss.

atfzl avatar Mar 15 '18 14:03 atfzl

I use postcss. Files extension .css

olgenn avatar Mar 15 '18 14:03 olgenn

I don't think this would be fixed so easily because it is dependent on gonzales-pe. Meanwhile I suggest that you can use scss for your css. If you are using webpack, you can use sass-loader combined with postcss-loader or css-loader with postcss plugin.

atfzl avatar Mar 15 '18 15:03 atfzl

@atfzl Thank you, I have time to wait. Maybe you should write to developers gonzales-pe?

olgenn avatar Mar 15 '18 15:03 olgenn

@olgenn over a year later, did you ever get progress on this issue? I've had to disable the plugin to consume the full postcss capabilities, but the linting was extremely helpfull..

ajarbol avatar Sep 18 '19 12:09 ajarbol