-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
230 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Prevent usage of deprecated methods (no-deprecated) | ||
|
||
Several methods are deprecated between React versions. This rule will warn you if you try to use a deprecated method. | ||
|
||
## Rule Details | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
React.render(<MyComponent />, root); | ||
|
||
React.unmountComponentAtNode(root); | ||
|
||
React.findDOMNode(this.refs.foo); | ||
|
||
React.renderToString(<MyComponent />); | ||
|
||
React.renderToStaticMarkup(<MyComponent />); | ||
``` | ||
|
||
The following patterns are not considered warnings: | ||
|
||
```js | ||
ReactDOM.render(<MyComponent />, root); | ||
|
||
// When [1, {"react": "0.13.0"}] | ||
ReactDOM.findDOMNode(this.refs.foo); | ||
``` | ||
|
||
## Rule Options | ||
|
||
By default this rule will warn to every methods marked as deprecated. You can limit it to an older React version if you are not using the latest one: | ||
|
||
```js | ||
"rules": { | ||
"react/no-deprecated": [1, {"react": "0.12.0"}] // Will warn for every deprecated methods in React 0.12.0 and below | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* @fileoverview Prevent usage of deprecated methods | ||
* @author Yannick Croissant | ||
* @author Scott Feeney | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Constants | ||
// ------------------------------------------------------------------------------ | ||
|
||
var DEPRECATED_MESSAGE = '{{oldMethod}} is deprecated since React {{version}}{{newMethod}}'; | ||
|
||
var DEPRECATED = { | ||
MemberExpression: { | ||
// 0.12.0 | ||
'React.renderComponent': ['0.12.0', 'React.render'], | ||
'React.renderComponentToString': ['0.12.0', 'React.renderToString'], | ||
'React.renderComponentToStaticMarkup': ['0.12.0', 'React.renderToStaticMarkup'], | ||
'React.isValidComponent': ['0.12.0', 'React.isValidElement'], | ||
'React.PropTypes.component': ['0.12.0', 'React.PropTypes.element'], | ||
'React.PropTypes.renderable': ['0.12.0', 'React.PropTypes.node'], | ||
'React.isValidClass': ['0.12.0'], | ||
'this.transferPropsTo': ['0.12.0', 'spread operator ({...})'], | ||
// 0.13.0 | ||
'React.addons.classSet': ['0.13.0', 'the npm module classnames'], | ||
'React.addons.cloneWithProps': ['0.13.0', 'React.cloneElement'], | ||
// 0.14.0 | ||
'React.render': ['0.14.0', 'ReactDOM.render'], | ||
'React.unmountComponentAtNode': ['0.14.0', 'ReactDOM.unmountComponentAtNode'], | ||
'React.findDOMNode': ['0.14.0', 'ReactDOM.findDOMNode'], | ||
'React.renderToString': ['0.14.0', 'ReactDOMServer.renderToString'], | ||
'React.renderToStaticMarkup': ['0.14.0', 'ReactDOMServer.renderToStaticMarkup'] | ||
} | ||
}; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = function(context) { | ||
|
||
// Validate React version passed in options | ||
// (append the patch version if missing, allowing shorthands like 0.12 for React 0.12.0) | ||
var optVer = context.options[0] ? context.options[0].react : '999.999.999'; | ||
optVer = /^[0-9]+\.[0-9]+$/.test(optVer) ? optVer + '.0' : optVer; | ||
optVer = optVer.split('.').map(function(part) { | ||
return Number(part); | ||
}); | ||
|
||
function checkVersion(methodVer) { | ||
methodVer = methodVer.split('.').map(function(part) { | ||
return Number(part); | ||
}); | ||
var higherMajor = methodVer[0] < optVer[0]; | ||
var higherMinor = methodVer[0] === optVer[0] && methodVer[1] < optVer[1]; | ||
var higherOrEqualPatch = methodVer[0] === optVer[0] && methodVer[1] === optVer[1] && methodVer[2] <= optVer[2]; | ||
|
||
return higherMajor || higherMinor || higherOrEqualPatch; | ||
} | ||
|
||
function isDeprecated(type, method) { | ||
return ( | ||
DEPRECATED[type] && | ||
DEPRECATED[type][method] && | ||
checkVersion(DEPRECATED[type][method][0]) | ||
); | ||
} | ||
|
||
// -------------------------------------------------------------------------- | ||
// Public | ||
// -------------------------------------------------------------------------- | ||
|
||
return { | ||
|
||
MemberExpression: function(node) { | ||
var method = context.getSource(node); | ||
if (!isDeprecated(node.type, method)) { | ||
return; | ||
} | ||
context.report(node, DEPRECATED_MESSAGE, { | ||
oldMethod: method, | ||
version: DEPRECATED[node.type][method][0], | ||
newMethod: DEPRECATED[node.type][method][1] ? ', use ' + DEPRECATED[node.type][method][1] + ' instead' : '' | ||
}); | ||
} | ||
|
||
}; | ||
|
||
}; | ||
|
||
module.exports.schema = [{ | ||
type: 'object', | ||
properties: { | ||
react: { | ||
type: 'string', | ||
pattern: '^[0-9]+\.[0-9]+(\.[0-9]+)?$' | ||
} | ||
}, | ||
additionalProperties: false | ||
}]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/** | ||
* @fileoverview Prevent usage of deprecated methods | ||
* @author Yannick Croissant | ||
* @author Scott Feeney | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require('../../../lib/rules/no-deprecated'); | ||
var RuleTester = require('eslint').RuleTester; | ||
|
||
require('babel-eslint'); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester(); | ||
ruleTester.run('no-deprecated', rule, { | ||
|
||
valid: [ | ||
// Not deprecated | ||
'var MyClass = React.createClass({});', | ||
'var element = React.createElement(\'p\', {}, null);', | ||
'var clone = React.cloneElement(element);', | ||
'ReactDOM.render(element, container);', | ||
'ReactDOM.unmountComponentAtNode(container);', | ||
'ReactDOM.findDOMNode(instance);', | ||
'ReactDOMServer.renderToString(element);', | ||
'ReactDOMServer.renderToStaticMarkup(element);', | ||
// Deprecated in a later version | ||
{code: 'React.renderComponent()', options: [{react: '0.11.0'}]} | ||
], | ||
|
||
invalid: [{ | ||
code: 'React.renderComponent()', | ||
options: [{react: '0.12.0'}], | ||
errors: [{ | ||
message: 'React.renderComponent is deprecated since React 0.12.0, use React.render instead' | ||
}] | ||
}, { | ||
code: 'this.transferPropsTo()', | ||
errors: [{ | ||
message: 'this.transferPropsTo is deprecated since React 0.12.0, use spread operator ({...}) instead' | ||
}] | ||
}, { | ||
code: 'React.addons.classSet()', | ||
errors: [{ | ||
message: 'React.addons.classSet is deprecated since React 0.13.0, use the npm module classnames instead' | ||
}] | ||
}, { | ||
code: 'React.render(element, container);', | ||
errors: [{ | ||
message: 'React.render is deprecated since React 0.14.0, use ReactDOM.render instead' | ||
}] | ||
}, { | ||
code: 'React.unmountComponentAtNode(container);', | ||
errors: [{ | ||
message: ( | ||
'React.unmountComponentAtNode is deprecated since React 0.14.0, ' + | ||
'use ReactDOM.unmountComponentAtNode instead' | ||
) | ||
}] | ||
}, { | ||
code: 'React.findDOMNode(instance);', | ||
errors: [{ | ||
message: 'React.findDOMNode is deprecated since React 0.14.0, use ReactDOM.findDOMNode instead' | ||
}] | ||
}, { | ||
code: 'React.renderToString(element);', | ||
errors: [{ | ||
message: 'React.renderToString is deprecated since React 0.14.0, use ReactDOMServer.renderToString instead' | ||
}] | ||
}, { | ||
code: 'React.renderToStaticMarkup(element);', | ||
errors: [{ | ||
message: ( | ||
'React.renderToStaticMarkup is deprecated since React 0.14.0, ' + | ||
'use ReactDOMServer.renderToStaticMarkup instead' | ||
) | ||
}] | ||
}] | ||
|
||
}); |