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 | module.exports = {
|
---|
51 | meta: {
|
---|
52 | docs: {
|
---|
53 | description: 'Disallow void DOM elements (e.g. `<img />`, `<br />`) from receiving children',
|
---|
54 | category: 'Best Practices',
|
---|
55 | recommended: false,
|
---|
56 | url: docsUrl('void-dom-elements-no-children'),
|
---|
57 | },
|
---|
58 |
|
---|
59 | messages: {
|
---|
60 | noChildrenInVoidEl,
|
---|
61 | },
|
---|
62 |
|
---|
63 | schema: [],
|
---|
64 | },
|
---|
65 |
|
---|
66 | create: (context) => ({
|
---|
67 | JSXElement(node) {
|
---|
68 | const elementName = node.openingElement.name.name;
|
---|
69 |
|
---|
70 | if (!isVoidDOMElement(elementName)) {
|
---|
71 | // e.g. <div />
|
---|
72 | return;
|
---|
73 | }
|
---|
74 |
|
---|
75 | if (node.children.length > 0) {
|
---|
76 | // e.g. <br>Foo</br>
|
---|
77 | report(context, noChildrenInVoidEl, 'noChildrenInVoidEl', {
|
---|
78 | node,
|
---|
79 | data: {
|
---|
80 | element: elementName,
|
---|
81 | },
|
---|
82 | });
|
---|
83 | }
|
---|
84 |
|
---|
85 | const attributes = node.openingElement.attributes;
|
---|
86 |
|
---|
87 | const hasChildrenAttributeOrDanger = attributes.some((attribute) => {
|
---|
88 | if (!attribute.name) {
|
---|
89 | return false;
|
---|
90 | }
|
---|
91 |
|
---|
92 | return attribute.name.name === 'children' || attribute.name.name === 'dangerouslySetInnerHTML';
|
---|
93 | });
|
---|
94 |
|
---|
95 | if (hasChildrenAttributeOrDanger) {
|
---|
96 | // e.g. <br children="Foo" />
|
---|
97 | report(context, noChildrenInVoidEl, 'noChildrenInVoidEl', {
|
---|
98 | node,
|
---|
99 | data: {
|
---|
100 | element: elementName,
|
---|
101 | },
|
---|
102 | });
|
---|
103 | }
|
---|
104 | },
|
---|
105 |
|
---|
106 | CallExpression(node) {
|
---|
107 | if (node.callee.type !== 'MemberExpression' && node.callee.type !== 'Identifier') {
|
---|
108 | return;
|
---|
109 | }
|
---|
110 |
|
---|
111 | if (!isCreateElement(context, node)) {
|
---|
112 | return;
|
---|
113 | }
|
---|
114 |
|
---|
115 | const args = node.arguments;
|
---|
116 |
|
---|
117 | if (args.length < 1) {
|
---|
118 | // React.createElement() should not crash linter
|
---|
119 | return;
|
---|
120 | }
|
---|
121 |
|
---|
122 | const elementName = args[0].value;
|
---|
123 |
|
---|
124 | if (!isVoidDOMElement(elementName)) {
|
---|
125 | // e.g. React.createElement('div');
|
---|
126 | return;
|
---|
127 | }
|
---|
128 |
|
---|
129 | if (args.length < 2 || args[1].type !== 'ObjectExpression') {
|
---|
130 | return;
|
---|
131 | }
|
---|
132 |
|
---|
133 | const firstChild = args[2];
|
---|
134 | if (firstChild) {
|
---|
135 | // e.g. React.createElement('br', undefined, 'Foo')
|
---|
136 | report(context, noChildrenInVoidEl, 'noChildrenInVoidEl', {
|
---|
137 | node,
|
---|
138 | data: {
|
---|
139 | element: elementName,
|
---|
140 | },
|
---|
141 | });
|
---|
142 | }
|
---|
143 |
|
---|
144 | const props = args[1].properties;
|
---|
145 |
|
---|
146 | const hasChildrenPropOrDanger = props.some((prop) => {
|
---|
147 | if (!prop.key) {
|
---|
148 | return false;
|
---|
149 | }
|
---|
150 |
|
---|
151 | return prop.key.name === 'children' || prop.key.name === 'dangerouslySetInnerHTML';
|
---|
152 | });
|
---|
153 |
|
---|
154 | if (hasChildrenPropOrDanger) {
|
---|
155 | // e.g. React.createElement('br', { children: 'Foo' })
|
---|
156 | report(context, noChildrenInVoidEl, 'noChildrenInVoidEl', {
|
---|
157 | node,
|
---|
158 | data: {
|
---|
159 | element: elementName,
|
---|
160 | },
|
---|
161 | });
|
---|
162 | }
|
---|
163 | },
|
---|
164 | }),
|
---|
165 | };
|
---|