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

no-unused-prop-types is unable to parse type aliases #1446

Closed
Li0liQ opened this issue Sep 25, 2017 · 9 comments
Closed

no-unused-prop-types is unable to parse type aliases #1446

Li0liQ opened this issue Sep 25, 2017 · 9 comments

Comments

@Li0liQ
Copy link

Li0liQ commented Sep 25, 2017

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)
@jseminck
Copy link
Contributor

jseminck commented Sep 25, 2017

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).

@k15a
Copy link

k15a commented Oct 11, 2017

This also errors (I think this is related):

type TProps = typeof someObject

Probably also worth testing @jseminck

@jseminck
Copy link
Contributor

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.

@k15a
Copy link

k15a commented Oct 12, 2017

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

@rosskevin
Copy link
Contributor

rosskevin commented Nov 20, 2017

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>

@sscholle
Copy link

sscholle commented Dec 22, 2017

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)

@cogell
Copy link

cogell commented Apr 30, 2018

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 & {}

@nicholas-l
Copy link
Contributor

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

@ljharb
Copy link
Member

ljharb commented Jun 28, 2018

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

@ljharb ljharb closed this as completed Jun 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

8 participants