Skip to content

Commit e169101

Browse files
committed
Fix prop-types to not mark class static function as valid propTypes definition (fixes #1174)
1 parent 6bcb70f commit e169101

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

lib/rules/prop-types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ module.exports = {
879879
},
880880

881881
MethodDefinition: function(node) {
882-
if (!isPropTypesDeclaration(node.key)) {
882+
if (!node.static || node.kind !== 'get' || !isPropTypesDeclaration(node.key)) {
883883
return;
884884
}
885885

tests/lib/rules/prop-types.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2616,6 +2616,23 @@ ruleTester.run('prop-types', rule, {
26162616
errors: [
26172617
{message: '\'foo\' is missing in props validation'}
26182618
]
2619+
}, {
2620+
code: [
2621+
'class Hello extends React.Component {',
2622+
' static propTypes() {',
2623+
' return {',
2624+
' name: PropTypes.string',
2625+
' };',
2626+
' }',
2627+
' render() {',
2628+
' return <div>Hello {this.props.name}</div>;',
2629+
' }',
2630+
'}'
2631+
].join('\n'),
2632+
parserOptions: parserOptions,
2633+
errors: [
2634+
{message: '\'name\' is missing in props validation'}
2635+
]
26192636
}
26202637
]
26212638
});

tests/lib/rules/require-default-props.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,6 +1781,29 @@ ruleTester.run('require-default-props', rule, {
17811781
column: 5
17821782
}
17831783
]
1784+
},
1785+
{
1786+
code: [
1787+
'class Hello extends React.Component {',
1788+
' static get propTypes() {',
1789+
' return {',
1790+
' name: PropTypes.string',
1791+
' };',
1792+
' }',
1793+
' static defaultProps() {',
1794+
' return {',
1795+
' name: \'John\'',
1796+
' };',
1797+
' }',
1798+
' render() {',
1799+
' return <div>Hello {this.props.name}</div>;',
1800+
' }',
1801+
'}'
1802+
].join('\n'),
1803+
parser: 'babel-eslint',
1804+
errors: [{
1805+
message: 'propType "name" is not required, but has no corresponding defaultProp declaration.'
1806+
}]
17841807
}
17851808
]
17861809
});

0 commit comments

Comments
 (0)