Eslint-plugin-react: no-unused-prop-types is unable to parse type aliases

Created on 25 Sep 2017  路  9Comments  路  Source: yannickcr/eslint-plugin-react

no-unused-prop-types v.7.4.0 throws when trying to parse this code

type SomeProps = {};
type DefaultProps = SomeProps;
type Props = DefaultProps & {};
export class TestComponent extends React.Component<Props, void> {}
TypeError: Cannot read property 'length' of undefined
    at iterateProperties (eslint-plugin-react/lib/rules/no-unused-prop-types.js:336:21)
    at declarePropTypesForObjectTypeAnnotation (eslint-plugin-react/lib/rules/no-unused-prop-types.js:725:7)
    at propTypes.types.some.annotation (eslint-plugin-react/lib/rules/no-unused-prop-types.js:767:16)
flow help wanted

All 9 comments

Thanks for the report.

I did some quick digging and reproduced the issue. Additionally I found that the following code did parse correctly, but did not detect unused prop-types:

        type SomeProps = {
          firstname: string
        }
        type Props = SomeProps;
        export class TestComponent extends React.Component<Props, void> {
          render() {
            return <div>{this.props.foo}</div>
          }
        }

I have managed to get both cases working (your example + the above). I'll try to get a PR in within the next few days (as I'm currently travelling a bit).

This also errors (I think this is related):

type TProps = typeof someObject

Probably also worth testing @jseminck

Thanks. Not sure what that syntax means? Is it really valid?

Regarding the other case: sorry, have been a bit busy lately.

Since it seems that we skip props validation at the moment when we have:

type SomeProps = { firstname: string }
type Props = SomeProps;

Then I'll probably have to add similar skipping of props validation for:

type SomeProps = {};
type DefaultProps = SomeProps;
type Props = DefaultProps & {};

Several other unexpected cases presented themselves when trying to properly fix it.

Yes, this is valid Syntax. You can read more about them here: https://flow.org/en/docs/types/typeof/

I think this is the same, but at least is another good test case:

import AutoComplete from '../rfmui/AutoComplete'
import type {ElementProps} from 'react'

type AutoCompleteProps = ElementProps<typeof AutoComplete>

Also getting this issue with v7.5.1
I an using FlowType syntax annotations same as @k15a

Note sure what code is causing this.

events.js:183 throw er; // Unhandled 'error' event ^ TypeError: Cannot read property 'length' of undefined at iterateProperties (/Users/seb/repo/bonline/offer-marketplace/node_modules/eslint-plugin-react/lib/rules/no-unused-prop-types.js:355:22) at declarePropTypesForObjectTypeAnnotation (/Users/seb/repo/bonline/offer-marketplace/node_modules/eslint-plugin-react/lib/rules/no-unused-prop-types.js:752:7) at propTypes.types.some.annotation (/Users/seb/repo/bonline/offer-marketplace/node_modules/eslint-plugin-react/lib/rules/no-unused-prop-types.js:791:16) at Array.some (<anonymous>) at declarePropTypesForIntersectionTypeAnnotation (/Users/seb/repo/bonline/offer-marketplace/node_modules/eslint-plugin-react/lib/rules/no-unused-prop-types.js:778:30) at markPropTypesAsDeclared (/Users/seb/repo/bonline/offer-marketplace/node_modules/eslint-plugin-react/lib/rules/no-unused-prop-types.js:853:35) at markAnnotatedFunctionArgumentsAsDeclared (/Users/seb/repo/bonline/offer-marketplace/node_modules/eslint-plugin-react/lib/rules/no-unused-prop-types.js:948:7) at Object.handleFunctionLikeExpressions (/Users/seb/repo/bonline/offer-marketplace/node_modules/eslint-plugin-react/lib/rules/no-unused-prop-types.js:966:7) at updatedRuleInstructions.(anonymous function) (/Users/seb/repo/bonline/offer-marketplace/node_modules/eslint-plugin-react/lib/util/Components.js:654:75) at listeners.(anonymous function).forEach.listener (/Users/seb/repo/bonline/offer-marketplace/node_modules/eslint/lib/util/safe-emitter.js:47:58)

a workaround for my code was adding & {} to the type that was erroring out:

type CommonProps = {
  bar: string,
}

// TODO: remove `& {}` when eslint-plugin-react/issues/1446 is resolved
type Props = CommonProps & {}

This looks like it might have been fixed in v7.10.0 (specifically #1806). Should this be closed?

I'll close; if this is still an issue, please file a new one.

Was this page helpful?
0 / 5 - 0 ratings