prefer_spread

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 PreferSpreadRule = rule.Rule{
	Name: "prefer-spread",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		return rule.RuleListeners{
			ast.KindCallExpression: func(node *ast.Node) {
				call := node.AsCallExpression()

				callee := ast.SkipParentheses(call.Expression)
				if !utils.IsSpecificMemberAccess(callee, "", "apply") {
					return
				}

				if call.Arguments == nil {
					return
				}
				args := call.Arguments.Nodes
				if len(args) != 2 {
					return
				}

				arg1 := ast.SkipParentheses(args[1])
				if arg1.Kind == ast.KindArrayLiteralExpression ||
					arg1.Kind == ast.KindSpreadElement {
					return
				}

				var memberObject *ast.Node
				switch callee.Kind {
				case ast.KindPropertyAccessExpression:
					memberObject = callee.AsPropertyAccessExpression().Expression
				case ast.KindElementAccessExpression:
					memberObject = callee.AsElementAccessExpression().Expression
				default:
					return
				}

				applied := ast.SkipParentheses(memberObject)
				var expectedThis *ast.Node
				switch applied.Kind {
				case ast.KindPropertyAccessExpression:
					expectedThis = applied.AsPropertyAccessExpression().Expression
				case ast.KindElementAccessExpression:
					expectedThis = applied.AsElementAccessExpression().Expression
				}

				thisArg := args[0]
				if !isValidThisArg(ctx.SourceFile, expectedThis, thisArg) {
					return
				}

				ctx.ReportNode(node, rule.RuleMessage{
					Id:          "preferSpread",
					Description: "Use the spread operator instead of '.apply()'.",
				})
			},
		}
	},
}

https://eslint.org/docs/latest/rules/prefer-spread

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