eslint-plugin-react-with-styles icon indicating copy to clipboard operation
eslint-plugin-react-with-styles copied to clipboard

Rule proposal: no dynamic style references

Open lencioni opened this issue 8 years ago • 7 comments

Dynamic style references make code more difficult to understand and interfere with static analycis. This weakens rules such as no-unused-styles. It would be good to have a rule that prevents this kind of coding.

Bad:

styles[foo]
styles['foo' + bar]
styles[`foo${bar}`]

I think it would also be okay to disallow the following:

styles['foo']

Good:

styles.foo

lencioni avatar Feb 17 '17 18:02 lencioni

What about things that aren't valid identifiers?

I'd think that a string literal, or a template literal with no interpolations, should be permitted in bracket notation.

ljharb avatar Feb 17 '17 18:02 ljharb

What about things that aren't valid identifiers?

Can you give me an example?

I'd think that a string literal, or a template literal with no interpolations, should be permitted in bracket notation.

Sure, I don't really care much either way about this.

lencioni avatar Feb 17 '17 18:02 lencioni

':first-child'

ljharb avatar Feb 17 '17 18:02 ljharb

Why is that not a valid identifier?

lencioni avatar Feb 17 '17 19:02 lencioni

Because it starts with a colon and contains a hyphen

ljharb avatar Feb 17 '17 20:02 ljharb

The hyphen should be fine, and the colon is potentially fine--that all depends on the underlying implementation. In our case with Aphrodite, this isn't handled very well and results in a className that contains a colon.

But, it seems like these cases should be better handled by separate rules that dictate the shape of styles properties and not a rule that dictates the type of styles properties.

lencioni avatar Feb 17 '17 22:02 lencioni

I'm not sure I'm being clear - you can't do styles.foo-bar, you have to do styles['foo-bar'] - it seems like an eslint plugin shouldn't try to be too opinionated about dot vs bracket access, nor about the casing of the property names of the styles object.

ljharb avatar Feb 18 '17 03:02 ljharb