File tree Expand file tree Collapse file tree 5 files changed +131
-7
lines changed Expand file tree Collapse file tree 5 files changed +131
-7
lines changed Original file line number Diff line number Diff line change @@ -44,21 +44,23 @@ Finally, enable all of the rules that you would like to use.
44
44
{
45
45
"rules" : {
46
46
"react/no-multi-comp" : 1 ,
47
- "react/prop-types" : 1
47
+ "react/prop-types" : 1 ,
48
+ "react/display-name" : 1
48
49
}
49
50
}
50
51
```
51
52
52
53
# List of supported rules
53
54
54
- * no-multi-comp: Prevent multiple component definition per file
55
- * prop-types: Prevent missing propTypes in a React component definition
55
+ * [ no-multi-comp] ( docs/rules/no-multi-comp.md ) : Prevent multiple component definition per file
56
+ * [ prop-types] ( docs/rules/prop-types.md ) : Prevent missing propTypes in a React component definition
57
+ * [ display-name] ( docs/rules/display-name.md ) : Prevent missing displayName in a React component definition
56
58
57
- ## Not supported yet
59
+ ## To Do
58
60
59
- * display-name: Prevent missing displayName in a React component definition
60
61
* no-deprecated: Prevent usage of deprecated methods ([ React 0.12 Updated API] ( http://facebook.github.io/react/blog/2014/10/28/react-v0.12.html#new-terminology-amp-updated-apis ) )
61
62
* no-classic: Prevent usage of "classic" methods ([ #2700 ] ( https://github.com/facebook/react/pull/2700 ) )
63
+ * [ Implement rules from David Chang's React Style Guide] ( https://reactjsnews.com/react-style-guide-patterns-i-like )
62
64
63
65
[ Any rule idea is welcome !] ( https://github.com/yannickcr/eslint-plugin-react/issues )
64
66
Original file line number Diff line number Diff line change
1
+ # Prevent missing displayName in a React component definition (display-name)
2
+
3
+ DisplayName allows you to name your component. This name is used by React in debugging messages.
4
+
5
+ ## Rule Details
6
+
7
+ The following patterns are considered warnings:
8
+
9
+ ``` js
10
+ var Hello = React .createClass ({
11
+ render : function () {
12
+ return < div> Hello {this .props .name }< / div> ;
13
+ }
14
+ });
15
+ ```
16
+
17
+ The following patterns are not warnings:
18
+
19
+ ``` js
20
+ var Hello = React .createClass ({
21
+ displayName: ' Hello' ,
22
+ render : function () {
23
+ return < div> Hello {this .props .name }< / div> ;
24
+ }
25
+ });
26
+ ```
27
+
28
+ ## When Not To Use It
29
+
30
+ If you are using JSX this value is already automatically set and it is safe for you to disable this rule.
Original file line number Diff line number Diff line change 3
3
module . exports = {
4
4
rules : {
5
5
'no-multi-comp' : require ( './lib/rules/no-multi-comp' ) ,
6
- 'prop-types' : require ( './lib/rules/prop-types' )
6
+ 'prop-types' : require ( './lib/rules/prop-types' ) ,
7
+ 'display-name' : require ( './lib/rules/display-name' )
7
8
} ,
8
9
rulesConfig : {
9
10
'no-multi-comp' : 0 ,
10
- 'prop-types' : 0
11
+ 'prop-types' : 0 ,
12
+ 'display-name' : 0
11
13
}
12
14
} ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @fileoverview Prevent missing displayName in a React component definition
3
+ * @author Yannick Croissant
4
+ */
5
+ 'use strict' ;
6
+
7
+ // ------------------------------------------------------------------------------
8
+ // Rule Definition
9
+ // ------------------------------------------------------------------------------
10
+
11
+ module . exports = function ( context ) {
12
+
13
+ var hasDisplayName = false ;
14
+
15
+ // --------------------------------------------------------------------------
16
+ // Public
17
+ // --------------------------------------------------------------------------
18
+
19
+ return {
20
+
21
+ 'ObjectExpression' : function ( node ) {
22
+
23
+ if ( ! node . parent . callee || node . parent . callee . object . name !== 'React' || node . parent . callee . property . name !== 'createClass' ) {
24
+ return ;
25
+ }
26
+
27
+ node . properties . forEach ( function ( property ) {
28
+ var keyName = property . key . name || property . key . value ;
29
+ if ( keyName === 'displayName' ) {
30
+ hasDisplayName = true ;
31
+ }
32
+ } ) ;
33
+
34
+ } ,
35
+
36
+ 'ObjectExpression:exit' : function ( node ) {
37
+ if ( ! node . parent . callee || node . parent . callee . object . name !== 'React' || node . parent . callee . property . name !== 'createClass' ) {
38
+ return ;
39
+ }
40
+ if ( ! hasDisplayName ) {
41
+ context . report ( node , 'Component definition is missing display name' ) ;
42
+ }
43
+ hasDisplayName = false ;
44
+ }
45
+ } ;
46
+
47
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @fileoverview Prevent missing displayName in a React component definition
3
+ * @author Yannick Croissant
4
+ */
5
+ 'use strict' ;
6
+
7
+ // ------------------------------------------------------------------------------
8
+ // Requirements
9
+ // ------------------------------------------------------------------------------
10
+
11
+ var eslint = require ( 'eslint' ) . linter ;
12
+ var ESLintTester = require ( 'eslint-tester' ) ;
13
+
14
+ // ------------------------------------------------------------------------------
15
+ // Tests
16
+ // ------------------------------------------------------------------------------
17
+
18
+ var eslintTester = new ESLintTester ( eslint ) ;
19
+ eslintTester . addRuleTest ( 'lib/rules/display-name' , {
20
+
21
+ valid : [
22
+ {
23
+ code : 'var Hello = React.createClass({displayName: \'Hello\',render: function() {return <div>Hello {this.props.name}</div>;}});' ,
24
+ settings : {
25
+ ecmascript : 6 ,
26
+ jsx : true
27
+ }
28
+ }
29
+ ] ,
30
+
31
+ invalid : [
32
+ {
33
+ code : 'var Hello = React.createClass({render: function() {return <div>Hello {this.props.name}</div>;}});' ,
34
+ settings : {
35
+ ecmascript : 6 ,
36
+ jsx : true
37
+ } ,
38
+ errors : [ {
39
+ message : 'Component definition is missing display name'
40
+ } ]
41
+ }
42
+ ]
43
+ } ) ;
You can’t perform that action at this time.
0 commit comments