void_dom_elements_no_children

package
v0.5.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 14, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var VoidDomElementsNoChildrenRule = rule.Rule{
	Name: "react/void-dom-elements-no-children",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		return rule.RuleListeners{
			ast.KindCallExpression: func(node *ast.Node) {
				call := node.AsCallExpression()
				if !reactutil.IsCreateElementCall(call.Expression, reactutil.GetReactPragma(ctx.Settings)) {
					return
				}
				args := call.Arguments
				if args == nil || len(args.Nodes) < 1 {
					return
				}

				firstArg := args.Nodes[0]
				if firstArg.Kind != ast.KindStringLiteral {
					return
				}
				elementName := firstArg.AsStringLiteral().Text
				if !voidElements[elementName] {
					return
				}

				if len(args.Nodes) >= 3 {
					ctx.ReportNode(node, rule.RuleMessage{
						Id:          "noChildrenInVoidEl",
						Description: "Void DOM element <" + elementName + " /> cannot receive children.",
					})
					return
				}

				if len(args.Nodes) < 2 || args.Nodes[1].Kind != ast.KindObjectLiteralExpression {
					return
				}

				{
					obj := args.Nodes[1].AsObjectLiteralExpression()
					if obj.Properties != nil {
						for _, prop := range obj.Properties.Nodes {
							if prop.Kind == ast.KindPropertyAssignment {
								nameNode := prop.AsPropertyAssignment().Name()
								if nameNode != nil && nameNode.Kind == ast.KindIdentifier {
									propName := nameNode.AsIdentifier().Text
									if propName == "children" || propName == "dangerouslySetInnerHTML" {
										ctx.ReportNode(node, rule.RuleMessage{
											Id:          "noChildrenInVoidEl",
											Description: "Void DOM element <" + elementName + " /> cannot receive children.",
										})
										return
									}
								}
							}
						}
					}
				}
			},
			ast.KindJsxElement: func(node *ast.Node) {
				jsxElement := node.AsJsxElement()
				openingElement := jsxElement.OpeningElement.AsJsxOpeningElement()
				tagName := getTagNameText(ctx.SourceFile, openingElement.TagName)

				if !voidElements[tagName] {
					return
				}

				hasChildren := jsxElement.Children != nil && len(jsxElement.Children.Nodes) > 0
				hasChildrenAttr, hasDangerousAttr := hasChildrenOrDangerousJsxAttr(openingElement.Attributes)

				if hasChildren || hasChildrenAttr || hasDangerousAttr {
					ctx.ReportNode(node, rule.RuleMessage{
						Id:          "noChildrenInVoidEl",
						Description: "Void DOM element <" + tagName + " /> cannot receive children.",
					})
				}
			},
			ast.KindJsxSelfClosingElement: func(node *ast.Node) {
				selfClosing := node.AsJsxSelfClosingElement()
				tagName := getTagNameText(ctx.SourceFile, selfClosing.TagName)

				if !voidElements[tagName] {
					return
				}

				hasChildrenAttr, hasDangerousAttr := hasChildrenOrDangerousJsxAttr(selfClosing.Attributes)

				if hasChildrenAttr || hasDangerousAttr {
					ctx.ReportNode(node, rule.RuleMessage{
						Id:          "noChildrenInVoidEl",
						Description: "Void DOM element <" + tagName + " /> cannot receive children.",
					})
				}
			},
		}
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL