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

Documentation

Index

Constants

This section is empty.

Variables

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

		report := func(node *ast.Node) {
			ctx.ReportNode(node, rule.RuleMessage{
				Id:          "noDirectMutation",
				Description: "Do not mutate state directly. Use setState().",
			})
		}

		check := func(node, target, reportNode *ast.Node) {
			target = ast.SkipParentheses(target)
			switch target.Kind {
			case ast.KindPropertyAccessExpression, ast.KindElementAccessExpression:
			default:
				return
			}
			innermost := innermostMemberExpression(target)
			if !isThisStateMember(innermost) {
				return
			}
			component := reactutil.GetEnclosingReactComponentOrStateless(node, pragma, createClass)
			if component == nil {
				return
			}
			if inConstructorExemption(node, component) {
				return
			}
			report(reportNode)
		}

		return rule.RuleListeners{
			ast.KindBinaryExpression: func(node *ast.Node) {
				bin := node.AsBinaryExpression()
				if bin.OperatorToken == nil || !ast.IsAssignmentOperator(bin.OperatorToken.Kind) {
					return
				}
				left := ast.SkipParentheses(bin.Left)
				// Report position mirrors ESLint's `node.left.object` — the
				// direct base of the assignment's LHS (e.g. `this.state` for
				// `this.state.foo = ...`, or `this` for `this.state = ...`).
				var reportNode *ast.Node
				switch left.Kind {
				case ast.KindPropertyAccessExpression:
					reportNode = left.AsPropertyAccessExpression().Expression
				case ast.KindElementAccessExpression:
					reportNode = left.AsElementAccessExpression().Expression
				default:
					return
				}
				check(node, left, reportNode)
			},

			ast.KindPrefixUnaryExpression: func(node *ast.Node) {
				pf := node.AsPrefixUnaryExpression()
				if pf.Operator != ast.KindPlusPlusToken && pf.Operator != ast.KindMinusMinusToken {
					return
				}
				operand := ast.SkipParentheses(pf.Operand)

				switch operand.Kind {
				case ast.KindPropertyAccessExpression, ast.KindElementAccessExpression:
				default:
					return
				}
				check(node, operand, innermostMemberExpression(operand))
			},

			ast.KindPostfixUnaryExpression: func(node *ast.Node) {
				pf := node.AsPostfixUnaryExpression()
				if pf.Operator != ast.KindPlusPlusToken && pf.Operator != ast.KindMinusMinusToken {
					return
				}
				operand := ast.SkipParentheses(pf.Operand)
				switch operand.Kind {
				case ast.KindPropertyAccessExpression, ast.KindElementAccessExpression:
				default:
					return
				}
				check(node, operand, innermostMemberExpression(operand))
			},
		}
	},
}

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