source: imaps-frontend/node_modules/eslint-plugin-react/lib/rules/no-unsafe.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

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