-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
void-dom-elements-no-children.js
166 lines (136 loc) · 4.04 KB
/
void-dom-elements-no-children.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
163
164
165
166
/**
* @fileoverview Prevent void elements (e.g. <img />, <br />) from receiving
* children
* @author Joe Lencioni
*/
'use strict';
const has = require('hasown');
const docsUrl = require('../util/docsUrl');
const isCreateElement = require('../util/isCreateElement');
const report = require('../util/report');
// ------------------------------------------------------------------------------
// Helpers
// ------------------------------------------------------------------------------
// Using an object here to avoid array scan. We should switch to Set once
// support is good enough.
const VOID_DOM_ELEMENTS = {
area: true,
base: true,
br: true,
col: true,
embed: true,
hr: true,
img: true,
input: true,
keygen: true,
link: true,
menuitem: true,
meta: true,
param: true,
source: true,
track: true,
wbr: true,
};
function isVoidDOMElement(elementName) {
return has(VOID_DOM_ELEMENTS, elementName);
}
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
const noChildrenInVoidEl = 'Void DOM element <{{element}} /> cannot receive children.';
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
docs: {
description: 'Disallow void DOM elements (e.g. `<img />`, `<br />`) from receiving children',
category: 'Best Practices',
recommended: false,
url: docsUrl('void-dom-elements-no-children'),
},
messages: {
noChildrenInVoidEl,
},
schema: [],
},
create: (context) => ({
JSXElement(node) {
const elementName = node.openingElement.name.name;
if (!isVoidDOMElement(elementName)) {
// e.g. <div />
return;
}
if (node.children.length > 0) {
// e.g. <br>Foo</br>
report(context, noChildrenInVoidEl, 'noChildrenInVoidEl', {
node,
data: {
element: elementName,
},
});
}
const attributes = node.openingElement.attributes;
const hasChildrenAttributeOrDanger = attributes.some((attribute) => {
if (!attribute.name) {
return false;
}
return attribute.name.name === 'children' || attribute.name.name === 'dangerouslySetInnerHTML';
});
if (hasChildrenAttributeOrDanger) {
// e.g. <br children="Foo" />
report(context, noChildrenInVoidEl, 'noChildrenInVoidEl', {
node,
data: {
element: elementName,
},
});
}
},
CallExpression(node) {
if (node.callee.type !== 'MemberExpression' && node.callee.type !== 'Identifier') {
return;
}
if (!isCreateElement(context, node)) {
return;
}
const args = node.arguments;
if (args.length < 1) {
// React.createElement() should not crash linter
return;
}
const elementName = 'value' in args[0] ? args[0].value : undefined;
if (!isVoidDOMElement(elementName)) {
// e.g. React.createElement('div');
return;
}
if (args.length < 2 || args[1].type !== 'ObjectExpression') {
return;
}
const firstChild = args[2];
if (firstChild) {
// e.g. React.createElement('br', undefined, 'Foo')
report(context, noChildrenInVoidEl, 'noChildrenInVoidEl', {
node,
data: {
element: elementName,
},
});
}
const props = args[1].properties;
const hasChildrenPropOrDanger = props.some((prop) => {
if (!('key' in prop) || !prop.key || !('name' in prop.key)) {
return false;
}
return prop.key.name === 'children' || prop.key.name === 'dangerouslySetInnerHTML';
});
if (hasChildrenPropOrDanger) {
// e.g. React.createElement('br', { children: 'Foo' })
report(context, noChildrenInVoidEl, 'noChildrenInVoidEl', {
node,
data: {
element: elementName,
},
});
}
},
}),
};