1 |
|
2 |
|
3 |
|
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 | const versionUtil = require('../util/version');
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | module.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 |
|
73 |
|
74 |
|
75 | function getUnsafeMethods() {
|
76 | return Object.keys(unsafe);
|
77 | }
|
78 |
|
79 | |
80 |
|
81 |
|
82 |
|
83 |
|
84 | function isUnsafe(method) {
|
85 | const unsafeMethods = getUnsafeMethods();
|
86 | return unsafeMethods.indexOf(method) !== -1;
|
87 | }
|
88 |
|
89 | |
90 |
|
91 |
|
92 |
|
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 |
|
111 |
|
112 |
|
113 |
|
114 | function getLifeCycleMethods(node) {
|
115 | const properties = astUtil.getComponentProperties(node);
|
116 | return properties.map((property) => astUtil.getPropertyName(property));
|
117 | }
|
118 |
|
119 | |
120 |
|
121 |
|
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 | };
|