@@ -11,6 +11,7 @@ const Components = require('../util/Components');
1111const astUtil = require ( '../util/ast' ) ;
1212const docsUrl = require ( '../util/docsUrl' ) ;
1313const lifecycleMethods = require ( '../util/lifecycleMethods' ) ;
14+ const report = require ( '../util/report' ) ;
1415
1516function getText ( node ) {
1617 const params = node . value . params . map ( ( p ) => p . name ) ;
@@ -26,6 +27,10 @@ function getText(node) {
2627 return null ;
2728}
2829
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+
2934module . exports = {
3035 meta : {
3136 docs : {
@@ -34,6 +39,7 @@ module.exports = {
3439 recommended : false ,
3540 url : docsUrl ( 'no-arrow-function-lifecycle' ) ,
3641 } ,
42+ messages,
3743 schema : [ ] ,
3844 fixable : 'code' ,
3945 } ,
@@ -95,26 +101,30 @@ module.exports = {
95101 ( previousComment . length > 0 ? previousComment [ 0 ] : body ) . range [ 0 ] ,
96102 ] ;
97103
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+ ) ;
118128 }
119129 } ) ;
120130 }
0 commit comments