self_closing_comp

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2026 License: MIT, MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SelfClosingCompRule = rule.Rule{
	Name: "react/self-closing-comp",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		checkComponent := true
		checkHTML := true

		optsMap := utils.GetOptionsMap(options)
		if optsMap != nil {
			if v, ok := optsMap["component"]; ok {
				if b, ok := v.(bool); ok {
					checkComponent = b
				}
			}
			if v, ok := optsMap["html"]; ok {
				if b, ok := v.(bool); ok {
					checkHTML = b
				}
			}
		}

		return rule.RuleListeners{
			ast.KindJsxOpeningElement: func(node *ast.Node) {
				parent := node.Parent
				if parent == nil || parent.Kind != ast.KindJsxElement {
					return
				}

				jsxElement := parent.AsJsxElement()

				if !isChildrenEmpty(jsxElement) {
					return
				}

				openingElement := node.AsJsxOpeningElement()
				tagName := openingElement.TagName

				isComp := isComponent(ctx.SourceFile, tagName)

				if isComp && !checkComponent {
					return
				}
				if !isComp && !checkHTML {
					return
				}

				openEnd := node.End()
				closeEnd := parent.End()

				fixRange := core.NewTextRange(openEnd-1, closeEnd)
				fix := rule.RuleFix{
					Text:  " />",
					Range: fixRange,
				}

				ctx.ReportNodeWithFixes(parent, rule.RuleMessage{
					Id:          "notSelfClosing",
					Description: "Empty components are self-closing",
				}, fix)
			},
		}
	},
}

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