no_unsafe_call

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoUnsafeCallRule = rule.CreateRule(rule.Rule{
	Name: "no-unsafe-call",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		compilerOptions := ctx.Program.Options()
		isNoImplicitThis := utils.IsStrictCompilerOptionEnabled(
			compilerOptions,
			compilerOptions.NoImplicitThis,
		)

		checkCall := func(
			node *ast.Node,
			reportingNode *ast.Node,
			messageBuilder func(t string) rule.RuleMessage,
			newCall bool,
		) {
			t := utils.GetConstrainedTypeAtLocation(ctx.TypeChecker, node)

			if utils.IsTypeAnyType(t) {
				if !isNoImplicitThis {

					thisExpression := utils.GetThisExpression(node)
					if thisExpression != nil && utils.IsTypeAnyType(
						utils.GetConstrainedTypeAtLocation(ctx.TypeChecker, thisExpression),
					) {
						messageBuilder = buildUnsafeCallThisMessage
					}
				}

				isErrorType := utils.IsIntrinsicErrorType(t)

				msg := "`any`"
				if isErrorType {
					msg = "`error` type"
				}
				ctx.ReportNode(reportingNode, messageBuilder(msg))
				return
			}

			if utils.IsBuiltinSymbolLike(ctx.Program, ctx.TypeChecker, t, "Function") {

				constructSignatures := utils.GetConstructSignatures(ctx.TypeChecker, t)
				if len(constructSignatures) > 0 {
					return
				}

				callSignatures := utils.GetCallSignatures(ctx.TypeChecker, t)
				if newCall {
					if utils.Some(callSignatures, func(signature *checker.Signature) bool {
						return !utils.IsIntrinsicVoidType(checker.Checker_getReturnTypeOfSignature(ctx.TypeChecker, signature))
					}) {
						return
					}
				} else if len(callSignatures) > 0 {
					return
				}

				ctx.ReportNode(reportingNode, messageBuilder("`Function`"))
				return
			}
		}

		return rule.RuleListeners{
			ast.KindCallExpression: func(node *ast.Node) {
				callee := node.Expression()
				if callee.Kind == ast.KindImportKeyword {
					return
				}
				checkCall(callee, callee, buildUnsafeCallMessage, false)
			},
			ast.KindNewExpression: func(node *ast.Node) {
				callee := node.Expression()
				checkCall(callee, node, buildUnsafeNewMessage, true)
			},
			ast.KindTaggedTemplateExpression: func(node *ast.Node) {
				tag := node.AsTaggedTemplateExpression().Tag
				checkCall(tag, tag, buildUnsafeTemplateTagMessage, false)
			},
		}
	},
})

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