Skip to content

Commit eea92ec

Browse files
committed
fix(no-floating-promises): handle spread arguments before rejection handlers
1 parent 7131c61 commit eea92ec

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

internal/rule_tester/__snapshots__/no-floating-promises.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,3 +1672,15 @@ Help: The promise must end with a call to .catch, or end with a call to .then wi
16721672
Suggestion 1: [floatingFixVoid] Add void operator to ignore.
16731673
Suggestion 2: [floatingFixAwait] Add await operator.
16741674
---
1675+
1676+
[TestNoFloatingPromisesRule/invalid-100 - 1]
1677+
Diagnostic 1: floatingVoid (2:1 - 2:44)
1678+
Message: Promises must be awaited, add void operator to ignore.
1679+
Help: The promise must end with a call to .catch, or end with a call to .then with a rejection handler, or be explicitly marked as ignored with the `void` operator.
1680+
1 |
1681+
2 | Promise.reject('foo').then(...[], () => {});
1682+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1683+
3 |
1684+
Suggestion 1: [floatingFixVoid] Add void operator to ignore.
1685+
Suggestion 2: [floatingFixAwait] Add await operator.
1686+
---

internal/rules/no_floating_promises/no_floating_promises.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,18 @@ var NoFloatingPromisesRule = rule.Rule{
244244
return len(utils.GetCallSignatures(ctx.TypeChecker, ctx.TypeChecker.GetTypeAtLocation(rejectionHandler))) > 0
245245
}
246246

247+
isKnownArgumentAt := func(args *ast.ArgumentList, index int) bool {
248+
if len(args.Nodes) <= index {
249+
return false
250+
}
251+
for _, arg := range args.Nodes[:index+1] {
252+
if ast.IsSpreadElement(arg) {
253+
return false
254+
}
255+
}
256+
return true
257+
}
258+
247259
var isUnhandledPromise func(
248260
node *ast.Node,
249261
) (
@@ -315,12 +327,18 @@ var NoFloatingPromisesRule = rule.Rule{
315327
methodName, _ := checker.Checker_getAccessedPropertyName(ctx.TypeChecker, callee)
316328

317329
if methodName == "catch" && len(callExpr.Arguments.Nodes) >= 1 {
330+
if !isKnownArgumentAt(callExpr.Arguments, 0) {
331+
return true, false, false
332+
}
318333
if isValidRejectionHandler(callExpr.Arguments.Nodes[0]) {
319334
return false, false, false
320335
}
321336
return true, true, false
322337
}
323338
if methodName == "then" && len(callExpr.Arguments.Nodes) >= 2 {
339+
if !isKnownArgumentAt(callExpr.Arguments, 1) {
340+
return true, false, false
341+
}
324342
if isValidRejectionHandler(callExpr.Arguments.Nodes[1]) {
325343
return false, false, false
326344
}

internal/rules/no_floating_promises/no_floating_promises_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5582,7 +5582,6 @@ await Promise.reject('foo').finally(...[], () => {});
55825582
},
55835583
},
55845584
{
5585-
Skip: true,
55865585
Code: `
55875586
Promise.reject('foo').then(...[], () => {});
55885587
`,

0 commit comments

Comments
 (0)