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

main
Last change on this file since 0c6b92a was 0c6b92a, checked in by stefan toskovski <stefantoska84@…>, 5 weeks ago

Pred finalna verzija

  • Property mode set to 100644
File size: 4.3 KB
RevLine 
[d565449]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',
[0c6b92a]60 details: 'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.',
[d565449]61 },
62 UNSAFE_componentWillReceiveProps: {
63 newMethod: 'getDerivedStateFromProps',
[0c6b92a]64 details: 'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.',
[d565449]65 },
66 UNSAFE_componentWillUpdate: {
67 newMethod: 'componentDidUpdate',
[0c6b92a]68 details: 'See https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html.',
[d565449]69 },
70 };
71 if (checkAliases) {
72 unsafe.componentWillMount = unsafe.UNSAFE_componentWillMount;
73 unsafe.componentWillReceiveProps = unsafe.UNSAFE_componentWillReceiveProps;
74 unsafe.componentWillUpdate = unsafe.UNSAFE_componentWillUpdate;
75 }
76
77 /**
78 * Returns a list of unsafe methods
79 * @returns {Array} A list of unsafe methods
80 */
81 function getUnsafeMethods() {
82 return Object.keys(unsafe);
83 }
84
85 /**
86 * Checks if a passed method is unsafe
87 * @param {string} method Life cycle method
88 * @returns {boolean} Returns true for unsafe methods, otherwise returns false
89 */
90 function isUnsafe(method) {
91 const unsafeMethods = getUnsafeMethods();
92 return unsafeMethods.indexOf(method) !== -1;
93 }
94
95 /**
96 * Reports the error for an unsafe method
97 * @param {ASTNode} node The AST node being checked
98 * @param {string} method Life cycle method
99 */
100 function checkUnsafe(node, method) {
101 if (!isUnsafe(method)) {
102 return;
103 }
104
105 const meta = unsafe[method];
106 const newMethod = meta.newMethod;
107 const details = meta.details;
108
109 const propertyNode = astUtil.getComponentProperties(node)
110 .find((property) => astUtil.getPropertyName(property) === method);
111
112 report(context, messages.unsafeMethod, 'unsafeMethod', {
113 node: propertyNode,
114 data: {
115 method,
116 newMethod,
117 details,
118 },
119 });
120 }
121
122 /**
123 * Returns life cycle methods if available
124 * @param {ASTNode} node The AST node being checked.
125 * @returns {Array} The array of methods.
126 */
127 function getLifeCycleMethods(node) {
128 const properties = astUtil.getComponentProperties(node);
129 return properties.map((property) => astUtil.getPropertyName(property));
130 }
131
132 /**
133 * Checks life cycle methods
134 * @param {ASTNode} node The AST node being checked.
135 */
136 function checkLifeCycleMethods(node) {
137 if (componentUtil.isES5Component(node, context) || componentUtil.isES6Component(node, context)) {
138 const methods = getLifeCycleMethods(node);
139 methods
140 .sort((a, b) => a.localeCompare(b))
141 .forEach((method) => checkUnsafe(node, method));
142 }
143 }
144
145 return {
146 ClassDeclaration: checkLifeCycleMethods,
147 ClassExpression: checkLifeCycleMethods,
148 ObjectExpression: checkLifeCycleMethods,
149 };
150 },
151};
Note: See TracBrowser for help on using the repository browser.