-
Notifications
You must be signed in to change notification settings - Fork 24
feat: add promise/no-nesting rule #1056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
internal/plugins/promise/rules/no_nesting/no_nesting.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| package no_nesting | ||
|
|
||
| import ( | ||
| "github.com/microsoft/typescript-go/shim/ast" | ||
| "github.com/web-infra-dev/rslint/internal/plugins/promise/promiseutil" | ||
| "github.com/web-infra-dev/rslint/internal/rule" | ||
| "github.com/web-infra-dev/rslint/internal/utils" | ||
| ) | ||
|
|
||
| const skipTransparent = ast.OEKParentheses | ||
|
|
||
| // isThenOrCatchCall reports whether node is a call whose callee is a non-computed | ||
| // .then or .catch member access. Mirrors eslint-plugin-promise's has-promise-callback. | ||
| func isThenOrCatchCall(node *ast.Node) bool { | ||
| return promiseutil.IsMemberCall(node, "then") || promiseutil.IsMemberCall(node, "catch") | ||
| } | ||
|
|
||
| // isPromiseCallback reports whether node is a FunctionExpression or ArrowFunction | ||
| // directly passed as argument to a .then() or .catch() call. | ||
| // Mirrors eslint-plugin-promise's lib/is-inside-promise helper. | ||
| func isPromiseCallback(node *ast.Node) bool { | ||
| if node.Kind != ast.KindFunctionExpression && node.Kind != ast.KindArrowFunction { | ||
| return false | ||
| } | ||
| // In tsgo, an argument node's parent is the containing CallExpression. | ||
| parent := node.Parent | ||
| for parent != nil && ast.IsOuterExpression(parent, skipTransparent) { | ||
| parent = parent.Parent | ||
| } | ||
| return isThenOrCatchCall(parent) | ||
| } | ||
|
|
||
| // walkIdentifiers calls fn for each identifier node found anywhere in node's | ||
| // subtree, stopping early across the whole traversal if fn returns true. | ||
| func walkIdentifiers(node *ast.Node, fn func(identNode *ast.Node) bool) bool { | ||
| if node == nil { | ||
| return false | ||
| } | ||
| if node.Kind == ast.KindIdentifier { | ||
| return fn(node) | ||
| } | ||
| found := false | ||
| node.ForEachChild(func(child *ast.Node) bool { | ||
| if walkIdentifiers(child, fn) { | ||
| found = true | ||
| return true | ||
| } | ||
| return false | ||
| }) | ||
| return found | ||
| } | ||
|
|
||
| // argsContainRef reports whether any identifier anywhere in the call's argument | ||
| // list has a name that is bound in fn's parameter list or local variable | ||
| // declarations. Uses utils.HasShadowingParameter for params and | ||
| // utils.HasShadowingDeclaration / utils.HasHoistedVarDeclaration for body | ||
| // declarations, mirroring the upstream's scope-reference check. | ||
| func argsContainRef(callNode *ast.Node, fn *ast.Node) bool { | ||
| args := callNode.AsCallExpression().Arguments | ||
| if args == nil { | ||
| return false | ||
| } | ||
| body := fn.Body() | ||
| boundary := fn | ||
| if body != nil { | ||
| boundary = body | ||
| } | ||
| for _, arg := range args.Nodes { | ||
| found := false | ||
| walkIdentifiers(arg, func(identNode *ast.Node) bool { | ||
| if utils.IsNonReferenceIdentifier(identNode) { | ||
| return false | ||
| } | ||
|
|
||
| name := identNode.AsIdentifier().Text | ||
|
|
||
| if name == "arguments" { | ||
| if utils.IsNameShadowedBetween(identNode, boundary, name) { | ||
| return false | ||
| } | ||
| isArgumentsShadowed := false | ||
| for curr := identNode.Parent; curr != nil && curr != fn; curr = curr.Parent { | ||
| if ast.IsFunctionLikeDeclaration(curr) && curr.Kind != ast.KindArrowFunction { | ||
| isArgumentsShadowed = true | ||
| break | ||
| } | ||
| } | ||
| if isArgumentsShadowed { | ||
| return false | ||
| } | ||
| if fn.Kind != ast.KindArrowFunction { | ||
| found = true | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| hasParam := utils.HasShadowingParameter(fn, name) | ||
| hasDecl := body != nil && (utils.HasShadowingDeclaration(body, name) || utils.HasHoistedVarDeclaration(body, name)) | ||
| if !hasParam && !hasDecl { | ||
| return false | ||
| } | ||
|
|
||
| if utils.IsNameShadowedBetween(identNode, boundary, name) { | ||
| return false | ||
| } | ||
|
|
||
| found = true | ||
| return true | ||
| }) | ||
| if found { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func buildAvoidNestingMessage() rule.RuleMessage { | ||
| return rule.RuleMessage{ | ||
| Id: "avoidNesting", | ||
| Description: "Avoid nesting promises.", | ||
| } | ||
| } | ||
|
|
||
| var NoNestingRule = rule.Rule{ | ||
| Name: "promise/no-nesting", | ||
| Run: func(ctx rule.RuleContext, options any) rule.RuleListeners { | ||
| // Stack of promise-callback function nodes, closest last. | ||
| callbackStack := []*ast.Node{} | ||
|
|
||
| onEnter := func(node *ast.Node) { | ||
| if isPromiseCallback(node) { | ||
| callbackStack = append(callbackStack, node) | ||
| } | ||
| } | ||
| onExit := func(node *ast.Node) { | ||
| if isPromiseCallback(node) && len(callbackStack) > 0 { | ||
| callbackStack = callbackStack[:len(callbackStack)-1] | ||
| } | ||
| } | ||
|
|
||
| return rule.RuleListeners{ | ||
| ast.KindFunctionExpression: onEnter, | ||
| ast.KindArrowFunction: onEnter, | ||
| rule.ListenerOnExit(ast.KindFunctionExpression): onExit, | ||
| rule.ListenerOnExit(ast.KindArrowFunction): onExit, | ||
|
|
||
| ast.KindCallExpression: func(node *ast.Node) { | ||
| if !isThenOrCatchCall(node) || len(callbackStack) == 0 { | ||
| return | ||
| } | ||
| closestCallback := callbackStack[len(callbackStack)-1] | ||
| if argsContainRef(node, closestCallback) { | ||
| return | ||
| } | ||
| callee := ast.SkipOuterExpressions(node.AsCallExpression().Expression, skipTransparent) | ||
| if callee == nil || !ast.IsPropertyAccessExpression(callee) { | ||
| return | ||
| } | ||
| nameNode := callee.AsPropertyAccessExpression().Name() | ||
| if nameNode != nil { | ||
| ctx.ReportNode(nameNode, buildAvoidNestingMessage()) | ||
| } | ||
| }, | ||
| } | ||
| }, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # promise/no-nesting | ||
|
|
||
| ## Rule Details | ||
|
|
||
| Disallow nesting `.then()` or `.catch()` statements inside promise callbacks when the | ||
| inner call does not depend on variables introduced by the enclosing callback. | ||
|
|
||
| Deeply nested promise chains are harder to read and can usually be rewritten as a flat | ||
| chain. This rule flags the inner `.then()` / `.catch()` when its arguments do not | ||
| reference any binding (parameter or local variable) that belongs to the immediately | ||
| enclosing promise callback, because in that case the nesting is unnecessary. | ||
|
|
||
| Examples of **incorrect** code for this rule: | ||
|
|
||
| ```javascript | ||
| doThing().then(function () { | ||
| return a.then(); | ||
| }); | ||
|
|
||
| doThing().then(() => b.catch()); | ||
|
|
||
| doThing().then(function () { | ||
| return a.then(function () { | ||
| return b.catch(); | ||
| }); | ||
| }); | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule: | ||
|
|
||
| ```javascript | ||
| // Flat chain — no nesting | ||
| doThing().then(function () { | ||
| return 4; | ||
| }); | ||
|
|
||
| // Inner call uses a closure variable from the enclosing callback — cannot be flattened | ||
| doThing().then((a) => getB(a).then((b) => getC(a, b))); | ||
|
|
||
| // Promise.resolve / Promise.all inside a callback is fine | ||
| doThing().then(function () { | ||
| return Promise.resolve(4); | ||
| }); | ||
| ``` | ||
|
|
||
| ## Original Documentation | ||
|
|
||
| https://github.com/eslint-community/eslint-plugin-promise/blob/main/docs/rules/no-nesting.md |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.