no_async_promise_executor

package
v0.1.14 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoAsyncPromiseExecutorRule = rule.CreateRule(rule.Rule{
	Name: "no-async-promise-executor",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		return rule.RuleListeners{
			ast.KindNewExpression: func(node *ast.Node) {
				if node == nil {
					return
				}

				expr := node.Expression()
				if expr == nil || expr.Kind != ast.KindIdentifier {
					return
				}

				if expr.Text() != "Promise" {
					return
				}

				args := node.Arguments()
				if len(args) == 0 {
					return
				}

				executor := args[0]
				if executor == nil {
					return
				}

				actualExecutor := executor
				for actualExecutor != nil && actualExecutor.Kind == ast.KindParenthesizedExpression {
					actualExecutor = actualExecutor.Expression()
				}

				if actualExecutor == nil {
					return
				}

				// Check if the executor is an async function
				var isAsync bool
				var asyncKeywordNode *ast.Node

				switch actualExecutor.Kind {
				case ast.KindFunctionExpression:

					mods := actualExecutor.Modifiers()
					if mods != nil {
						for _, mod := range mods.Nodes {
							if mod != nil && mod.Kind == ast.KindAsyncKeyword {
								isAsync = true
								asyncKeywordNode = mod
								break
							}
						}
					}

				case ast.KindArrowFunction:

					mods := actualExecutor.Modifiers()
					if mods != nil {
						for _, mod := range mods.Nodes {
							if mod != nil && mod.Kind == ast.KindAsyncKeyword {
								isAsync = true
								asyncKeywordNode = mod
								break
							}
						}
					}
				}

				if isAsync && asyncKeywordNode != nil {
					ctx.ReportNode(asyncKeywordNode, buildAsyncMessage())
				}
			},
		}
	},
})

NoAsyncPromiseExecutorRule disallows using an async function as a Promise executor

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