forked from jsx-eslint/eslint-plugin-react
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjsx-max-depth.js
162 lines (132 loc) · 4.05 KB
/
jsx-max-depth.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
158
159
160
161
162
/**
* @fileoverview Validate JSX maximum depth
* @author Chris<[email protected]>
*/
'use strict';
const has = require('object.hasown/polyfill')();
const includes = require('array-includes');
const variableUtil = require('../util/variable');
const jsxUtil = require('../util/jsx');
const docsUrl = require('../util/docsUrl');
const reportC = require('../util/report');
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
const messages = {
wrongDepth: 'Expected the depth of nested jsx elements to be <= {{needed}}, but found {{found}}.',
};
module.exports = {
meta: {
docs: {
description: 'Enforce JSX maximum depth',
category: 'Stylistic Issues',
recommended: false,
url: docsUrl('jsx-max-depth'),
},
messages,
schema: [
{
type: 'object',
properties: {
max: {
type: 'integer',
minimum: 0,
},
},
additionalProperties: false,
},
],
},
create(context) {
const DEFAULT_DEPTH = 2;
const option = context.options[0] || {};
const maxDepth = has(option, 'max') ? option.max : DEFAULT_DEPTH;
function isExpression(node) {
return node.type === 'JSXExpressionContainer';
}
function hasJSX(node) {
return jsxUtil.isJSX(node) || (isExpression(node) && jsxUtil.isJSX(node.expression));
}
function isLeaf(node) {
const children = node.children;
return !children || children.length === 0 || !children.some(hasJSX);
}
function getDepth(node) {
let count = 0;
while (jsxUtil.isJSX(node.parent) || isExpression(node.parent)) {
node = node.parent;
if (jsxUtil.isJSX(node)) {
count += 1;
}
}
return count;
}
function report(node, depth) {
reportC(context, messages.wrongDepth, 'wrongDepth', {
node,
data: {
found: depth,
needed: maxDepth,
},
});
}
function findJSXElementOrFragment(variables, name, previousReferences) {
function find(refs, prevRefs) {
for (let i = refs.length - 1; i >= 0; i--) {
if (has(refs[i], 'writeExpr')) {
const writeExpr = refs[i].writeExpr;
return (jsxUtil.isJSX(writeExpr)
&& writeExpr)
|| ((writeExpr && writeExpr.type === 'Identifier')
&& findJSXElementOrFragment(variables, writeExpr.name, prevRefs));
}
}
return null;
}
const variable = variableUtil.getVariable(variables, name);
if (variable && variable.references) {
const containDuplicates = previousReferences.some((ref) => includes(variable.references, ref));
// Prevent getting stuck in circular references
if (containDuplicates) {
return false;
}
return find(variable.references, previousReferences.concat(variable.references));
}
return false;
}
function checkDescendant(baseDepth, children) {
baseDepth += 1;
(children || []).filter((node) => hasJSX(node)).forEach((node) => {
if (baseDepth > maxDepth) {
report(node, baseDepth);
} else if (!isLeaf(node)) {
checkDescendant(baseDepth, node.children);
}
});
}
function handleJSX(node) {
if (!isLeaf(node)) {
return;
}
const depth = getDepth(node);
if (depth > maxDepth) {
report(node, depth);
}
}
return {
JSXElement: handleJSX,
JSXFragment: handleJSX,
JSXExpressionContainer(node) {
if (node.expression.type !== 'Identifier') {
return;
}
const variables = variableUtil.variablesInScope(context, node);
const element = findJSXElementOrFragment(variables, node.expression.name, []);
if (element) {
const baseDepth = getDepth(node);
checkDescendant(baseDepth, element.children);
}
},
};
},
};