forked from jsx-eslint/eslint-plugin-react
/
require-render-return.js
100 lines (88 loc) · 2.95 KB
/
require-render-return.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
/**
* @fileoverview Enforce ES5 or ES6 class for returning value in render function.
* @author Mark Orel
*/
'use strict';
const Components = require('../util/Components');
const astUtil = require('../util/ast');
const docsUrl = require('../util/docsUrl');
const report = require('../util/report');
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
const messages = {
noRenderReturn: 'Your render method should have a return statement',
};
module.exports = {
meta: {
docs: {
description: 'Enforce ES5 or ES6 class for returning value in render function',
category: 'Possible Errors',
recommended: true,
url: docsUrl('require-render-return'),
},
messages,
schema: [],
},
create: Components.detect((context, components, utils) => {
/**
* Mark a return statement as present
* @param {ASTNode} node The AST node being checked.
*/
function markReturnStatementPresent(node) {
components.set(node, {
hasReturnStatement: true,
});
}
/**
* Find render method in a given AST node
* @param {ASTNode} node The component to find render method.
* @returns {ASTNode} Method node if found, undefined if not.
*/
function findRenderMethod(node) {
const properties = astUtil.getComponentProperties(node);
return properties
.filter((property) => astUtil.getPropertyName(property) === 'render' && property.value)
.find((property) => astUtil.isFunctionLikeExpression(property.value));
}
return {
ReturnStatement(node) {
const ancestors = context.getAncestors(node).reverse();
let depth = 0;
ancestors.forEach((ancestor) => {
if (/Function(Expression|Declaration)$/.test(ancestor.type)) {
depth += 1;
}
if (
/(MethodDefinition|Property|ClassProperty|PropertyDefinition)$/.test(ancestor.type)
&& astUtil.getPropertyName(ancestor) === 'render'
&& depth <= 1
) {
markReturnStatementPresent(node);
}
});
},
ArrowFunctionExpression(node) {
if (node.expression === false || astUtil.getPropertyName(node.parent) !== 'render') {
return;
}
markReturnStatementPresent(node);
},
'Program:exit'() {
const list = components.list();
Object.keys(list).forEach((component) => {
if (
!findRenderMethod(list[component].node)
|| list[component].hasReturnStatement
|| (!utils.isES5Component(list[component].node) && !utils.isES6Component(list[component].node))
) {
return;
}
report(context, messages.noRenderReturn, 'noRenderReturn', {
node: findRenderMethod(list[component].node),
});
});
},
};
}),
};