no_will_update_set_state

package
v0.16.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2026 License: MIT, MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoWillUpdateSetStateRule = rule.Rule{
	Name: "react/no-will-update-set-state",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		disallowInFunc := parseDisallowInFunc(options)
		checkUnsafe := shouldCheckUnsafe(ctx.Settings)

		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 esTreeNameOf(prop.Name()) != "setState" {
					return
				}

				depth := 0
				for p := node.Parent; p != nil; p = p.Parent {
					if ast.IsFunctionLikeDeclaration(p) {
						depth++
					}
					if !isStopper(p) {
						continue
					}
					sn := stopperName(p)

					if sn != methodName && (!checkUnsafe || sn != unsafeMethodName) {
						continue
					}
					if !disallowInFunc && depth > 1 {
						continue
					}
					ctx.ReportNode(callee, rule.RuleMessage{
						Id:          "noSetState",
						Description: "Do not use setState in " + sn,
					})
					return
				}
			},
		}
	},
}

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