Skip to content

Strange behavior of forbid-prop-types for nested prop types #2945

Closed
@zfletch

Description

@zfletch

I'm running into some unexpected behavior with the forbid-prop-types rule and nested prop types (e.g. arrayOf(object)). It looks like there's either a bug that manifests differently depending on how prop types are imported or there are two different bugs. Similar issues have been reported (#2657, #2662), but I'm re-reporting this because they're marked as fixed by 997a99f and this is still happening.

I'm including two examples along with my configuration below. The version of eslint is 7.20.0 and the version of eslint-plugin-react is 7.22.0.

Example 1

import React from './React';

import { arrayOf, object } from 'prop-types';

const App = ({ foo }) => (
  <div>
    Hello world {foo}
  </div>
);

App.propTypes = {
  foo: arrayOf(object)
}

export default App;
/tmp/test/src/App.js
  13:3  error  Prop type `undefined` is forbidden  react/forbid-prop-types

✖ 1 problem (1 error, 0 warnings)

Here, eslint is correctly returning an error, but the description should say "Prop type `object`", not "Prop type `undefined`".

Example 2

import React from './React';

import PropTypes, { arrayOf } from 'prop-types';

const App = ({ foo }) => (
  <div>
    Hello world {foo}
  </div>
);

App.propTypes = {
  foo: arrayOf(PropTypes.object)
}

export default App;
✨  Done in 0.77s.

In this case, eslint is incorrectly returning no errors.

Configuration

  "rules": {
    "react/forbid-prop-types": ["error", {
      "forbid": ["object"]
    }]
  },

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions