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

Disabling prop-types on the function declaration line is no longer always sufficient #2325

Closed
ThiefMaster opened this issue Jun 24, 2019 · 8 comments · Fixed by #2699
Closed

Comments

@ThiefMaster
Copy link
Contributor

ThiefMaster commented Jun 24, 2019

I have this function (which isn't even a component):

// eslint-disable-next-line react/prop-types
const renderEquipment = ({equipment, features}) => {
  const count = equipment.length + features.length;
  if (!count) {
    return null;
  }
  return (
    <>
      <Translate>Equipment</Translate>
      <Label circular horizontal className="white" size="tiny" styleName="filter-bar-button-label">
        {count}
      </Label>
    </>
  );
};

As you can see, I disabled the prop-types validation on it. However, after updating from 7.13 to 7.14.1, I'm now getting additional errors on the line where I'm accessing the length of the arrays:

  39:27  error  'equipment.length' is missing in props validation  react/prop-types
  39:45  error  'features.length' is missing in props validation   react/prop-types

I guess this happened because now accessing nested props also trigger a prop-type validation - but of course this makes it very ugly to disable prop-type validation for a whole (non-)component...

Maybe the component detection should never consider functions whose name starts with a lowercase character components, since they can't be components (when used in JSX)...

@yannickcr
Copy link
Member

yannickcr commented Jun 24, 2019

Props detection became more powerful in last release, hence these new errors.

Disabling component detection on lowercase functions was discussed in #512 but no decision was made yet.

Right now the easiest way to disable react/prop-types on your component is to wrap it between eslint-disable/eslint-enable directives:

/* eslint-disable react/prop-types */
const renderEquipment = ({equipment, features}) => {
  const count = equipment.length + features.length;
  if (!count) {
    return null;
  }
  return (
    <>
      <Translate>Equipment</Translate>
      <Label circular horizontal className="white" size="tiny" styleName="filter-bar-button-label">
        {count}
      </Label>
    </>
  );
};
/* eslint-enable react/prop-types */

@ThiefMaster
Copy link
Contributor Author

Yes, that's what I went for in the end.

@GreenGremlin
Copy link

This seems like a breaking change, in a minor version release. Can it be rolled back and pushed in a major version release?

@GreenGremlin
Copy link

Also, this is going to be very confusing for developers. Where was this change discussed?

@ljharb
Copy link
Member

ljharb commented Jun 24, 2019

the issue here isn’t the change; its that that render function is a component by our heuristic (I’d argue it is one conceptually and thus should be defined as one, but that’s a separate topic).

If you define a propType for both of those props as an array, you won’t get a warning.

@GreenGremlin
Copy link

Also, this is going to be very confusing for developers. Where was this change discussed?

Digging a little more, I'm ok with the change, just not rolling it out as a minor version bump.

@yannickcr
Copy link
Member

yannickcr commented Jun 24, 2019

We try to follow the same Semantic Versioning Policy as ESLint, this case being covered by:

A bug fix in a rule that results in ESLint reporting more errors.

So AFAIK it is ok to release it in a minor.

@jzabala
Copy link
Contributor

jzabala commented Jul 10, 2020

With #2699 render functions are not considered components. This one should be fixed now.

@ljharb

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

Successfully merging a pull request may close this issue.

5 participants