|
| 1 | +/** |
| 2 | + * @fileoverview Prevent usage of deprecated methods |
| 3 | + * @author Yannick Croissant |
| 4 | + * @author Scott Feeney |
| 5 | + */ |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +// ------------------------------------------------------------------------------ |
| 9 | +// Constants |
| 10 | +// ------------------------------------------------------------------------------ |
| 11 | + |
| 12 | +var DEPRECATED_MESSAGE = '{{oldMethod}} is deprecated since React {{version}}{{newMethod}}'; |
| 13 | + |
| 14 | +var DEPRECATED = { |
| 15 | + MemberExpression: { |
| 16 | + // 0.12.0 |
| 17 | + 'React.renderComponent': ['0.12.0', 'React.render'], |
| 18 | + 'React.renderComponentToString': ['0.12.0', 'React.renderToString'], |
| 19 | + 'React.renderComponentToStaticMarkup': ['0.12.0', 'React.renderToStaticMarkup'], |
| 20 | + 'React.isValidComponent': ['0.12.0', 'React.isValidElement'], |
| 21 | + 'React.PropTypes.component': ['0.12.0', 'React.PropTypes.element'], |
| 22 | + 'React.PropTypes.renderable': ['0.12.0', 'React.PropTypes.node'], |
| 23 | + 'React.isValidClass': ['0.12.0'], |
| 24 | + 'this.transferPropsTo': ['0.12.0', 'spread operator ({...})'], |
| 25 | + // 0.13.0 |
| 26 | + 'React.addons.classSet': ['0.13.0', 'the npm module classnames'], |
| 27 | + 'React.addons.cloneWithProps': ['0.13.0', 'React.cloneElement'], |
| 28 | + // 0.14.0 |
| 29 | + 'React.render': ['0.14.0', 'ReactDOM.render'], |
| 30 | + 'React.unmountComponentAtNode': ['0.14.0', 'ReactDOM.unmountComponentAtNode'], |
| 31 | + 'React.findDOMNode': ['0.14.0', 'ReactDOM.findDOMNode'], |
| 32 | + 'React.renderToString': ['0.14.0', 'ReactDOMServer.renderToString'], |
| 33 | + 'React.renderToStaticMarkup': ['0.14.0', 'ReactDOMServer.renderToStaticMarkup'] |
| 34 | + } |
| 35 | +}; |
| 36 | + |
| 37 | +// ------------------------------------------------------------------------------ |
| 38 | +// Rule Definition |
| 39 | +// ------------------------------------------------------------------------------ |
| 40 | + |
| 41 | +module.exports = function(context) { |
| 42 | + |
| 43 | + // Validate React version passed in options |
| 44 | + // (append the patch version if missing, allowing shorthands like 0.12 for React 0.12.0) |
| 45 | + var optVer = context.options[0] ? context.options[0].react : '999.999.999'; |
| 46 | + optVer = /^[0-9]+\.[0-9]+$/.test(optVer) ? optVer + '.0' : optVer; |
| 47 | + optVer = optVer.split('.').map(function(part) { |
| 48 | + return Number(part); |
| 49 | + }); |
| 50 | + |
| 51 | + function checkVersion(methodVer) { |
| 52 | + methodVer = methodVer.split('.').map(function(part) { |
| 53 | + return Number(part); |
| 54 | + }); |
| 55 | + var higherMajor = methodVer[0] < optVer[0]; |
| 56 | + var higherMinor = methodVer[0] === optVer[0] && methodVer[1] < optVer[1]; |
| 57 | + var higherOrEqualPatch = methodVer[0] === optVer[0] && methodVer[1] === optVer[1] && methodVer[2] <= optVer[2]; |
| 58 | + |
| 59 | + return higherMajor || higherMinor || higherOrEqualPatch; |
| 60 | + } |
| 61 | + |
| 62 | + function isDeprecated(type, method) { |
| 63 | + return ( |
| 64 | + DEPRECATED[type] && |
| 65 | + DEPRECATED[type][method] && |
| 66 | + checkVersion(DEPRECATED[type][method][0]) |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + // -------------------------------------------------------------------------- |
| 71 | + // Public |
| 72 | + // -------------------------------------------------------------------------- |
| 73 | + |
| 74 | + return { |
| 75 | + |
| 76 | + MemberExpression: function(node) { |
| 77 | + var method = context.getSource(node); |
| 78 | + if (!isDeprecated(node.type, method)) { |
| 79 | + return; |
| 80 | + } |
| 81 | + context.report(node, DEPRECATED_MESSAGE, { |
| 82 | + oldMethod: method, |
| 83 | + version: DEPRECATED[node.type][method][0], |
| 84 | + newMethod: DEPRECATED[node.type][method][1] ? ', use ' + DEPRECATED[node.type][method][1] + ' instead' : '' |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + }; |
| 89 | + |
| 90 | +}; |
| 91 | + |
| 92 | +module.exports.schema = [{ |
| 93 | + type: 'object', |
| 94 | + properties: { |
| 95 | + react: { |
| 96 | + type: 'string', |
| 97 | + pattern: '^[0-9]+\.[0-9]+(\.[0-9]+)?$' |
| 98 | + } |
| 99 | + }, |
| 100 | + additionalProperties: false |
| 101 | +}]; |
0 commit comments