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

Add support for nested child selectors #25

Open
satazor opened this issue Aug 26, 2017 · 20 comments
Open

Add support for nested child selectors #25

satazor opened this issue Aug 26, 2017 · 20 comments

Comments

@satazor
Copy link
Contributor

satazor commented Aug 26, 2017

<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 #15

@vasco-santos
Copy link

vasco-santos commented Oct 23, 2017

Any news regarding this problem? 😟

Thanks

@nicollecastrog
Copy link

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!

@amannn
Copy link
Contributor

amannn commented Nov 22, 2017

@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?

@chapati23
Copy link

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>
)

@amannn
Copy link
Contributor

amannn commented Dec 3, 2017

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.

@chapati23
Copy link

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

@olgenn
Copy link

olgenn commented Mar 5, 2018

Any news regarding this problem? Thanks!

@olgenn
Copy link

olgenn commented Mar 15, 2018

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

@atfzl
Copy link
Owner

atfzl commented Mar 15, 2018

@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;
  }
}

@olgenn
Copy link

olgenn commented Mar 15, 2018

@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.

@atfzl
Copy link
Owner

atfzl commented Mar 15, 2018

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

@olgenn
Copy link

olgenn commented Mar 15, 2018

@atfzl Yep.

@atfzl
Copy link
Owner

atfzl commented Mar 15, 2018

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: #32

Are you sure that you have the camelCase option enabled.

@olgenn
Copy link

olgenn commented Mar 15, 2018

@atfzl 7b86ff1#diff-0361dac614e957fd8929e88158cb5b9bR341 There must be className={s.fooBar}

@atfzl
Copy link
Owner

atfzl commented Mar 15, 2018

I have fixed the PR with s.fooBar.

@atfzl
Copy link
Owner

atfzl commented Mar 15, 2018

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.

@olgenn
Copy link

olgenn commented Mar 15, 2018

I use postcss. Files extension .css

@atfzl
Copy link
Owner

atfzl commented Mar 15, 2018

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.

@olgenn
Copy link

olgenn commented Mar 15, 2018

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

@ajarbol
Copy link

ajarbol commented Sep 18, 2019

@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..

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

No branches or pull requests

8 participants