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

7.4.0-rc.0 flow imported type intersection error - Cannot read property 'length' of undefined in prop-types.js #1413

Closed
rosskevin opened this issue Sep 5, 2017 · 13 comments

Comments

@rosskevin
Copy link
Contributor

We are heavy flow users and are on flow 0.54.0, so I assume this may be related to @jseminck's changes in #1400. This may be an entirely new case or separate bug.

 yarn list eslint-plugin-react eslint                                                                                                                                                                   ⏎
yarn list v0.27.5
├─ eslint-plugin-react@7.4.0-rc.0
└─ eslint@4.5.0
git clone https://github.com/alienfast/material-ui.git
git checkout remove-class-property-props
yarn install
yarn lint
Cannot read property 'length' of undefined
TypeError: Cannot read property 'length' of undefined
    at iterateProperties (/Users/kross/projects/material-ui/node_modules/eslint-plugin-react/lib/rules/prop-types.js:365:21)
    at propTypes.types.forEach.annotation (/Users/kross/projects/material-ui/node_modules/eslint-plugin-react/lib/rules/prop-types.js:798:13)
    at Array.forEach (<anonymous>)
    at markPropTypesAsDeclared (/Users/kross/projects/material-ui/node_modules/eslint-plugin-react/lib/rules/prop-types.js:796:27)
    at markAnnotatedFunctionArgumentsAsDeclared (/Users/kross/projects/material-ui/node_modules/eslint-plugin-react/lib/rules/prop-types.js:911:7)
    at Object.handleStatelessComponent (/Users/kross/projects/material-ui/node_modules/eslint-plugin-react/lib/rules/prop-types.js:920:7)
    at Linter.updatedRuleInstructions.(anonymous function) (/Users/kross/projects/material-ui/node_modules/eslint-plugin-react/lib/util/Components.js:666:75)
    at emitOne (events.js:120:20)
    at Linter.emit (events.js:210:7)
    at NodeEventGenerator.applySelector (/Users/kross/projects/material-ui/node_modules/eslint/lib/util/node-event-generator.js:265:26)

It appears to be failing when visiting IntersectionTypeAnnotation:

prop-types_js_-material-ui-___projects_material-ui

How can I help narrow this down further?

@rosskevin
Copy link
Contributor Author

rosskevin commented Sep 5, 2017

It appears to happen when there is an intersection of an imported type:

import type { HiddenProps } from './types';

type DefaultProps = {
  classes: Object,
};

export type Props = HiddenProps & {
  classes?: Object,
};

