-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathno-render-return-undefined.js
157 lines (138 loc) · 5.5 KB
/
no-render-return-undefined.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* @fileoverview Prevent returning undefined from react components
* @author Akul Srivastava
*/
'use strict';
const astUtil = require('../util/ast');
const docsUrl = require('../util/docsUrl');
const isFirstLetterCapitalized = require('../util/isFirstLetterCapitalized');
const report = require('../util/report');
const variableUtil = require('../util/variable');
const messages = {
returnsUndefined: "Don't return `undefined` from react components",
};
function getReturnValue(context, returnNode) {
const variables = variableUtil.variablesInScope(context, returnNode);
const returnIdentifierName = returnNode && returnNode.name;
const returnIdentifierVar = variableUtil.getVariable(
variables,
returnIdentifierName
);
if (!returnNode) return undefined;
if (
returnIdentifierVar
&& returnIdentifierVar.defs
&& returnIdentifierVar.defs[0]
) {
const value = returnIdentifierVar.defs[0].node.init;
if (
returnIdentifierVar.defs[0].node
&& returnIdentifierVar.defs[0].node.type === 'VariableDeclarator'
&& value === null
) {
return undefined;
}
return value;
}
switch (returnNode.type) {
case 'LogicalExpression': {
return getReturnValue(context, returnNode.right);
}
case 'ConditionalExpression': {
const possibleReturnValue = [
getReturnValue(context, returnNode.consequent),
getReturnValue(context, returnNode.alternate),
];
const returnsUndefined = possibleReturnValue.some((val) => typeof val === 'undefined');
if (returnsUndefined) return;
return possibleReturnValue;
}
case 'CallExpression': {
if (returnNode.callee.type === 'MemberExpression') {
const calleeObjName = returnNode.callee.object.name;
const calleePropertyName = returnNode.callee.property.name;
const calleeObjNode = variables.find((item) => item && item.name === calleeObjName);
const isCalleeObjArray = calleeObjNode.defs[0].node.init.type === 'ArrayExpression';
const isMapCall = isCalleeObjArray && calleePropertyName === 'map';
if (isMapCall) {
const mapCallback = returnNode.arguments[0];
const mapReturnStatement = mapCallback.body.type === 'BlockStatement'
&& astUtil.findReturnStatement(returnNode.arguments[0]);
const mapReturnNode = (mapReturnStatement && mapReturnStatement.argument) || mapCallback.body;
// console.log('DEBUG', mapReturnNode);
return getReturnValue(context, mapReturnNode);
}
}
const calleeName = returnNode.callee.name;
const calleeNode = variables.find((item) => item && item.name === calleeName);
const calleeDefinitionNode = calleeNode && calleeNode.defs && calleeNode.defs[0] && calleeNode.defs[0].node;
const calleeReturnStatement = astUtil.findReturnStatement(calleeDefinitionNode);
const calleeReturnNode = (calleeReturnStatement && calleeReturnStatement.argument)
|| (calleeDefinitionNode.init && calleeDefinitionNode.init.body);
return getReturnValue(context, calleeReturnNode);
}
case 'ArrayExpression': {
return returnNode.elements;
}
case 'JSXElement': {
return returnNode;
}
default:
return returnNode.value;
}
}
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
docs: {
description: 'Disallow returning `undefined` from react components',
category: 'Best Practices',
recommended: false,
url: docsUrl('no-render-return-undefined'),
},
messages,
schema: [],
},
create(context) {
const isReturningUndefined = (returnStatement) => {
const returnNode = returnStatement && returnStatement.argument;
const returnIdentifierName = returnNode && returnNode.name;
const returnIdentifierValue = getReturnValue(context, returnNode);
const returnsArrayHavingUndefined = Array.isArray(returnIdentifierValue)
&& returnIdentifierValue.some((el) => el && el.type === 'Identifier' && el.name === 'undefined');
return !returnStatement
|| returnIdentifierName === 'undefined'
|| typeof returnIdentifierValue === 'undefined'
|| (returnIdentifierValue && returnIdentifierValue.name === 'undefined')
|| returnsArrayHavingUndefined;
};
const handleFunctionalComponents = (node) => {
const fnName = (node.id && node.id.name) || (node.parent.id && node.parent.id.name);
// Considering functions starting with Uppercase letters are react components
const isReactComponent = isFirstLetterCapitalized(fnName);
const returnStatement = astUtil.findReturnStatement(node);
if (!isReactComponent) return;
if (isReturningUndefined(returnStatement)) {
report(context, messages.returnsUndefined, 'returnsUndefined', {
node,
});
}
};
const handleClassComponents = (node) => {
const componentProperties = astUtil.getComponentProperties(node);
const renderFnNode = componentProperties.find((prop) => prop.key.name === 'render');
const returnStatement = astUtil.findReturnStatement(renderFnNode);
if (isReturningUndefined(returnStatement)) {
report(context, messages.returnsUndefined, 'returnsUndefined', {
node,
});
}
};
return {
FunctionDeclaration: handleFunctionalComponents,
ArrowFunctionExpression: handleFunctionalComponents,
ClassDeclaration: handleClassComponents,
ClassExpression: handleClassComponents,
};
},
};