Skip to content

Commit 0497750

Browse files
committed
Fix prop-types crash when accessing constructor on props (fixes #654)
1 parent 01e8b59 commit 0497750

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

lib/rules/prop-types.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,12 @@ module.exports = Components.detect(function(context, components, utils) {
168168
for (var i = 0, j = keyList.length; i < j; i++) {
169169
var key = keyList[i];
170170
var propType = (
171-
// Check if this key is declared
172-
declaredPropTypes[key] ||
173-
// If not, check if this type accepts any key
174-
declaredPropTypes.__ANY_KEY__
171+
declaredPropTypes && (
172+
// Check if this key is declared
173+
declaredPropTypes[key] ||
174+
// If not, check if this type accepts any key
175+
declaredPropTypes.__ANY_KEY__
176+
)
175177
);
176178

177179
if (!propType) {

tests/lib/rules/prop-types.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2206,6 +2206,21 @@ ruleTester.run('prop-types', rule, {
22062206
{message: '\'firstname\' is missing in props validation'},
22072207
{message: '\'lastname\' is missing in props validation'}
22082208
]
2209+
}, {
2210+
code: [
2211+
'function Hello(props) {',
2212+
' return <div>{props.name.constructor.firstname}</div>',
2213+
'}',
2214+
'Hello.propTypes = {',
2215+
' name: PropTypes.shape({',
2216+
' firstname: PropTypes.object',
2217+
' })',
2218+
'};'
2219+
].join('\n'),
2220+
parser: 'babel-eslint',
2221+
errors: [
2222+
{message: '\'name.constructor.firstname\' is missing in props validation'}
2223+
]
22092224
}
22102225
]
22112226
});

0 commit comments

Comments
 (0)