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

Create only-extend-defined rule #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

TaeKimJR
Copy link

"only-extend-defined" ESLint Rule
Ensures that any extendable styles are defined in the component's style definition. This PR was created based on feedback on the extending styles PR.

Failing Example

export default withStyles(
  () => ({
    container: {
      background: 'red',
    },
  }),
  {
    extendableStyles: {
      container: {
        color: () => true, // would fail here
      },
      foo: { // would also fail here
        background: () => true,
      },
    },
  },
)(Foo);

},

configs: {
recommended: {
rules: {
'react-with-styles/only-spread-css': 'error',
'react-with-styles/no-unused-styles': 'error',
'react-with-styles/only-extend-defined': 'error',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, adding this to the recommended list makes this a breaking change. Sometimes we add rules at first and then when we are ready to make a breaking change, we lump all of the modifications to the recommended config into one breaking change.

However, given how rarely we work on this particular plugin, it might make sense to do this now.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might still be worth it to ship it as minor first (without this line) and then do a followup as a major that enables it.

extendedStyle.properties.forEach((property) => {
if (property.computed) {
// Skip over computed properties for now.
// e.g. `{ [foo]: { ... } }`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this raises an interesting question: should there be a rule to disallow computed properties for extendable styles (or for styles in general)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen a lot of usages of constants for things around responsiveness (e.g. [responsive.mediumAndAbove]). I think we would begin to limit ourselves a lot if we disallowed computed properties.

I wonder if we can ensure that the same variable is used in both places... I can take a look tonight.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right! Can we add some valid and invalid test cases for nested styles? Media queries, @supports queries, pseudo-elements, pseudo-selectors, etc.


if (property.type === 'ExperimentalSpreadProperty' || property.type === 'SpreadElement') {
// Skip over object spread for now.
// e.g. `{ ...foo }`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, should spreading in extendable styles be disallowed?

)(Foo);
`.trim(),
errors: [{
message: '`background` is not extendable',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can make this error message a little more helpful:

foo.background is not extendable because it is undefined

)(Foo);
`.trim(),
errors: [{
message: '`bar` is not extendable',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, add "because it is undefined" or something similar

foo: {
background: () => true,
color: () => true,
fontSize: () => true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have some valid and invalid examples where the functions use args?

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

Successfully merging this pull request may close these issues.

3 participants