2
2
* @fileoverview Prevent usage of deprecated methods
3
3
* @author Yannick Croissant
4
4
* @author Scott Feeney
5
+ * @author Sergei Startsev
5
6
*/
6
7
'use strict' ;
7
8
8
9
const has = require ( 'has' ) ;
9
10
11
+ const Components = require ( '../util/Components' ) ;
12
+ const astUtil = require ( '../util/ast' ) ;
13
+ const docsUrl = require ( '../util/docsUrl' ) ;
10
14
const pragmaUtil = require ( '../util/pragma' ) ;
11
15
const versionUtil = require ( '../util/version' ) ;
12
- const docsUrl = require ( '../util/docsUrl' ) ;
13
16
14
17
// ------------------------------------------------------------------------------
15
18
// Constants
@@ -20,7 +23,7 @@ const MODULES = {
20
23
'react-addons-perf' : [ 'ReactPerf' , 'Perf' ]
21
24
} ;
22
25
23
- const DEPRECATED_MESSAGE = '{{oldMethod}} is deprecated since React {{version}}{{newMethod}}' ;
26
+ const DEPRECATED_MESSAGE = '{{oldMethod}} is deprecated since React {{version}}{{newMethod}}{{refs}} ' ;
24
27
25
28
// ------------------------------------------------------------------------------
26
29
// Rule Definition
@@ -37,7 +40,7 @@ module.exports = {
37
40
schema : [ ]
38
41
} ,
39
42
40
- create : function ( context ) {
43
+ create : Components . detect ( ( context , components , utils ) => {
41
44
const sourceCode = context . getSourceCode ( ) ;
42
45
const pragma = pragmaUtil . getFromContext ( context ) ;
43
46
@@ -73,6 +76,22 @@ module.exports = {
73
76
deprecated [ `${ pragma } .PropTypes` ] = [ '15.5.0' , 'the npm module prop-types' ] ;
74
77
// 15.6.0
75
78
deprecated [ `${ pragma } .DOM` ] = [ '15.6.0' , 'the npm module react-dom-factories' ] ;
79
+ // 16.3.0
80
+ deprecated . componentWillMount = [
81
+ '16.3.0' ,
82
+ 'UNSAFE_componentWillMount' ,
83
+ 'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount'
84
+ ] ;
85
+ deprecated . componentWillReceiveProps = [
86
+ '16.3.0' ,
87
+ 'UNSAFE_componentWillReceiveProps' ,
88
+ 'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops'
89
+ ] ;
90
+ deprecated . componentWillUpdate = [
91
+ '16.3.0' ,
92
+ 'UNSAFE_componentWillUpdate' ,
93
+ 'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate'
94
+ ] ;
76
95
return deprecated ;
77
96
}
78
97
@@ -91,13 +110,17 @@ module.exports = {
91
110
return ;
92
111
}
93
112
const deprecated = getDeprecated ( ) ;
113
+ const version = deprecated [ method ] [ 0 ] ;
114
+ const newMethod = deprecated [ method ] [ 1 ] ;
115
+ const refs = deprecated [ method ] [ 2 ] ;
94
116
context . report ( {
95
117
node : node ,
96
118
message : DEPRECATED_MESSAGE ,
97
119
data : {
98
120
oldMethod : method ,
99
- version : deprecated [ method ] [ 0 ] ,
100
- newMethod : deprecated [ method ] [ 1 ] ? `, use ${ deprecated [ method ] [ 1 ] } instead` : ''
121
+ version,
122
+ newMethod : newMethod ? `, use ${ newMethod } instead` : '' ,
123
+ refs : refs ? `, see ${ refs } ` : ''
101
124
}
102
125
} ) ;
103
126
}
@@ -119,6 +142,27 @@ module.exports = {
119
142
return moduleName ;
120
143
}
121
144
145
+ /**
146
+ * Returns life cycle methods if available
147
+ * @param {ASTNode } node The AST node being checked.
148
+ * @returns {Array } The array of methods.
149
+ */
150
+ function getLifeCycleMethods ( node ) {
151
+ const properties = astUtil . getComponentProperties ( node ) ;
152
+ return properties . map ( property => astUtil . getPropertyName ( property ) ) ;
153
+ }
154
+
155
+ /**
156
+ * Checks life cycle methods
157
+ * @param {ASTNode } node The AST node being checked.
158
+ */
159
+ function checkLifeCycleMethods ( node ) {
160
+ if ( utils . isES5Component ( node ) || utils . isES6Component ( node ) ) {
161
+ const methods = getLifeCycleMethods ( node ) ;
162
+ methods . forEach ( method => checkDeprecation ( node , method ) ) ;
163
+ }
164
+ }
165
+
122
166
// --------------------------------------------------------------------------
123
167
// Public
124
168
// --------------------------------------------------------------------------
@@ -145,10 +189,10 @@ module.exports = {
145
189
VariableDeclarator : function ( node ) {
146
190
const reactModuleName = getReactModuleName ( node ) ;
147
191
const isRequire = node . init && node . init . callee && node . init . callee . name === 'require' ;
148
- const isReactRequire =
149
- node . init && node . init . arguments &&
150
- node . init . arguments . length && typeof MODULES [ node . init . arguments [ 0 ] . value ] !== 'undefined'
151
- ;
192
+ const isReactRequire = node . init
193
+ && node . init . arguments
194
+ && node . init . arguments . length
195
+ && typeof MODULES [ node . init . arguments [ 0 ] . value ] !== 'undefined' ;
152
196
const isDestructuring = node . id && node . id . type === 'ObjectPattern' ;
153
197
154
198
if (
@@ -160,8 +204,11 @@ module.exports = {
160
204
node . id . properties . forEach ( property => {
161
205
checkDeprecation ( node , `${ reactModuleName || pragma } .${ property . key . name } ` ) ;
162
206
} ) ;
163
- }
207
+ } ,
164
208
209
+ ClassDeclaration : checkLifeCycleMethods ,
210
+ ClassExpression : checkLifeCycleMethods ,
211
+ ObjectExpression : checkLifeCycleMethods
165
212
} ;
166
- }
213
+ } )
167
214
} ;
0 commit comments