only_throw_error

package
v0.1.8 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var OnlyThrowErrorRule = rule.CreateRule(rule.Rule{
	Name: "only-throw-error",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		opts, ok := options.(OnlyThrowErrorOptions)
		if !ok {
			opts = OnlyThrowErrorOptions{}
		}
		if opts.Allow == nil {
			opts.Allow = []utils.TypeOrValueSpecifier{}
		}
		if opts.AllowInline == nil {
			opts.AllowInline = []string{}
		}
		if opts.AllowThrowingAny == nil {
			opts.AllowThrowingAny = utils.Ref(true)
		}
		if opts.AllowThrowingUnknown == nil {
			opts.AllowThrowingUnknown = utils.Ref(true)
		}

		return rule.RuleListeners{
			ast.KindThrowStatement: func(node *ast.Node) {
				expr := node.Expression()

				t := ctx.TypeChecker.GetTypeAtLocation(expr)

				if utils.TypeMatchesSomeSpecifier(t, opts.Allow, opts.AllowInline, ctx.Program) {
					return
				}

				if utils.IsTypeFlagSet(t, checker.TypeFlagsUndefined) {
					ctx.ReportNode(node, buildUndefMessage())
					return
				}

				if *opts.AllowThrowingAny && utils.IsTypeAnyType(t) {
					return
				}

				if *opts.AllowThrowingUnknown && utils.IsTypeUnknownType(t) {
					return
				}

				if utils.IsErrorLike(ctx.Program, ctx.TypeChecker, t) {
					return
				}

				ctx.ReportNode(expr, buildObjectMessage())
			},
		}
	},
})

Functions

This section is empty.

Types

type OnlyThrowErrorOptions

type OnlyThrowErrorOptions struct {
	Allow                []utils.TypeOrValueSpecifier
	AllowInline          []string
	AllowThrowingAny     *bool
	AllowThrowingUnknown *bool
}

Jump to

Keyboard shortcuts

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