-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathno-render-return-value.js
82 lines (70 loc) · 2.25 KB
/
no-render-return-value.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* @fileoverview Prevent usage of the return value of React.render
* @author Dustan Kasten
*/
'use strict';
const testReactVersion = require('../util/version').testReactVersion;
const docsUrl = require('../util/docsUrl');
const report = require('../util/report');
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
const messages = {
noReturnValue: 'Do not depend on the return value from {{node}}.render',
};
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
docs: {
description: 'Disallow usage of the return value of ReactDOM.render',
category: 'Best Practices',
recommended: true,
url: docsUrl('no-render-return-value'),
},
messages,
schema: [],
},
create(context) {
// --------------------------------------------------------------------------
// Public
// --------------------------------------------------------------------------
let calleeObjectName = /^ReactDOM$/;
if (testReactVersion(context, '>= 15.0.0')) {
calleeObjectName = /^ReactDOM$/;
} else if (testReactVersion(context, '^0.14.0')) {
calleeObjectName = /^React(DOM)?$/;
} else if (testReactVersion(context, '^0.13.0')) {
calleeObjectName = /^React$/;
}
return {
CallExpression(node) {
const callee = node.callee;
const parent = node.parent;
if (callee.type !== 'MemberExpression') {
return;
}
if (
callee.object.type !== 'Identifier'
|| !calleeObjectName.test(callee.object.name)
|| (!('name' in callee.property) || callee.property.name !== 'render')
) {
return;
}
if (
parent.type === 'VariableDeclarator'
|| parent.type === 'Property'
|| parent.type === 'ReturnStatement'
|| parent.type === 'ArrowFunctionExpression'
|| parent.type === 'AssignmentExpression'
) {
report(context, messages.noReturnValue, 'noReturnValue', {
node: callee,
data: {
node: callee.object.name,
},
});
}
},
};
},
};