no_unstable_nested_components

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoUnstableNestedComponentsRule = rule.Rule{
	Name: "react/no-unstable-nested-components",
	Run: func(ctx rule.RuleContext, rawOptions any) rule.RuleListeners {
		opts := parseOptions(rawOptions)

		env := componentEnv{
			pragma:      reactutil.GetReactPragma(ctx.Settings),
			createClass: reactutil.GetReactCreateClass(ctx.Settings),
			tc:          ctx.TypeChecker,
		}

		env.wrappers = reactutil.GetComponentWrapperFunctions(ctx.Settings, env.pragma)

		reported := map[int]struct{}{}

		validate := func(node *ast.Node) {
			if node == nil || node.Parent == nil {
				return
			}

			switch node.Kind {
			case ast.KindMethodDeclaration, ast.KindGetAccessor, ast.KindSetAccessor:
				if node.Parent.Kind != ast.KindObjectLiteralExpression {
					return
				}
			}

			if reactutil.IsFunctionLikeForComponent(node) && isInsideWrapperCall(node, env) {
				return
			}

			isDeclaredInsideProps := isComponentInProp(node, env)
			isComponent := isDetectedComponent(node, env)
			isFnInClass := !isComponent && !isDeclaredInsideProps && isFunctionComponentInsideClassComponent(node, env)

			if !isComponent && !isDeclaredInsideProps && !isFnInClass {
				return
			}

			if isDeclaredInsideProps && (opts.allowAsProps || isComponentInRenderProp(node, opts.propNamePattern)) {
				return
			}

			if isMapCall(node) || isMapCall(reactutil.SkipExpressionWrappersUp(node)) {
				return
			}
			if isReturnStatementOfHook(node) {
				return
			}
			if isDirectValueOfRenderProperty(node, opts.propNamePattern) {
				return
			}
			if isStatelessComponentReturningNull(node, env) {
				return
			}

			parentComponent := getClosestParentComponent(node, env)
			if parentComponent == nil {
				return
			}

			parentName := resolveComponentName(parentComponent)

			if parentName != "" && reactutil.IsLowercaseFirstLetter(parentName) {
				return
			}

			if isOptionalPragmaWrapperCall(node) {
				parentName = ""
			}

			pos := node.Pos()
			if _, seen := reported[pos]; seen {
				return
			}
			reported[pos] = struct{}{}

			msg := generateErrorMessageWithParentName(parentName)
			if isDeclaredInsideProps && !opts.allowAsProps {
				msg += asPropsInfo
			}

			ruleMsg := rule.RuleMessage{Id: "", Description: msg}

			if reactutil.IsObjectLiteralShorthandFunction(node) {
				if start := reactutil.ParamListOpenParenPos(ctx.SourceFile, node); start >= 0 {
					ctx.ReportRange(core.NewTextRange(start, node.End()), ruleMsg)
					return
				}
			}
			ctx.ReportNode(node, ruleMsg)
		}

		return rule.RuleListeners{
			ast.KindFunctionDeclaration: validate,
			ast.KindFunctionExpression:  validate,
			ast.KindArrowFunction:       validate,
			ast.KindMethodDeclaration:   validate,
			ast.KindGetAccessor:         validate,
			ast.KindSetAccessor:         validate,
			ast.KindClassDeclaration:    validate,
			ast.KindCallExpression:      validate,
		}
	},
}

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