File tree Expand file tree Collapse file tree 4 files changed +57
-2
lines changed Expand file tree Collapse file tree 4 files changed +57
-2
lines changed Original file line number Diff line number Diff line change @@ -37,6 +37,17 @@ module.exports = function(context) {
37
37
* @returns {Boolean } True if we are declaring a display name, false if not.
38
38
*/
39
39
function isDisplayNameDeclaration ( node ) {
40
+
41
+ // Special case for class properties
42
+ // (babel-eslint does not expose property name so we have to rely on tokens)
43
+ if ( node . type === 'ClassProperty' ) {
44
+ var tokens = context . getFirstTokens ( node , 2 ) ;
45
+ if ( tokens [ 0 ] . value === 'displayName' || tokens [ 1 ] . value === 'displayName' ) {
46
+ return true ;
47
+ }
48
+ return false ;
49
+ }
50
+
40
51
return Boolean (
41
52
node &&
42
53
node . name === 'displayName'
@@ -72,6 +83,14 @@ module.exports = function(context) {
72
83
73
84
return {
74
85
86
+ ClassProperty : function ( node ) {
87
+ if ( ! isDisplayNameDeclaration ( node ) ) {
88
+ return ;
89
+ }
90
+
91
+ markDisplayNameAsDeclared ( node ) ;
92
+ } ,
93
+
75
94
MemberExpression : function ( node ) {
76
95
if ( ! isDisplayNameDeclaration ( node . property ) ) {
77
96
return ;
Original file line number Diff line number Diff line change @@ -42,10 +42,22 @@ module.exports = function(context) {
42
42
* @returns {Boolean } True if we are declaring a prop, false if not.
43
43
*/
44
44
function isPropTypesDeclaration ( node ) {
45
+
46
+ // Special case for class properties
47
+ // (babel-eslint does not expose property name so we have to rely on tokens)
48
+ if ( node . type === 'ClassProperty' ) {
49
+ var tokens = context . getFirstTokens ( node , 2 ) ;
50
+ if ( tokens [ 0 ] . value === 'propTypes' || tokens [ 1 ] . value === 'propTypes' ) {
51
+ return true ;
52
+ }
53
+ return false ;
54
+ }
55
+
45
56
return Boolean (
46
57
node &&
47
58
node . name === 'propTypes'
48
59
) ;
60
+
49
61
}
50
62
51
63
/**
@@ -198,6 +210,14 @@ module.exports = function(context) {
198
210
199
211
return {
200
212
213
+ ClassProperty : function ( node ) {
214
+ if ( ! isPropTypesDeclaration ( node ) ) {
215
+ return ;
216
+ }
217
+
218
+ markPropTypesAsDeclared ( node , node . value ) ;
219
+ } ,
220
+
201
221
MemberExpression : function ( node ) {
202
222
var type ;
203
223
if ( isPropTypesUsage ( node ) ) {
Original file line number Diff line number Diff line change 11
11
var eslint = require ( 'eslint' ) . linter ;
12
12
var ESLintTester = require ( 'eslint-tester' ) ;
13
13
14
+ require ( 'babel-eslint' ) ;
15
+
14
16
// ------------------------------------------------------------------------------
15
17
// Tests
16
18
// ------------------------------------------------------------------------------
@@ -70,6 +72,20 @@ eslintTester.addRuleTest('lib/rules/display-name', {
70
72
classes : true ,
71
73
jsx : true
72
74
}
75
+ } , {
76
+ code : [
77
+ 'class Hello extends React.Component {' ,
78
+ ' static displayName = \'Widget\'' ,
79
+ ' render() {' ,
80
+ ' return <div>Hello {this.props.name}</div>;' ,
81
+ ' }' ,
82
+ '}'
83
+ ] . join ( '\n' ) ,
84
+ parser : 'babel-eslint' ,
85
+ ecmaFeatures : {
86
+ classes : true ,
87
+ jsx : true
88
+ }
73
89
} ] ,
74
90
75
91
invalid : [ {
Original file line number Diff line number Diff line change @@ -203,7 +203,7 @@ eslintTester.addRuleTest('lib/rules/prop-types', {
203
203
destructuring : true ,
204
204
jsx : true
205
205
}
206
- } /* , {
206
+ } , {
207
207
code : [
208
208
'class Hello extends React.Component {' ,
209
209
' static propTypes = {' ,
@@ -220,7 +220,7 @@ eslintTester.addRuleTest('lib/rules/prop-types', {
220
220
destructuring : true ,
221
221
jsx : true
222
222
}
223
- }*/
223
+ }
224
224
] ,
225
225
226
226
invalid : [
You can’t perform that action at this time.
0 commit comments