function HiddenCss(props: DefaultProps & Props) {

prop-types_js_-material-ui-___projects_material-ui

@rosskevin rosskevin changed the title 7.4.0-rc.0 error Cannot read property 'length' of undefined in prop-types.js 7.4.0-rc.0 flow imported type intersection error - Cannot read property 'length' of undefined in prop-types.js Sep 5, 2017
@jseminck
Copy link
Contributor

jseminck commented Sep 6, 2017

Thanks for looking into this. I guess it is related to: #1404 where we added support for the IntersectionTypeAnnotation.

I can fix the crash easily, but can we even properly validate the props in this case? Since they are imported from another file, we do not what's inside them... 😞

@jseminck
Copy link
Contributor

jseminck commented Sep 6, 2017

I did a quick check and there were 2 issues:

  • One is more related to the fact that there were multiple intersections. The rule only handled one intersection, e.g. PropsA & PropsB - but in your case you have a second intersection as well.
  • When a type is imported, it seems like we skip props validation because we cannot figure out what props are actually imported from the other file.

@jseminck
Copy link
Contributor

jseminck commented Sep 6, 2017

@rosskevin @bsr203 can you check out https://github.com/jseminck/eslint-plugin-react/tree/intersection-imported-type and see if it fixes the issue for you? I've added test cases that cover the examples here.

@rosskevin did the issue appear in the material-ui code base? Yesterday I was looking around for open-source code bases that we could use to test releases. Perhaps it's a good candidate? My other ideas so far were https://codesandbox.io/ & https://babeljs.io/repl/ 😄

@jseminck
Copy link
Contributor

jseminck commented Sep 6, 2017

I tested on material-ui with this branch and found another problem with the HiddenCss.js component in that codebase:

I think it's the following case:

export type Props = HiddenProps & {
  /**
   * Useful to extend the style applied to components.
   */
  classes?: Object,
};

@bsr203
Copy link

bsr203 commented Sep 6, 2017

hi @yannickcr

I get a different stack this time.

Cannot read property 'name' of undefined
TypeError: Cannot read property 'name' of undefined
    at propTypes.types.forEach.annotation (/Users/bsr/client/node_modules/eslint-plugin-react/lib/rules/prop-types.js:726:49)
    at Array.forEach (<anonymous>)
    at declarePropTypesForIntersectionTypeAnnotation (/Users/bsr/client/node_modules/eslint-plugin-react/lib/rules/prop-types.js:725:23)
    at markPropTypesAsDeclared (/Users/bsr/client/node_modules/eslint-plugin-react/lib/rules/prop-types.js:831:35)
    at markAnnotatedFunctionArgumentsAsDeclared (/Users/bsr/client/node_modules/eslint-plugin-react/lib/rules/prop-types.js:937:7)
    at Object.handleStatelessComponent (/Users/bsr/client/node_modules/eslint-plugin-react/lib/rules/prop-types.js:946:7)
    at Linter.updatedRuleInstructions.(anonymous function) (/Users/bsr/client/node_modules/eslint-plugin-react/lib/util/Components.js:666:75)
    at emitOne (events.js:120:20)
    at Linter.emit (events.js:210:7)
    at NodeEventGenerator.applySelector (/Users/bsr/client/node_modules/eslint/lib/util/node-event-generator.js:265:26)

@jseminck
Copy link
Contributor

jseminck commented Sep 6, 2017

@bsr203 see my last comment. I just pushed a fix to my branch and tested it on the material-ui project and now everything passes 🎉

I still have to clean up the code though... it's a bit messy.

To try it out (or npm i --save-dev if you use npm)

rm -rf node_modules/eslint-plugin-react/
yarn add -D https://github.com/jseminck/eslint-plugin-react#intersection-imported-type

@bsr203
Copy link

bsr203 commented Sep 6, 2017

@jseminck everything looks good for me. no crash and also props are being detected.
P.S. If anyone try to install the github branch, I had to yarn cache clean, as it keep getting previous commit from cache so wasn't updating with latest.

thanks again for the quick fix and help.

@rosskevin
Copy link
Contributor Author

Thanks everyone for the assistance.

Regarding importing from other files, it is possible. I did some research into the matter and commented my findings here: brigand/babel-plugin-flow-react-proptypes#106 (comment)

Essentially, this is a common problem and thejameskyle/babel-plugin-react-flow-props-to-prop-types has a solution to grab the type definitions from another file, if it is wanted.

@jseminck
Copy link
Contributor

jseminck commented Sep 6, 2017

Cool. Thanks for testing @bsr203. I'll clean up the code and push the changes. It'll take a bit of time as I have some things to attend to.

Regarding imports from other files: For now I'll stick to the same logic as we applied previously: if there's an import of a type, then we cannot find it, and we just skip props validation for that file.

I'll try to read through that thread later and see how complicated it is 😄

@rosskevin
Copy link
Contributor Author

@jseminck - if there is a way to extract that code and grab imported types, it might be a generally useful utility project. I know the brigand/babel-plugin-flow-react-proptypes needs it, and we could use it here. Like everyone, we all have limited time so I understand! These issues are linked now so I'll post over here if I end up doing it over there.

And yes, the callemall/material-ui#v1-beta is a perfect project for smoke testing!

@EvHaus
Copy link
Collaborator

EvHaus commented Sep 8, 2017

Closely related to this issue I'm also seeing the following code:

type SomeType = {};

type SomeOtherType = {
	a: number
} & SomeType;

Result in the following eslint-plugin-react crash:

Cannot read property 'name' of undefined TypeError: Cannot read property 'name' of undefined
    at propTypes.types.forEach.annotation (/Users/ev.haus/Git/web-platform/node_modules/eslint-plugin-react/lib/rules/prop-types.js:797:54)
    at Array.forEach (native)
    at markPropTypesAsDeclared (/Users/ev.haus/Git/web-platform/node_modules/eslint-plugin-react/lib/rules/prop-types.js:796:27)
    at Object.ClassDeclaration (/Users/ev.haus/Git/web-platform/node_modules/eslint-plugin-react/lib/rules/prop-types.js:930:11)
    at EventEmitter.updatedRuleInstructions.(anonymous function) (/Users/ev.haus/Git/web-platform/node_modules/eslint-plugin-react/lib/util/Components.js:666:75)
    at emitOne (events.js:101:20)
    at EventEmitter.emit (events.js:188:7)
    at NodeEventGenerator.applySelector (/Users/ev.haus/Git/web-platform/node_modules/eslint/lib/util/node-event-generator.js:265:26)
    at NodeEventGenerator.applySelectors (/Users/ev.haus/Git/web-platform/node_modules/eslint/lib/util/node-event-generator.js:294:22)
    at NodeEventGenerator.enterNode (/Users/ev.haus/Git/web-platform/node_modules/eslint/lib/util/node-event-generator.js:308:14)
    at CodePathAnalyzer.enterNode (/Users/ev.haus/Git/web-platform/node_modules/eslint/lib/code-path-analysis/code-path-analyzer.js:606:23)
    at Traverser.enter (/Users/ev.haus/Git/web-platform/node_modules/eslint/lib/linter.js:1035:32)
    at Traverser.__execute (/Users/ev.haus/Git/web-platform/node_modules/estraverse/estraverse.js:397:31)
    at Traverser.traverse (/Users/ev.haus/Git/web-platform/node_modules/estraverse/estraverse.js:501:28)
    at Traverser.traverse (/Users/ev.haus/Git/web-platform/node_modules/eslint/lib/util/traverser.js:31:22)
    at Linter.verify (/Users/ev.haus/Git/web-platform/node_modules/eslint/lib/linter.js:1032:24)
    at processText (/Users/ev.haus/Git/web-platform/node_modules/eslint/lib/cli-engine.js:203:31)
    at CLIEngine.executeOnText (/Users/ev.haus/Git/web-platform/node_modules/eslint/lib/cli-engine.js:668:26)
    at lintJob (/Users/evgueni.naverniouk/.atom/packages/linter-eslint/src/worker.js:14:20)
    at process.<anonymous> (/Users/evgueni.naverniouk/.atom/packages/linter-eslint/src/worker.js:52:22)
    at emitTwo (events.js:111:20)
    at process.emit (events.js:191:7)
    at process.nextTick (internal/child_process.js:752:12)
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)

@jseminck
Copy link
Contributor

jseminck commented Sep 8, 2017

@EvHaus this is already handled in the PR 😄 though I only tested Props & { ...}, now I also added tests for {...} & Props now.

Can you try with #1415 ?

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

No branches or pull requests

4 participants