Skip to content

prop-types rule reports for Array.prototype.length is missing #1047

Closed
@ashleahhill

Description

@ashleahhill

h2. Expected

When a propType React.PropTypes.arrayOf(<something>), you shouldn't be prompted to add
length by eslint.

h2. What happens

You get '.length' is missing in props validation (react/prop-types) from eslint

h2. Example

class MyClass extends React.Component {
    static propTypes = {
        results: PropTypes.arrayOf(
            PropTypes.shape({
                items: PropTypes.arrayOf(PropTypes.shape(<Shape definition>))
            })
        )
    };

    get hasItems() {
        this.props.results && this.props.results.items && this.props.results.items.length > 0; 
    }
}

Results in this:
'results.items.length' is missing in props validation (react/prop-types)

Activity

guidobouman

guidobouman commented on Feb 1, 2017

@guidobouman

@ljharb Any pointers as to where it would be smart to implement the filtering of default methods & properties? Somewhere in https://github.com/yannickcr/eslint-plugin-react/blob/master/lib/rules/prop-types.js ofcourse. But at what stage might this be best to implement? reportUndeclaredPropTypes() & Program:exit() seem a little late in the chain.

karissame

karissame commented on Mar 24, 2017

@karissame

I have a similar issue- is there any way to get the length of an array in your component's props?

ljharb

ljharb commented on Mar 24, 2017

@ljharb
Member

@karissame I built withShape in airbnb-prop-types specifically so you could do withShape(PropTypes.array, { length: PropTypes.number }) or similar, since React.PropTypes makes that impossible.

That said, the plugin should definitely exempt anything on Array.prototype when PropTypes.array or arrayOf is used.

added a commit that references this issue on May 26, 2019
a131363

42 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @ljharb@guidobouman@ashleahhill@karissame

      Issue actions

        `prop-types` rule reports for Array.prototype.length is missing · Issue #1047 · jsx-eslint/eslint-plugin-react