require_array_sort_compare

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RequireArraySortCompareRule = rule.CreateRule(rule.Rule{
	Name: "require-array-sort-compare",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		opts, ok := options.(RequireArraySortCompareOptions)
		if !ok {
			opts = RequireArraySortCompareOptions{}
		}
		if opts.IgnoreStringArrays == nil {
			opts.IgnoreStringArrays = utils.Ref(true)
		}

		return rule.RuleListeners{
			ast.KindCallExpression: func(node *ast.Node) {
				expr := node.AsCallExpression()
				if len(expr.Arguments.Nodes) != 0 {
					return
				}
				callee := expr.Expression

				if !ast.IsAccessExpression(callee) {
					return
				}

				if propertyName, found := checker.Checker_getAccessedPropertyName(ctx.TypeChecker, callee); !found || (propertyName != "sort" && propertyName != "toSorted") {
					return
				}

				calleeObjType := utils.GetConstrainedTypeAtLocation(ctx.TypeChecker, callee.Expression())

				if *opts.IgnoreStringArrays && checker.Checker_isArrayOrTupleType(ctx.TypeChecker, calleeObjType) {
					if utils.Every(checker.Checker_getTypeArguments(ctx.TypeChecker, calleeObjType), func(t *checker.Type) bool {
						return utils.IsTypeFlagSet(t, checker.TypeFlagsString)
					}) {
						return
					}
				}

				if utils.Every(utils.UnionTypeParts(calleeObjType), func(t *checker.Type) bool {
					return checker.Checker_isArrayOrTupleType(ctx.TypeChecker, t)
				}) {
					ctx.ReportNode(node, buildRequireCompareMessage())
				}
			},
		}
	},
})

Functions

This section is empty.

Types

type RequireArraySortCompareOptions

type RequireArraySortCompareOptions struct {
	IgnoreStringArrays *bool
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL