@@ -11,6 +11,7 @@ const Components = require('../util/Components');
11
11
const astUtil = require ( '../util/ast' ) ;
12
12
const docsUrl = require ( '../util/docsUrl' ) ;
13
13
const lifecycleMethods = require ( '../util/lifecycleMethods' ) ;
14
+ const report = require ( '../util/report' ) ;
14
15
15
16
function getText ( node ) {
16
17
const params = node . value . params . map ( ( p ) => p . name ) ;
@@ -26,6 +27,10 @@ function getText(node) {
26
27
return null ;
27
28
}
28
29
30
+ const messages = {
31
+ lifecycle : '{{propertyName}} is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' ,
32
+ } ;
33
+
29
34
module . exports = {
30
35
meta : {
31
36
docs : {
@@ -34,6 +39,7 @@ module.exports = {
34
39
recommended : false ,
35
40
url : docsUrl ( 'no-arrow-function-lifecycle' ) ,
36
41
} ,
42
+ messages,
37
43
schema : [ ] ,
38
44
fixable : 'code' ,
39
45
} ,
@@ -95,26 +101,30 @@ module.exports = {
95
101
( previousComment . length > 0 ? previousComment [ 0 ] : body ) . range [ 0 ] ,
96
102
] ;
97
103
98
- context . report ( {
99
- node,
100
- message : '{{propertyName}} is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' ,
101
- data : {
102
- propertyName,
103
- } ,
104
- fix ( fixer ) {
105
- if ( ! sourceCode . getCommentsAfter ) {
106
- // eslint 3.x
107
- return isBlockBody && fixer . replaceTextRange ( headRange , getText ( node ) ) ;
108
- }
109
- return [ ] . concat (
110
- fixer . replaceTextRange ( headRange , getText ( node ) ) ,
111
- isBlockBody ? [ ] : fixer . replaceTextRange (
112
- bodyRange ,
113
- `{ return ${ previousComment . map ( ( x ) => sourceCode . getText ( x ) ) . join ( '' ) } ${ sourceCode . getText ( body ) } ${ nextComment . map ( ( x ) => sourceCode . getText ( x ) ) . join ( '' ) } ; }`
114
- )
115
- ) ;
116
- } ,
117
- } ) ;
104
+ report (
105
+ context ,
106
+ messages . lifecycle ,
107
+ 'lifecycle' ,
108
+ {
109
+ node,
110
+ data : {
111
+ propertyName,
112
+ } ,
113
+ fix ( fixer ) {
114
+ if ( ! sourceCode . getCommentsAfter ) {
115
+ // eslint 3.x
116
+ return isBlockBody && fixer . replaceTextRange ( headRange , getText ( node ) ) ;
117
+ }
118
+ return [ ] . concat (
119
+ fixer . replaceTextRange ( headRange , getText ( node ) ) ,
120
+ isBlockBody ? [ ] : fixer . replaceTextRange (
121
+ bodyRange ,
122
+ `{ return ${ previousComment . map ( ( x ) => sourceCode . getText ( x ) ) . join ( '' ) } ${ sourceCode . getText ( body ) } ${ nextComment . map ( ( x ) => sourceCode . getText ( x ) ) . join ( '' ) } ; }`
123
+ )
124
+ ) ;
125
+ } ,
126
+ }
127
+ ) ;
118
128
}
119
129
} ) ;
120
130
}
0 commit comments