Skip to content

sort-prop-types throws an error with a shape defined by a const #1749

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

Closed
leggechr opened this issue Mar 29, 2018 · 2 comments · Fixed by #1823
Closed

sort-prop-types throws an error with a shape defined by a const #1749

leggechr opened this issue Mar 29, 2018 · 2 comments · Fixed by #1823

Comments

@leggechr
Copy link

leggechr commented Mar 29, 2018

The following code snippet:

const accountInfoType = {
  name: PropTypes.string,
  address: PropTypes.string,
  taxid: PropTypes.string,
};

AccountDetails.propTypes = {
  accountInfo: PropTypes.shape(accountInfoType),
}

Throws the following error:

Cannot read property 'reduce' of undefined
TypeError: Cannot read property 'reduce' of undefined
    at checkSorted ([localPath]/node_modules/eslint-plugin-react/lib/rules/sort-prop-types.js:80:19)
    at CallExpression ([localPath]/node_modules/eslint-plugin-react/lib/rules/sort-prop-types.js:166:9)
    at listeners.(anonymous function).forEach.listener ([localPath]/node_modules/eslint/lib/util/safe-emitter.js:47:58)
    at Array.forEach (native)
    at Object.emit ([localPath]/node_modules/eslint/lib/util/safe-emitter.js:47:38)
    at NodeEventGenerator.applySelector ([localPath]/node_modules/eslint/lib/util/node-event-generator.js:251:26)
    at NodeEventGenerator.applySelectors ([localPath]/node_modules/eslint/lib/util/node-event-generator.js:280:22)
    at NodeEventGenerator.enterNode ([localPath]/node_modules/eslint/lib/util/node-event-generator.js:294:14)
    at CodePathAnalyzer.enterNode ([localPath]/node_modules/eslint/lib/code-path-analysis/code-path-analyzer.js:608:23)
    at Traverser.enter [as _enter] ([localPath/node_modules/eslint/lib/linter.js:1006:32)

Declaring the propTypes in either of the following ways is a workaround so that it doesn't crash:

AccountDetails.propTypes = {
  accountInfo: PropTypes.shape({
    name: PropTypes.string,
    address: PropTypes.string,
    taxid: PropTypes.string,
  }),
}
const accountInfoType = PropTypes.shape({
  name: PropTypes.string,
  address: PropTypes.string,
  taxid: PropTypes.string,
});

AccountDetails.propTypes = {
  accountInfo: accountInfoType,
}
@ljharb
Copy link
Member

ljharb commented Apr 1, 2018

When the value passed into shape is defined in the same file, it would be ideal to resolve it. Separately, if the value is defined elsewhere, we should ignore it instead of crashing.

@alexzherdev
Copy link
Contributor

I've looked into this, and it seems like the error had been dealt with in #1669. That PR doesn't have any resolution logic though.

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

Successfully merging a pull request may close this issue.

3 participants