Skip to content

Commit ac42603

Browse files
committed
Add support for static methods in display-name and prop-types (fixes #48)
1 parent 6a63a6c commit ac42603

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

lib/rules/display-name.js

100644100755
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ module.exports = function(context) {
8383
markDisplayNameAsDeclared(component.node);
8484
},
8585

86+
MethodDefinition: function(node) {
87+
if (!isDisplayNameDeclaration(node.key)) {
88+
return;
89+
}
90+
markDisplayNameAsDeclared(node);
91+
},
92+
8693
ObjectExpression: function(node) {
8794
// Search for the displayName declaration
8895
node.properties.forEach(function(property) {

lib/rules/prop-types.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,21 @@ module.exports = function(context) {
220220
}
221221
},
222222

223+
MethodDefinition: function(node) {
224+
if (!isPropTypesDeclaration(node.key)) {
225+
return;
226+
}
227+
228+
var i = node.value.body.body.length - 1;
229+
for (; i >= 0; i--) {
230+
if (node.value.body.body[i].type === 'ReturnStatement') {
231+
break;
232+
}
233+
}
234+
235+
markPropTypesAsDeclared(node, node.value.body.body[i].argument);
236+
},
237+
223238
ObjectExpression: function(node) {
224239
// Search for the displayName declaration
225240
node.properties.forEach(function(property) {

tests/lib/rules/display-name.js

100644100755
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@ eslintTester.addRuleTest('lib/rules/display-name', {
5555
classes: true,
5656
jsx: true
5757
}
58+
}, {
59+
code: [
60+
'class Hello extends React.Component {',
61+
' static get displayName() {',
62+
' return \'Hello\';',
63+
' }',
64+
' render() {',
65+
' return <div>Hello {this.props.name}</div>;',
66+
' }',
67+
'}'
68+
].join('\n'),
69+
ecmaFeatures: {
70+
classes: true,
71+
jsx: true
72+
}
5873
}],
5974

6075
invalid: [{

tests/lib/rules/prop-types.js

100644100755
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,59 @@ eslintTester.addRuleTest('lib/rules/prop-types', {
165165
classes: true,
166166
jsx: true
167167
}
168+
}, {
169+
code: [
170+
'class Hello extends React.Component {',
171+
' static get propTypes() {',
172+
' return {',
173+
' name: React.PropTypes.string',
174+
' };',
175+
' }',
176+
' render() {',
177+
' return <div>Hello {this.props.name}</div>;',
178+
' }',
179+
'}'
180+
].join('\n'),
181+
ecmaFeatures: {
182+
classes: true,
183+
destructuring: true,
184+
jsx: true
185+
}
186+
/* }, {
187+
code: [
188+
'class Hello extends React.Component {',
189+
' render() {',
190+
' var { firstname, ...other } = this.props;',
191+
' return <div>Hello {firstname}</div>;',
192+
' }',
193+
'}',
194+
'Hello.propTypes = {',
195+
' firstname: React.PropTypes.string',
196+
'};'
197+
].join('\n'),
198+
parser: 'babel-eslint',
199+
ecmaFeatures: {
200+
classes: true,
201+
destructuring: true,
202+
jsx: true
203+
}
204+
}, {
205+
code: [
206+
'class Hello extends React.Component {',
207+
' static propTypes = {',
208+
' name: React.PropTypes.string',
209+
' }',
210+
' render() {',
211+
' return <div>Hello {this.props.name}</div>;',
212+
' }',
213+
'}'
214+
].join('\n'),
215+
parser: 'babel-eslint',
216+
ecmaFeatures: {
217+
classes: true,
218+
destructuring: true,
219+
jsx: true
220+
}*/
168221
}
169222
],
170223

0 commit comments

Comments
 (0)