no_done_callback

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoDoneCallbackRule = rule.Rule{
	Name: "jest/no-done-callback",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		return rule.RuleListeners{
			ast.KindCallExpression: func(node *ast.Node) {
				callExpr := node.AsCallExpression()
				if callExpr == nil {
					return
				}

				jestFnCall := utils.ParseJestFnCall(node, ctx)
				if jestFnCall == nil {
					return
				}

				callee := callExpr.Expression
				if callee == nil {
					return
				}

				isJestEach := slices.Contains(jestFnCall.Members, "each")
				if isJestEach && callee.Kind != ast.KindTaggedTemplateExpression {
					return
				}

				callback := findCallbackArgument(callExpr, jestFnCall, isJestEach)
				callbackArgIndex := boolToInt(isJestEach)
				if callback == nil ||
					!isFunction(callback) ||
					len(callback.Parameters()) == 0 ||
					len(callback.Parameters()) != 1+callbackArgIndex {
					return
				}

				params := callback.Parameters()
				argument := params[callbackArgIndex]
				paramDecl := argument.AsParameterDeclaration()
				nameNode := paramDecl.Name()
				if paramDecl.DotDotDotToken != nil ||
					nameNode == nil ||
					nameNode.Kind != ast.KindIdentifier {
					ctx.ReportNode(argument, buildErrorNoDoneCallbackMessage())
					return
				}

				if ast.IsAsyncFunction(callback) {
					ctx.ReportNode(argument, buildErrorUseAwaitInsteadOfCallbackMessage())
					return
				}

				body := callback.Body()
				if body == nil {
					ctx.ReportNode(argument, buildErrorNoDoneCallbackMessage())
					return
				}

				text := ctx.SourceFile.Text()

				paramListLoc := callback.FunctionLikeData().Parameters.Loc
				hasParens := paramListLoc.End() < len(text) && text[paramListLoc.End()] == ')'

				bodyStart := scanner.GetTokenPosOfNode(body, ctx.SourceFile, false)
				bodyEnd := body.End()
				bodyIsBlock := body.Kind == ast.KindBlock
				callbackName := nameNode.Text()

				var fixes []rule.RuleFix
				if hasParens {
					fixes = append(fixes, rule.RuleFixRemoveRange(paramListLoc))
				} else {
					firstParam := params[0]
					firstParamStart := scanner.GetTokenPosOfNode(firstParam, ctx.SourceFile, false)
					fixes = append(fixes, rule.RuleFixReplaceRange(
						core.NewTextRange(firstParamStart, firstParam.End()),
						"()",
					))
				}

				beforeReplacement := "new Promise(" + callbackName + " => "
				afterReplacement := ")"
				if bodyIsBlock {
					beforeReplacement = "return " + beforeReplacement + "{"
					afterReplacement += "}"
					fixes = append(fixes, rule.RuleFixReplaceRange(
						core.NewTextRange(bodyStart+1, bodyStart+1),
						beforeReplacement,
					))
				} else {
					fixes = append(fixes, rule.RuleFixReplaceRange(
						core.NewTextRange(bodyStart, bodyStart),
						beforeReplacement,
					))
				}
				fixes = append(fixes, rule.RuleFixReplaceRange(
					core.NewTextRange(bodyEnd, bodyEnd),
					afterReplacement,
				))

				ctx.ReportNodeWithSuggestions(
					argument,
					buildErrorNoDoneCallbackMessage(),
					rule.RuleSuggestion{
						Message:  buildErrorSuggestWrappingInPromiseMessage(callbackName),
						FixesArr: fixes,
					},
				)
			},
		}
	},
}

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