no_children_prop

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: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoChildrenPropRule = rule.Rule{
	Name: "react/no-children-prop",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		allowFunctions := false
		if optsMap := utils.GetOptionsMap(options); optsMap != nil {
			if v, ok := optsMap["allowFunctions"].(bool); ok {
				allowFunctions = v
			}
		}

		isAllowedFunction := func(node *ast.Node) bool {
			if !allowFunctions || node == nil {
				return false
			}
			return ast.IsFunctionExpressionOrArrowFunction(ast.SkipParentheses(node))
		}

		pragma := reactutil.GetReactPragma(ctx.Settings)

		return rule.RuleListeners{
			ast.KindJsxAttribute: func(node *ast.Node) {
				attr := node.AsJsxAttribute()
				nameNode := attr.Name()
				if nameNode == nil || nameNode.Kind != ast.KindIdentifier {
					return
				}
				if nameNode.AsIdentifier().Text != "children" {
					return
				}
				if initializer := attr.Initializer; initializer != nil && initializer.Kind == ast.KindJsxExpression {
					if expr := initializer.AsJsxExpression().Expression; isAllowedFunction(expr) {
						return
					}
				}
				ctx.ReportNode(node, rule.RuleMessage{
					Id:          "nestChildren",
					Description: msgNestChildren,
				})
			},
			ast.KindCallExpression: func(node *ast.Node) {
				call := node.AsCallExpression()
				if !reactutil.IsCreateElementCall(call.Expression, pragma) {
					return
				}
				if call.Arguments == nil || len(call.Arguments.Nodes) < 2 {
					return
				}
				secondArg := ast.SkipParentheses(call.Arguments.Nodes[1])
				if secondArg.Kind != ast.KindObjectLiteralExpression {
					return
				}
				obj := secondArg.AsObjectLiteralExpression()
				childrenValue, hasChildrenProp := findChildrenValue(obj)
				if hasChildrenProp {

					if childrenValue == nil || isAllowedFunction(childrenValue) {
						return
					}
					ctx.ReportNode(node, rule.RuleMessage{
						Id:          "passChildrenAsArgs",
						Description: msgPassChildrenAsArgs,
					})
					return
				}

				if len(call.Arguments.Nodes) == 3 && isAllowedFunction(call.Arguments.Nodes[2]) {
					ctx.ReportNode(node, rule.RuleMessage{
						Id:          "passFunctionAsArgs",
						Description: msgPassFunctionAsArgs,
					})
				}
			},
			ast.KindJsxElement: func(node *ast.Node) {
				jsx := node.AsJsxElement()
				if jsx.Children == nil || len(jsx.Children.Nodes) != 1 {
					return
				}
				child := jsx.Children.Nodes[0]
				if child.Kind != ast.KindJsxExpression {
					return
				}
				if !isAllowedFunction(child.AsJsxExpression().Expression) {
					return
				}
				ctx.ReportNode(node, rule.RuleMessage{
					Id:          "nestFunction",
					Description: msgNestFunction,
				})
			},
		}
	},
}

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