jsx_handler_names

package
v0.5.2 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var JsxHandlerNamesRule = rule.Rule{
	Name: "react/jsx-handler-names",
	Run: func(ctx rule.RuleContext, rawOptions any) rule.RuleListeners {
		opts := parseOptions(rawOptions)

		return rule.RuleListeners{
			ast.KindJsxAttribute: func(node *ast.Node) {
				attr := node.AsJsxAttribute()

				parent := reactutil.GetJsxParentElement(node)
				componentName := reactutil.GetJsxElementTypeString(parent)
				for _, pattern := range opts.ignoreComponentNames {
					if reactutil.MatchGlob(componentName, pattern) {
						return
					}
				}

				init := attr.Initializer
				if init == nil || !ast.IsJsxExpression(init) {
					return
				}
				rawExpr := init.AsJsxExpression().Expression
				if rawExpr == nil {
					return
				}

				expression := ast.SkipParentheses(rawExpr)
				if expression == nil {
					return
				}

				isInline := ast.IsArrowFunction(expression)

				if !opts.checkInlineFunction && isInline {
					return
				}

				if !opts.checkLocalVariables {
					if isInline {
						callee := arrowBodyCallExprCallee(expression)
						if !isPlainMemberAccess(callee) {
							return
						}
					} else {
						if !isPlainMemberAccess(expression) {
							return
						}
					}
				}

				nameNode := attr.Name()
				if nameNode == nil || !ast.IsIdentifier(nameNode) {
					return
				}
				propKey := nameNode.AsIdentifier().Text
				if propKey == "ref" {
					return
				}

				// propValue text — for inline `checkInlineFunction` handlers,
				// upstream reads `getText(expression.body.callee)`; otherwise
				// `getText(expression)`. SkipParentheses on the inner pieces
				// keeps tsgo aligned with ESLint's flat view.
				var textNode *ast.Node
				if opts.checkInlineFunction && isInline {
					callee := arrowBodyCallExprCallee(expression)
					if callee == nil {

						textNode = nil
					} else {
						textNode = callee
					}
				} else {
					textNode = expression
				}

				var propValue string
				if textNode != nil {
					propValue = stripWhitespace(utils.TrimmedNodeText(ctx.SourceFile, textNode))
					propValue = stripThisOrBindBase(propValue)
				}

				propIsEventHandler := opts.propEventHandlerRegex != nil && opts.propEventHandlerRegex.MatchString(propKey)
				propFnIsNamedCorrectly := opts.eventHandlerRegex != nil && opts.eventHandlerRegex.MatchString(propValue)

				switch {
				case propIsEventHandler && opts.eventHandlerRegex != nil && !propFnIsNamedCorrectly:
					data := map[string]string{
						"propKey":       propKey,
						"handlerPrefix": opts.eventHandlerPrefix,
					}
					ctx.ReportNode(node, rule.RuleMessage{
						Id:          "badHandlerName",
						Description: applyData(msgBadHandlerName, data),
						Data:        data,
					})
				case propFnIsNamedCorrectly && opts.propEventHandlerRegex != nil && !propIsEventHandler:
					data := map[string]string{
						"propValue":         propValue,
						"handlerPropPrefix": opts.eventHandlerPropPrefix,
					}
					ctx.ReportNode(node, rule.RuleMessage{
						Id:          "badPropKey",
						Description: applyData(msgBadPropKey, data),
						Data:        data,
					})
				}
			},
		}
	},
}

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