|
| 1 | +/** |
| 2 | + * @fileoverview Prevent usage of UNSAFE_ methods |
| 3 | + * @author Sergei Startsev |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +const Components = require('../util/Components'); |
| 9 | +const astUtil = require('../util/ast'); |
| 10 | +const docsUrl = require('../util/docsUrl'); |
| 11 | + |
| 12 | +// ------------------------------------------------------------------------------ |
| 13 | +// Rule Definition |
| 14 | +// ------------------------------------------------------------------------------ |
| 15 | + |
| 16 | +module.exports = { |
| 17 | + meta: { |
| 18 | + docs: { |
| 19 | + description: 'Prevent usage of UNSAFE_ methods', |
| 20 | + category: 'Best Practices', |
| 21 | + recommended: true, |
| 22 | + url: docsUrl('no-unsafe') |
| 23 | + }, |
| 24 | + schema: [] |
| 25 | + }, |
| 26 | + |
| 27 | + create: Components.detect((context, components, utils) => { |
| 28 | + /** |
| 29 | + * Returns a list of unsafe methods |
| 30 | + * @returns {Array} A list of unsafe methods |
| 31 | + */ |
| 32 | + function getUnsafeMethods() { |
| 33 | + return [ |
| 34 | + 'UNSAFE_componentWillMount', |
| 35 | + 'UNSAFE_componentWillReceiveProps', |
| 36 | + 'UNSAFE_componentWillUpdate' |
| 37 | + ]; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Checks if a passed method is unsafe |
| 42 | + * @param {string} method Life cycle method |
| 43 | + * @returns {boolean} Returns true for unsafe methods, otherwise returns false |
| 44 | + */ |
| 45 | + function isUnsafe(method) { |
| 46 | + const unsafeMethods = getUnsafeMethods(); |
| 47 | + return unsafeMethods.indexOf(method) !== -1; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Reports the error for an unsafe method |
| 52 | + * @param {ASTNode} node The AST node being checked |
| 53 | + * @param {string} method Life cycle method |
| 54 | + */ |
| 55 | + function checkUnsafe(node, method) { |
| 56 | + if (!isUnsafe(method)) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + context.report({ |
| 61 | + node: node, |
| 62 | + message: `Do not use ${method}` |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Returns life cycle methods if available |
| 68 | + * @param {ASTNode} node The AST node being checked. |
| 69 | + * @returns {Array} The array of methods. |
| 70 | + */ |
| 71 | + function getLifeCycleMethods(node) { |
| 72 | + const properties = astUtil.getComponentProperties(node); |
| 73 | + return properties.map(property => astUtil.getPropertyName(property)); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Checks life cycle methods |
| 78 | + * @param {ASTNode} node The AST node being checked. |
| 79 | + */ |
| 80 | + function checkLifeCycleMethods(node) { |
| 81 | + if (utils.isES5Component(node) || utils.isES6Component(node)) { |
| 82 | + const methods = getLifeCycleMethods(node); |
| 83 | + methods.forEach(method => checkUnsafe(node, method)); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return { |
| 88 | + ClassDeclaration: checkLifeCycleMethods, |
| 89 | + ClassExpression: checkLifeCycleMethods, |
| 90 | + ObjectExpression: checkLifeCycleMethods |
| 91 | + }; |
| 92 | + }) |
| 93 | +}; |
0 commit comments