no_unused_state

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 NoUnusedStateRule = rule.Rule{
	Name: "react/no-unused-state",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		pragma := reactutil.GetReactPragma(ctx.Settings)
		createClass := reactutil.GetReactCreateClass(ctx.Settings)

		processComponent := func(ci *classInfo) {
			if ci.abandoned {
				return
			}
			for _, field := range ci.stateFields {
				if !ci.usedFields[field.name] {
					ctx.ReportNode(field.node, rule.RuleMessage{
						Id:          "unusedStateField",
						Description: fmt.Sprintf("Unused state field: '%s'", field.name),
					})
				}
			}
		}

		return rule.RuleListeners{
			ast.KindClassDeclaration: func(node *ast.Node) {
				if !reactutil.ExtendsReactComponent(node, pragma) {
					return
				}
				ci := newClassInfo(ctx.TypeChecker)
				walkES6Component(ci, node)
				processComponent(ci)
			},
			ast.KindClassExpression: func(node *ast.Node) {
				if !reactutil.ExtendsReactComponent(node, pragma) {
					return
				}
				ci := newClassInfo(ctx.TypeChecker)
				walkES6Component(ci, node)
				processComponent(ci)
			},
			ast.KindCallExpression: func(node *ast.Node) {
				call := node.AsCallExpression()
				if !reactutil.IsCreateClassCall(call, pragma, createClass) {
					return
				}
				if call.Arguments == nil || len(call.Arguments.Nodes) == 0 {
					return
				}
				arg := ast.SkipParentheses(call.Arguments.Nodes[0])
				if arg.Kind != ast.KindObjectLiteralExpression {
					return
				}
				ci := newClassInfo(ctx.TypeChecker)
				walkES5Component(ci, arg)
				processComponent(ci)
			},
		}
	},
}

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