Skip to content

Commit 5ab7a23

Browse files
sergei-startsevljharb
authored andcommitted
Adjusted no-deprecated rule for React 16.3.0
Added warnings for componentWillMount, componentWillReceiveProps, componentWillUpdate
1 parent 8973758 commit 5ab7a23

File tree

4 files changed

+372
-141
lines changed

4 files changed

+372
-141
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ Enable the rules that you would like to use.
102102
* [react/no-children-prop](docs/rules/no-children-prop.md): Prevent passing children as props
103103
* [react/no-danger](docs/rules/no-danger.md): Prevent usage of dangerous JSX properties
104104
* [react/no-danger-with-children](docs/rules/no-danger-with-children.md): Prevent problem with children and props.dangerouslySetInnerHTML
105-
* [react/no-deprecated](docs/rules/no-deprecated.md): Prevent usage of deprecated methods
105+
* [react/no-deprecated](docs/rules/no-deprecated.md): Prevent usage of deprecated methods, including component lifecyle methods
106106
* [react/no-did-mount-set-state](docs/rules/no-did-mount-set-state.md): Prevent usage of `setState` in `componentDidMount`
107107
* [react/no-did-update-set-state](docs/rules/no-did-update-set-state.md): Prevent usage of `setState` in `componentDidUpdate`
108108
* [react/no-direct-mutation-state](docs/rules/no-direct-mutation-state.md): Prevent direct mutation of `this.state`

docs/rules/no-deprecated.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,27 @@ const propTypes = {
2727
React.DOM.div();
2828

2929
import React, { PropTypes } from 'react';
30+
31+
class Foo extends React.Component {
32+
componentWillMount() { }
33+
componentWillReceiveProps() { }
34+
componentWillUpdate() { }
35+
// ...
36+
}
37+
38+
class Foo extends React.PureComponent {
39+
componentWillMount() { }
40+
componentWillReceiveProps() { }
41+
componentWillUpdate() { }
42+
// ...
43+
}
44+
45+
var Foo = createReactClass({
46+
componentWillMount: function() {},
47+
componentWillReceiveProps: function() {},
48+
componentWillUpdate: function() {},
49+
// ...
50+
})
3051
```
3152

3253
The following patterns are **not** considered warnings:
@@ -38,4 +59,10 @@ ReactDOM.render(<MyComponent />, root);
3859
ReactDOM.findDOMNode(this.refs.foo);
3960

4061
import { PropTypes } from 'prop-types';
62+
63+
class Foo {
64+
componentWillMount() { }
65+
componentWillReceiveProps() { }
66+
componentWillUpdate() { }
67+
}
4168
```

lib/rules/no-deprecated.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
* @fileoverview Prevent usage of deprecated methods
33
* @author Yannick Croissant
44
* @author Scott Feeney
5+
* @author Sergei Startsev
56
*/
67
'use strict';
78

89
const has = require('has');
910

11+
const Components = require('../util/Components');
12+
const astUtil = require('../util/ast');
13+
const docsUrl = require('../util/docsUrl');
1014
const pragmaUtil = require('../util/pragma');
1115
const versionUtil = require('../util/version');
12-
const docsUrl = require('../util/docsUrl');
1316

1417
// ------------------------------------------------------------------------------
1518
// Constants
@@ -37,7 +40,7 @@ module.exports = {
3740
schema: []
3841
},
3942

40-
create: function(context) {
43+
create: Components.detect((context, components, utils) => {
4144
const sourceCode = context.getSourceCode();
4245
const pragma = pragmaUtil.getFromContext(context);
4346

@@ -73,6 +76,10 @@ module.exports = {
7376
deprecated[`${pragma}.PropTypes`] = ['15.5.0', 'the npm module prop-types'];
7477
// 15.6.0
7578
deprecated[`${pragma}.DOM`] = ['15.6.0', 'the npm module react-dom-factories'];
79+
// 16.3.0
80+
deprecated.componentWillMount = ['16.3.0'];
81+
deprecated.componentWillReceiveProps = ['16.3.0'];
82+
deprecated.componentWillUpdate = ['16.3.0'];
7683
return deprecated;
7784
}
7885

@@ -119,6 +126,27 @@ module.exports = {
119126
return moduleName;
120127
}
121128

129+
/**
130+
* Returns life cycle methods if available
131+
* @param {ASTNode} node The AST node being checked.
132+
* @returns {Array} The array of methods.
133+
*/
134+
function getLifeCycleMethods(node) {
135+
const properties = astUtil.getComponentProperties(node);
136+
return properties.map(property => astUtil.getPropertyName(property));
137+
}
138+
139+
/**
140+
* Checks life cycle methods
141+
* @param {ASTNode} node The AST node being checked.
142+
*/
143+
function checkLifeCycleMethods(node) {
144+
if (utils.isES5Component(node) || utils.isES6Component(node)) {
145+
const methods = getLifeCycleMethods(node);
146+
methods.forEach(method => checkDeprecation(node, method));
147+
}
148+
}
149+
122150
// --------------------------------------------------------------------------
123151
// Public
124152
// --------------------------------------------------------------------------
@@ -160,8 +188,11 @@ module.exports = {
160188
node.id.properties.forEach(property => {
161189
checkDeprecation(node, `${reactModuleName || pragma}.${property.key.name}`);
162190
});
163-
}
191+
},
164192

193+
ClassDeclaration: checkLifeCycleMethods,
194+
ClassExpression: checkLifeCycleMethods,
195+
ObjectExpression: checkLifeCycleMethods
165196
};
166-
}
197+
})
167198
};

0 commit comments

Comments
 (0)