1 | /**
|
---|
2 | * @fileoverview Prevent void elements (e.g. <img />, <br />) from receiving
|
---|
3 | * children
|
---|
4 | * @author Joe Lencioni
|
---|
5 | */
|
---|
6 |
|
---|
7 | 'use strict';
|
---|
8 |
|
---|
9 | const has = require('hasown');
|
---|
10 |
|
---|
11 | const docsUrl = require('../util/docsUrl');
|
---|
12 | const isCreateElement = require('../util/isCreateElement');
|
---|
13 | const report = require('../util/report');
|
---|
14 |
|
---|
15 | // ------------------------------------------------------------------------------
|
---|
16 | // Helpers
|
---|
17 | // ------------------------------------------------------------------------------
|
---|
18 |
|
---|
19 | // Using an object here to avoid array scan. We should switch to Set once
|
---|
20 | // support is good enough.
|
---|
21 | const VOID_DOM_ELEMENTS = {
|
---|
22 | area: true,
|
---|
23 | base: true,
|
---|
24 | br: true,
|
---|
25 | col: true,
|
---|
26 | embed: true,
|
---|
27 | hr: true,
|
---|
28 | img: true,
|
---|
29 | input: true,
|
---|
30 | keygen: true,
|
---|
31 | link: true,
|
---|
32 | menuitem: true,
|
---|
33 | meta: true,
|
---|
34 | param: true,
|
---|
35 | source: true,
|
---|
36 | track: true,
|
---|
37 | wbr: true,
|
---|
38 | };
|
---|
39 |
|
---|
40 | function isVoidDOMElement(elementName) {
|
---|
41 | return has(VOID_DOM_ELEMENTS, elementName);
|
---|
42 | }
|
---|
43 |
|
---|
44 | // ------------------------------------------------------------------------------
|
---|
45 | // Rule Definition
|
---|
46 | // ------------------------------------------------------------------------------
|
---|
47 |
|
---|
48 | const noChildrenInVoidEl = 'Void DOM element <{{element}} /> cannot receive children.';
|
---|
49 |
|
---|
50 | /** @type {import('eslint').Rule.RuleModule} */
|
---|
51 | module.exports = {
|
---|
52 | meta: {
|
---|
53 | docs: {
|
---|
54 | description: 'Disallow void DOM elements (e.g. `<img />`, `<br />`) from receiving children',
|
---|
55 | category: 'Best Practices',
|
---|
56 | recommended: false,
|
---|
57 | url: docsUrl('void-dom-elements-no-children'),
|
---|
58 | },
|
---|
59 |
|
---|
60 | messages: {
|
---|
61 | noChildrenInVoidEl,
|
---|
62 | },
|
---|
63 |
|
---|
64 | schema: [],
|
---|
65 | },
|
---|
66 |
|
---|
67 | create: (context) => ({
|
---|
68 | JSXElement(node) {
|
---|
69 | const elementName = node.openingElement.name.name;
|
---|
70 |
|
---|
71 | if (!isVoidDOMElement(elementName)) {
|
---|
72 | // e.g. <div />
|
---|
73 | return;
|
---|
74 | }
|
---|
75 |
|
---|
76 | if (node.children.length > 0) {
|
---|
77 | // e.g. <br>Foo</br>
|
---|
78 | report(context, noChildrenInVoidEl, 'noChildrenInVoidEl', {
|
---|
79 | node,
|
---|
80 | data: {
|
---|
81 | element: elementName,
|
---|
82 | },
|
---|
83 | });
|
---|
84 | }
|
---|
85 |
|
---|
86 | const attributes = node.openingElement.attributes;
|
---|
87 |
|
---|
88 | const hasChildrenAttributeOrDanger = attributes.some((attribute) => {
|
---|
89 | if (!attribute.name) {
|
---|
90 | return false;
|
---|
91 | }
|
---|
92 |
|
---|
93 | return attribute.name.name === 'children' || attribute.name.name === 'dangerouslySetInnerHTML';
|
---|
94 | });
|
---|
95 |
|
---|
96 | if (hasChildrenAttributeOrDanger) {
|
---|
97 | // e.g. <br children="Foo" />
|
---|
98 | report(context, noChildrenInVoidEl, 'noChildrenInVoidEl', {
|
---|
99 | node,
|
---|
100 | data: {
|
---|
101 | element: elementName,
|
---|
102 | },
|
---|
103 | });
|
---|
104 | }
|
---|
105 | },
|
---|
106 |
|
---|
107 | CallExpression(node) {
|
---|
108 | if (node.callee.type !== 'MemberExpression' && node.callee.type !== 'Identifier') {
|
---|
109 | return;
|
---|
110 | }
|
---|
111 |
|
---|
112 | if (!isCreateElement(context, node)) {
|
---|
113 | return;
|
---|
114 | }
|
---|
115 |
|
---|
116 | const args = node.arguments;
|
---|
117 |
|
---|
118 | if (args.length < 1) {
|
---|
119 | // React.createElement() should not crash linter
|
---|
120 | return;
|
---|
121 | }
|
---|
122 |
|
---|
123 | const elementName = 'value' in args[0] ? args[0].value : undefined;
|
---|
124 |
|
---|
125 | if (!isVoidDOMElement(elementName)) {
|
---|
126 | // e.g. React.createElement('div');
|
---|
127 | return;
|
---|
128 | }
|
---|
129 |
|
---|
130 | if (args.length < 2 || args[1].type !== 'ObjectExpression') {
|
---|
131 | return;
|
---|
132 | }
|
---|
133 |
|
---|
134 | const firstChild = args[2];
|
---|
135 | if (firstChild) {
|
---|
136 | // e.g. React.createElement('br', undefined, 'Foo')
|
---|
137 | report(context, noChildrenInVoidEl, 'noChildrenInVoidEl', {
|
---|
138 | node,
|
---|
139 | data: {
|
---|
140 | element: elementName,
|
---|
141 | },
|
---|
142 | });
|
---|
143 | }
|
---|
144 |
|
---|
145 | const props = args[1].properties;
|
---|
146 |
|
---|
147 | const hasChildrenPropOrDanger = props.some((prop) => {
|
---|
148 | if (!('key' in prop) || !prop.key || !('name' in prop.key)) {
|
---|
149 | return false;
|
---|
150 | }
|
---|
151 |
|
---|
152 | return prop.key.name === 'children' || prop.key.name === 'dangerouslySetInnerHTML';
|
---|
153 | });
|
---|
154 |
|
---|
155 | if (hasChildrenPropOrDanger) {
|
---|
156 | // e.g. React.createElement('br', { children: 'Foo' })
|
---|
157 | report(context, noChildrenInVoidEl, 'noChildrenInVoidEl', {
|
---|
158 | node,
|
---|
159 | data: {
|
---|
160 | element: elementName,
|
---|
161 | },
|
---|
162 | });
|
---|
163 | }
|
---|
164 | },
|
---|
165 | }),
|
---|
166 | };
|
---|