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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoSetStateRule = rule.Rule{
	Name: "react/no-set-state",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		pragma := reactutil.GetReactPragma(ctx.Settings)
		createClass := reactutil.GetReactCreateClass(ctx.Settings)
		wrappers := reactutil.GetComponentWrapperFunctions(ctx.Settings, pragma)

		return rule.RuleListeners{
			ast.KindCallExpression: func(node *ast.Node) {
				call := node.AsCallExpression()

				callee := ast.SkipParentheses(call.Expression)
				if callee.Kind != ast.KindPropertyAccessExpression {
					return
				}
				prop := callee.AsPropertyAccessExpression()

				if ast.SkipParentheses(prop.Expression).Kind != ast.KindThisKeyword {
					return
				}

				if reactutil.EsTreeName(prop.Name()) != "setState" {
					return
				}

				if reactutil.GetEnclosingReactComponentOrStateless(node, pragma, createClass, wrappers) == nil {
					return
				}

				ctx.ReportNode(callee, rule.RuleMessage{
					Id:          "noSetState",
					Description: "Do not use setState",
				})
			},
		}
	},
}

NoSetStateRule mirrors eslint-plugin-react's `no-set-state`: report any `this.setState(...)` call that lexically sits inside a detected React component (ES6 class extending Component / PureComponent, an `createReactClass` object literal, or a stateless functional component).

Upstream uses `Components.detect` + a deferred `Program:exit` pass that walks `components.list()` and reports each accumulated `setStateUsage`. We collapse that into one synchronous report per CallExpression — the observable contract is identical because:

  • upstream's `components.list()` only yields nodes its detection logic classified as components;
  • `GetEnclosingReactComponentOrStateless` mirrors that priority (getParentES6Component | getParentES5Component | getParentStatelessComponent);
  • upstream's `components.set(node, ...)` walks `node.parent` until it finds a node already in the registry, then merges. That's the same enclosing-walk we do here — no later observation can flip a component into "not a component", so there's no need to defer.

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