diff --git a/lib/rules/sort-prop-types.js b/lib/rules/sort-prop-types.js index 8ca7879623..a56cf4b786 100644 --- a/lib/rules/sort-prop-types.js +++ b/lib/rules/sort-prop-types.js @@ -77,6 +77,12 @@ module.exports = { * @returns {void} */ function checkSorted(declarations) { + // Declarations will be `undefined` if the `shape` is not a literal. For + // example, if it is a propType imported from another file. + if (!declarations) { + return; + } + declarations.reduce((prev, curr, idx, decls) => { if (/SpreadProperty$/.test(curr.type)) { return decls[idx + 1]; diff --git a/tests/lib/rules/sort-prop-types.js b/tests/lib/rules/sort-prop-types.js index 51d5a6a892..01095ee035 100644 --- a/tests/lib/rules/sort-prop-types.js +++ b/tests/lib/rules/sort-prop-types.js @@ -365,6 +365,24 @@ ruleTester.run('sort-prop-types', rule, { options: [{ sortShapeProp: true }] + }, { + code: ` + class Component extends React.Component { + render() { + return
; + } + } + Component.propTypes = { + a: PropTypes.any, + b: PropTypes.any, + c: PropTypes.shape( + importedPropType, + ), + }; + `, + options: [{ + sortShapeProp: true + }] }], invalid: [{