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) {
3737 * @returns {Boolean } True if we are declaring a display name, false if not.
3838 */
3939 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+
4051 return Boolean (
4152 node &&
4253 node . name === 'displayName'
@@ -72,6 +83,14 @@ module.exports = function(context) {
7283
7384 return {
7485
86+ ClassProperty : function ( node ) {
87+ if ( ! isDisplayNameDeclaration ( node ) ) {
88+ return ;
89+ }
90+
91+ markDisplayNameAsDeclared ( node ) ;
92+ } ,
93+
7594 MemberExpression : function ( node ) {
7695 if ( ! isDisplayNameDeclaration ( node . property ) ) {
7796 return ;
Original file line number Diff line number Diff line change @@ -42,10 +42,22 @@ module.exports = function(context) {
4242 * @returns {Boolean } True if we are declaring a prop, false if not.
4343 */
4444 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+
4556 return Boolean (
4657 node &&
4758 node . name === 'propTypes'
4859 ) ;
60+
4961 }
5062
5163 /**
@@ -198,6 +210,14 @@ module.exports = function(context) {
198210
199211 return {
200212
213+ ClassProperty : function ( node ) {
214+ if ( ! isPropTypesDeclaration ( node ) ) {
215+ return ;
216+ }
217+
218+ markPropTypesAsDeclared ( node , node . value ) ;
219+ } ,
220+
201221 MemberExpression : function ( node ) {
202222 var type ;
203223 if ( isPropTypesUsage ( node ) ) {
Original file line number Diff line number Diff line change 1111var eslint = require ( 'eslint' ) . linter ;
1212var ESLintTester = require ( 'eslint-tester' ) ;
1313
14+ require ( 'babel-eslint' ) ;
15+
1416// ------------------------------------------------------------------------------
1517// Tests
1618// ------------------------------------------------------------------------------
@@ -70,6 +72,20 @@ eslintTester.addRuleTest('lib/rules/display-name', {
7072 classes : true ,
7173 jsx : true
7274 }
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+ }
7389 } ] ,
7490
7591 invalid : [ {
Original file line number Diff line number Diff line change @@ -203,7 +203,7 @@ eslintTester.addRuleTest('lib/rules/prop-types', {
203203 destructuring : true ,
204204 jsx : true
205205 }
206- } /* , {
206+ } , {
207207 code : [
208208 'class Hello extends React.Component {' ,
209209 ' static propTypes = {' ,
@@ -220,7 +220,7 @@ eslintTester.addRuleTest('lib/rules/prop-types', {
220220 destructuring : true ,
221221 jsx : true
222222 }
223- }*/
223+ }
224224 ] ,
225225
226226 invalid : [
You can’t perform that action at this time.
0 commit comments