UNPKG

3.98 kBJavaScriptView Raw
1/**
2 * @fileoverview Prevent usage of unsafe lifecycle methods
3 * @author Sergei Startsev
4 */
5
6'use strict';
7
8const Components = require('../util/Components');
9const astUtil = require('../util/ast');
10const docsUrl = require('../util/docsUrl');
11const versionUtil = require('../util/version');
12
13// ------------------------------------------------------------------------------
14// Rule Definition
15// ------------------------------------------------------------------------------
16
17module.exports = {
18 meta: {
19 docs: {
20 description: 'Prevent usage of unsafe lifecycle methods',
21 category: 'Best Practices',
22 recommended: false,
23 url: docsUrl('no-unsafe')
24 },
25 schema: [
26 {
27 type: 'object',
28 properties: {
29 checkAliases: {
30 default: false,
31 type: 'boolean'
32 }
33 },
34 additionalProperties: false
35 }
36 ]
37 },
38
39 create: Components.detect((context, components, utils) => {
40 const config = context.options[0] || {};
41 const checkAliases = config.checkAliases || false;
42
43 const isApplicable = versionUtil.testReactVersion(context, '16.3.0');
44 if (!isApplicable) {
45 return {};
46 }
47
48 const unsafe = {
49 UNSAFE_componentWillMount: {
50 newMethod: 'componentDidMount',
51 details:
52 'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.'
53 },
54 UNSAFE_componentWillReceiveProps: {
55 newMethod: 'getDerivedStateFromProps',
56 details:
57 'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.'
58 },
59 UNSAFE_componentWillUpdate: {
60 newMethod: 'componentDidUpdate',
61 details:
62 'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.'
63 }
64 };
65 if (checkAliases) {
66 unsafe.componentWillMount = unsafe.UNSAFE_componentWillMount;
67 unsafe.componentWillReceiveProps = unsafe.UNSAFE_componentWillReceiveProps;
68 unsafe.componentWillUpdate = unsafe.UNSAFE_componentWillUpdate;
69 }
70
71 /**
72 * Returns a list of unsafe methods
73 * @returns {Array} A list of unsafe methods
74 */
75 function getUnsafeMethods() {
76 return Object.keys(unsafe);
77 }
78
79 /**
80 * Checks if a passed method is unsafe
81 * @param {string} method Life cycle method
82 * @returns {boolean} Returns true for unsafe methods, otherwise returns false
83 */
84 function isUnsafe(method) {
85 const unsafeMethods = getUnsafeMethods();
86 return unsafeMethods.indexOf(method) !== -1;
87 }
88
89 /**
90 * Reports the error for an unsafe method
91 * @param {ASTNode} node The AST node being checked
92 * @param {string} method Life cycle method
93 */
94 function checkUnsafe(node, method) {
95 if (!isUnsafe(method)) {
96 return;
97 }
98
99 const meta = unsafe[method];
100 const newMethod = meta.newMethod;
101 const details = meta.details;
102
103 context.report({
104 node,
105 message: `${method} is unsafe for use in async rendering. Update the component to use ${newMethod} instead. ${details}`
106 });
107 }
108
109 /**
110 * Returns life cycle methods if available
111 * @param {ASTNode} node The AST node being checked.
112 * @returns {Array} The array of methods.
113 */
114 function getLifeCycleMethods(node) {
115 const properties = astUtil.getComponentProperties(node);
116 return properties.map((property) => astUtil.getPropertyName(property));
117 }
118
119 /**
120 * Checks life cycle methods
121 * @param {ASTNode} node The AST node being checked.
122 */
123 function checkLifeCycleMethods(node) {
124 if (utils.isES5Component(node) || utils.isES6Component(node)) {
125 const methods = getLifeCycleMethods(node);
126 methods.forEach((method) => checkUnsafe(node, method));
127 }
128 }
129
130 return {
131 ClassDeclaration: checkLifeCycleMethods,
132 ClassExpression: checkLifeCycleMethods,
133 ObjectExpression: checkLifeCycleMethods
134 };
135 })
136};