|
| 1 | +use oxc_ast::{ |
| 2 | + ast::{ |
| 3 | + Argument, Expression, JSXAttributeItem, JSXAttributeName, JSXElementName, |
| 4 | + ObjectPropertyKind, PropertyKey, |
| 5 | + }, |
| 6 | + AstKind, |
| 7 | +}; |
| 8 | +use oxc_diagnostics::{ |
| 9 | + miette::{self, Diagnostic}, |
| 10 | + thiserror::Error, |
| 11 | +}; |
| 12 | +use oxc_macros::declare_oxc_lint; |
| 13 | +use oxc_span::Span; |
| 14 | +use phf::phf_set; |
| 15 | + |
| 16 | +use crate::{context::LintContext, rule::Rule, utils::is_create_element_call, AstNode}; |
| 17 | + |
| 18 | +#[derive(Debug, Error, Diagnostic)] |
| 19 | +#[error("eslint-plugin-react(void-dom-elements-no-children): Disallow void DOM elements (e.g. `<img />`, `<br />`) from receiving children.")] |
| 20 | +#[diagnostic(severity(warning), help("Void DOM element <{0:?} /> cannot receive children."))] |
| 21 | +struct VoidDomElementsNoChildrenDiagnostic(pub String, #[label] pub Span); |
| 22 | + |
| 23 | +#[derive(Debug, Default, Clone)] |
| 24 | +pub struct VoidDomElementsNoChildren; |
| 25 | + |
| 26 | +declare_oxc_lint!( |
| 27 | + /// ### What it does |
| 28 | + /// There are some HTML elements that are only self-closing (e.g. img, br, hr). These are collectively known as void DOM elements. |
| 29 | + /// This rule checks that children are not passed to void DOM elements. |
| 30 | + /// |
| 31 | + /// ### Example |
| 32 | + /// ```javascript |
| 33 | + /// // Bad |
| 34 | + /// <br>Children</br> |
| 35 | + /// <br children='Children' /> |
| 36 | + /// <br dangerouslySetInnerHTML={{ __html: 'HTML' }} /> |
| 37 | + /// React.createElement('br', undefined, 'Children') |
| 38 | + /// React.createElement('br', { children: 'Children' }) |
| 39 | + /// React.createElement('br', { dangerouslySetInnerHTML: { __html: 'HTML' } }) |
| 40 | + /// |
| 41 | + /// // Good |
| 42 | + /// <div>Children</div> |
| 43 | + /// <div children='Children' /> |
| 44 | + /// <div dangerouslySetInnerHTML={{ __html: 'HTML' }} /> |
| 45 | + /// React.createElement('div', undefined, 'Children') |
| 46 | + /// React.createElement('div', { children: 'Children' }) |
| 47 | + /// React.createElement('div', { dangerouslySetInnerHTML: { __html: 'HTML' } }) |
| 48 | + /// ``` |
| 49 | + VoidDomElementsNoChildren, |
| 50 | + correctness |
| 51 | +); |
| 52 | + |
| 53 | +const VOID_DOM_ELEMENTS: phf::Set<&'static str> = phf_set![ |
| 54 | + "area", "base", "br", "col", "embed", "hr", "img", "input", "keygen", "link", "menuitem", |
| 55 | + "meta", "param", "source", "track", "wbr", |
| 56 | +]; |
| 57 | + |
| 58 | +pub fn is_void_dom_element(element_name: &str) -> bool { |
| 59 | + VOID_DOM_ELEMENTS.contains(element_name) |
| 60 | +} |
| 61 | + |
| 62 | +impl Rule for VoidDomElementsNoChildren { |
| 63 | + fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { |
| 64 | + match node.kind() { |
| 65 | + AstKind::JSXElement(jsx_el) => { |
| 66 | + let jsx_opening_el = &jsx_el.opening_element; |
| 67 | + let JSXElementName::Identifier(identifier) = &jsx_opening_el.name else { |
| 68 | + return; |
| 69 | + }; |
| 70 | + |
| 71 | + if !is_void_dom_element(&identifier.name) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + let has_children_attribute_or_danger = |
| 76 | + jsx_opening_el.attributes.iter().any(|attribute| match attribute { |
| 77 | + JSXAttributeItem::Attribute(attr) => { |
| 78 | + let JSXAttributeName::Identifier(iden) = &attr.name else { |
| 79 | + return false; |
| 80 | + }; |
| 81 | + iden.name == "children" || iden.name == "dangerouslySetInnerHTML" |
| 82 | + } |
| 83 | + JSXAttributeItem::SpreadAttribute(_) => false, |
| 84 | + }); |
| 85 | + |
| 86 | + if !jsx_el.children.is_empty() || has_children_attribute_or_danger { |
| 87 | + ctx.diagnostic(VoidDomElementsNoChildrenDiagnostic( |
| 88 | + identifier.name.to_string(), |
| 89 | + identifier.span, |
| 90 | + )); |
| 91 | + } |
| 92 | + } |
| 93 | + AstKind::CallExpression(call_expr) => { |
| 94 | + if !is_create_element_call(call_expr) { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + if call_expr.arguments.is_empty() { |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + let Some(Argument::Expression(Expression::StringLiteral(element_name))) = |
| 103 | + call_expr.arguments.first() |
| 104 | + else { |
| 105 | + return; |
| 106 | + }; |
| 107 | + |
| 108 | + if !is_void_dom_element(element_name.value.as_str()) { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + if call_expr.arguments.len() < 2 { |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + let Some(Argument::Expression(Expression::ObjectExpression(obj_expr))) = |
| 117 | + call_expr.arguments.get(1) |
| 118 | + else { |
| 119 | + return; |
| 120 | + }; |
| 121 | + |
| 122 | + let has_children_prop_or_danger = |
| 123 | + obj_expr.properties.iter().any(|property| match property { |
| 124 | + ObjectPropertyKind::ObjectProperty(prop) => match &prop.key { |
| 125 | + PropertyKey::Identifier(iden) => { |
| 126 | + iden.name == "children" || iden.name == "dangerouslySetInnerHTML" |
| 127 | + } |
| 128 | + _ => false, |
| 129 | + }, |
| 130 | + ObjectPropertyKind::SpreadProperty(_) => false, |
| 131 | + }); |
| 132 | + |
| 133 | + if call_expr.arguments.get(2).is_some() || has_children_prop_or_danger { |
| 134 | + ctx.diagnostic(VoidDomElementsNoChildrenDiagnostic( |
| 135 | + element_name.value.to_string(), |
| 136 | + element_name.span, |
| 137 | + )); |
| 138 | + } |
| 139 | + } |
| 140 | + _ => {} |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +#[test] |
| 146 | +fn test() { |
| 147 | + use crate::tester::Tester; |
| 148 | + |
| 149 | + let pass = vec![ |
| 150 | + (r"<div>Foo</div>;", None), |
| 151 | + (r"<div children='Foo' />;", None), |
| 152 | + (r"<div dangerouslySetInnerHTML={{ __html: 'Foo' }} />;", None), |
| 153 | + (r"React.createElement('div', {}, 'Foo');", None), |
| 154 | + (r"React.createElement('div', { children: 'Foo' });", None), |
| 155 | + (r"React.createElement('div', { dangerouslySetInnerHTML: { __html: 'Foo' } });", None), |
| 156 | + (r"React.createElement('img');", None), |
| 157 | + (r"React.createElement();", None), |
| 158 | + ( |
| 159 | + r" |
| 160 | + const props = {}; |
| 161 | + React.createElement('img', props); |
| 162 | + ", |
| 163 | + None, |
| 164 | + ), |
| 165 | + ( |
| 166 | + r" |
| 167 | + import React, {createElement} from 'react'; |
| 168 | + createElement('div'); |
| 169 | + ", |
| 170 | + None, |
| 171 | + ), |
| 172 | + ( |
| 173 | + r" |
| 174 | + import React, {createElement} from 'react'; |
| 175 | + createElement('img'); |
| 176 | + ", |
| 177 | + None, |
| 178 | + ), |
| 179 | + ( |
| 180 | + r" |
| 181 | + import React, {createElement, PureComponent} from 'react'; |
| 182 | + class Button extends PureComponent { |
| 183 | + handleClick(ev) { |
| 184 | + ev.preventDefault(); |
| 185 | + } |
| 186 | + render() { |
| 187 | + return <div onClick={this.handleClick}>Hello</div>; |
| 188 | + } |
| 189 | + } |
| 190 | + ", |
| 191 | + None, |
| 192 | + ), |
| 193 | + ]; |
| 194 | + |
| 195 | + let fail = vec![ |
| 196 | + (r"<br>Foo</br>;", None), |
| 197 | + (r"<br children='Foo' />;", None), |
| 198 | + (r"<img {...props} children='Foo' />;", None), |
| 199 | + (r"<br dangerouslySetInnerHTML={{ __html: 'Foo' }} />;", None), |
| 200 | + (r"React.createElement('br', {}, 'Foo');", None), |
| 201 | + (r"React.createElement('br', { children: 'Foo' });", None), |
| 202 | + (r"React.createElement('br', { dangerouslySetInnerHTML: { __html: 'Foo' } });", None), |
| 203 | + ( |
| 204 | + r" |
| 205 | + import React, {createElement} from 'react'; |
| 206 | + createElement('img', {}, 'Foo'); |
| 207 | + ", |
| 208 | + None, |
| 209 | + ), |
| 210 | + ( |
| 211 | + r" |
| 212 | + import React, {createElement} from 'react'; |
| 213 | + createElement('img', { children: 'Foo' }); |
| 214 | + ", |
| 215 | + None, |
| 216 | + ), |
| 217 | + ( |
| 218 | + r" |
| 219 | + import React, {createElement} from 'react'; |
| 220 | + createElement('img', { dangerouslySetInnerHTML: { __html: 'Foo' } }); |
| 221 | + ", |
| 222 | + None, |
| 223 | + ), |
| 224 | + ]; |
| 225 | + |
| 226 | + Tester::new(VoidDomElementsNoChildren::NAME, pass, fail).test_and_snapshot(); |
| 227 | +} |
0 commit comments