jsx_equals_spacing

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: MIT, MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var JsxEqualsSpacingRule = rule.Rule{
	Name: "react/jsx-equals-spacing",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		mode := "never"

		if arr, ok := options.([]interface{}); ok {
			if len(arr) > 0 {
				if m, ok := arr[0].(string); ok {
					mode = m
				}
			}
		} else if m, ok := options.(string); ok {
			mode = m
		}

		checkAttributes := func(node *ast.Node) {
			var attrs *ast.Node
			if node.Kind == ast.KindJsxOpeningElement {
				attrs = node.AsJsxOpeningElement().Attributes
			} else {
				attrs = node.AsJsxSelfClosingElement().Attributes
			}

			if attrs == nil {
				return
			}

			jsxAttrs := attrs.AsJsxAttributes()
			if jsxAttrs.Properties == nil {
				return
			}

			text := ctx.SourceFile.Text()

			for _, prop := range jsxAttrs.Properties.Nodes {
				if prop.Kind != ast.KindJsxAttribute {
					continue
				}

				attr := prop.AsJsxAttribute()
				if attr.Initializer == nil {
					continue
				}

				nameNode := attr.Name()
				if nameNode == nil {
					continue
				}

				nameTrimmed := utils.TrimNodeTextRange(ctx.SourceFile, nameNode)
				initTrimmed := utils.TrimNodeTextRange(ctx.SourceFile, attr.Initializer)
				nameEnd := nameTrimmed.End()
				initStart := initTrimmed.Pos()

				equalsPos := -1
				for i := nameEnd; i < initStart && i < len(text); i++ {
					if text[i] == '=' {
						equalsPos = i
						break
					}
				}

				if equalsPos < 0 {
					continue
				}

				if mode == "never" {
					if equalsPos > nameEnd {
						ctx.ReportRangeWithFixes(
							core.NewTextRange(nameEnd, equalsPos),
							rule.RuleMessage{
								Id:          "noSpaceBefore",
								Description: "There should be no space before '='",
							},
							rule.RuleFix{
								Text:  "",
								Range: core.NewTextRange(nameEnd, equalsPos),
							},
						)
					}

					afterEquals := equalsPos + 1
					if afterEquals < initStart {
						ctx.ReportRangeWithFixes(
							core.NewTextRange(afterEquals, initStart),
							rule.RuleMessage{
								Id:          "noSpaceAfter",
								Description: "There should be no space after '='",
							},
							rule.RuleFix{
								Text:  "",
								Range: core.NewTextRange(afterEquals, initStart),
							},
						)
					}
				} else {

					if equalsPos == nameEnd {
						insertPos := core.NewTextRange(equalsPos, equalsPos)
						ctx.ReportRangeWithFixes(
							insertPos,
							rule.RuleMessage{
								Id:          "needSpaceBefore",
								Description: "A space is required before '='",
							},
							rule.RuleFix{
								Text:  " ",
								Range: insertPos,
							},
						)
					}

					afterEquals := equalsPos + 1
					if afterEquals == initStart {
						insertPos := core.NewTextRange(afterEquals, afterEquals)
						ctx.ReportRangeWithFixes(
							insertPos,
							rule.RuleMessage{
								Id:          "needSpaceAfter",
								Description: "A space is required after '='",
							},
							rule.RuleFix{
								Text:  " ",
								Range: insertPos,
							},
						)
					}
				}
			}
		}

		return rule.RuleListeners{
			ast.KindJsxOpeningElement:     checkAttributes,
			ast.KindJsxSelfClosingElement: checkAttributes,
		}
	},
}

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