prefer_stateless_function

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var PreferStatelessFunctionRule = rule.Rule{
	Name: "react/prefer-stateless-function",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		opts := parseOptions(options)
		pragma := reactutil.GetReactPragma(ctx.Settings)
		createClass := reactutil.GetReactCreateClass(ctx.Settings)

		allowNull := !reactutil.ReactVersionLessThan(ctx.Settings, 15, 0, 0)

		externalCCT := collectExternalChildContextTypes(ctx.SourceFile, ctx.TypeChecker)

		hasExternalCCT := func(componentNode *ast.Node) bool {
			if ctx.TypeChecker != nil {
				if sym := componentBindingSymbol(ctx.TypeChecker, componentNode); sym != nil {
					return externalCCT.hasSymbol(sym)
				}

			}
			return externalCCT.hasName(componentBindingName(componentNode))
		}

		runOnClass := func(node *ast.Node) {
			if !reactutil.ExtendsReactComponent(node, pragma) {
				return
			}
			flags := &componentFlags{}
			if opts.IgnorePureComponents && reactutil.ExtendsReactPureComponent(node, pragma) {
				flags.hasSCU = true
			}
			analyzeES6Class(node, flags, pragma, createClass, allowNull)
			if hasExternalCCT(node) {
				flags.hasChildContextTypes = true
			}
			if shouldReport(flags) {

				start := reactutil.ClassKeywordStart(ctx.SourceFile.Text(), node)
				ctx.ReportRange(core.NewTextRange(start, node.End()), rule.RuleMessage{
					Id:          "componentShouldBePure",
					Description: "Component should be written as a pure function",
				})
			}
		}

		return rule.RuleListeners{
			ast.KindClassDeclaration: runOnClass,
			ast.KindClassExpression:  runOnClass,

			ast.KindObjectLiteralExpression: func(node *ast.Node) {
				if !isES5Component(node, pragma, createClass) {
					return
				}
				flags := &componentFlags{}
				analyzeES5Object(node, flags, pragma, createClass, allowNull)
				if hasExternalCCT(node) {
					flags.hasChildContextTypes = true
				}
				if shouldReport(flags) {
					ctx.ReportNode(node, rule.RuleMessage{
						Id:          "componentShouldBePure",
						Description: "Component should be written as a pure function",
					})
				}
			},
		}
	},
}

Functions

This section is empty.

Types

type Options

type Options struct {
	IgnorePureComponents bool
}

Options carries the user-supplied options for this rule.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL