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

Does not work with CSS-modules #3

Open
akhdefault opened this issue Aug 8, 2022 · 3 comments
Open

Does not work with CSS-modules #3

akhdefault opened this issue Aug 8, 2022 · 3 comments

Comments

@akhdefault
Copy link

Here's repro:
https://github.com/akhdefault/eslint-classnames-repro/blob/main/pages/index.js#L10

@fsubal
Copy link
Owner

fsubal commented Aug 9, 2022

What is the expected behavior in this case? ( Not sure from the comment in your repo )

You mean className={classNames(styles.main)} should be fixed into className={styles.main} ?

@fsubal
Copy link
Owner

fsubal commented Aug 9, 2022

It is actually prevented here → https://github.com/fsubal/eslint-plugin-classnames/blob/main/lib/rules/prefer-classnames-function.js#L95-L115

Dropping classNames() function for single argument is problematic when it's not a string literal value.
This is why it's currently prevented.

className={classNames({ a: true })}
className={{ a: true }} // This is wrong, of course

className={classNames(foo ? 'a' : 'b')}
className={foo ? 'a' : 'b'} // This can be acceptable, but not expected?

Maybe you can add some cases for specially allowing single argument with Identifier expression ?

@akd-io
Copy link

akd-io commented Oct 13, 2022

Took a look at this today @fsubal, and ended up realizing exactly what you wrote here; that it's problematic without a literal string value.

I guess you would need to use typescript-eslint's type checking to check that classes.myClass in classNames(classes.myClass) is of type string before allowing it to collapse from

className={classNames(classes.myClass)}

into

className={classes.myClass}

But that's a significantly bigger task, and should be its own rule as type-aware linting performs slower.

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

3 participants