prefer_promise_reject_errors

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 PreferPromiseRejectErrorsRule = rule.Rule{
	Name: "prefer-promise-reject-errors",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		opts := parseOptions(options)
		return rule.RuleListeners{
			ast.KindCallExpression: func(node *ast.Node) {
				if utils.IsSpecificMemberAccess(node.AsCallExpression().Expression, "Promise", "reject") {
					checkRejectCall(ctx, node, opts.AllowEmptyReject)
				}
			},
			ast.KindNewExpression: func(node *ast.Node) {

				callee := ast.SkipParentheses(node.AsNewExpression().Expression)
				if callee == nil || !ast.IsIdentifier(callee) || callee.AsIdentifier().Text != "Promise" {
					return
				}
				args := node.Arguments()
				if len(args) == 0 {
					return
				}

				executor := ast.SkipParentheses(args[0])
				if executor == nil || !ast.IsFunctionExpressionOrArrowFunction(executor) {
					return
				}
				params := executor.Parameters()
				if len(params) < 2 {
					return
				}

				rejectParam := params[1].AsParameterDeclaration()
				if rejectParam == nil || rejectParam.Initializer != nil || rejectParam.DotDotDotToken != nil {
					return
				}
				rejectName := rejectParam.Name()
				if rejectName == nil || !ast.IsIdentifier(rejectName) {
					return
				}
				name := rejectName.AsIdentifier().Text

				findRejectReferences(ctx, executor, name, func(call *ast.Node) {
					checkRejectCall(ctx, call, opts.AllowEmptyReject)
				})
			},
		}
	},
}

Functions

This section is empty.

Types

type Options added in v0.5.0

type Options struct {
	AllowEmptyReject bool
}

Jump to

Keyboard shortcuts

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