Skip to content

Commit fe03d99

Browse files
committed
no-single-promise-in-promise-methods: Suggest instead of fix when result is stored in array (#2385)
1 parent 37e00dd commit fe03d99

File tree

4 files changed

+97
-2
lines changed

4 files changed

+97
-2
lines changed

rules/no-single-promise-in-promise-methods.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ const isPromiseMethodCallWithSingleElementArray = node =>
3333
&& node.arguments[0].elements[0]
3434
&& node.arguments[0].elements[0].type !== 'SpreadElement';
3535

36+
const isStoredInArray = node => isMethodCall(node, {methods: ['all']})
37+
&& ['VariableDeclarator', 'AssignmentExpression'].includes(node.parent.parent.type);
38+
3639
const unwrapAwaitedCallExpression = (callExpression, sourceCode) => fixer => {
3740
const [promiseNode] = callExpression.arguments[0].elements;
3841
let text = getParenthesizedText(promiseNode, sourceCode);
@@ -133,7 +136,15 @@ const create = context => ({
133136
callExpression.parent.type === 'AwaitExpression'
134137
&& callExpression.parent.argument === callExpression
135138
) {
136-
problem.fix = unwrapAwaitedCallExpression(callExpression, sourceCode);
139+
if (isStoredInArray(callExpression)) {
140+
problem.suggest = [{
141+
messageId: MESSAGE_ID_SUGGESTION_UNWRAP,
142+
fix: unwrapAwaitedCallExpression(callExpression, sourceCode),
143+
}];
144+
} else {
145+
problem.fix = unwrapAwaitedCallExpression(callExpression, sourceCode);
146+
}
147+
137148
return problem;
138149
}
139150

test/no-single-promise-in-promise-methods.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ test.snapshot({
3939
'await Promise.race([promise])',
4040
'await Promise.all([new Promise(() => {})])',
4141
'+await Promise.all([+1])',
42+
'const results = await Promise.all([promise])',
43+
'results = await Promise.all([promise])',
44+
'const results = await Promise.any([promise])',
45+
'const results = await Promise.race([promise])',
4246

4347
// ASI, `Promise.all()` is not really `await`ed
4448
outdent`

test/snapshots/no-single-promise-in-promise-methods.mjs.md

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,87 @@ Generated by [AVA](https://avajs.dev).
655655
| ^^^^ Wrapping single-element array with \`Promise.all()\` is unnecessary.␊
656656
`
657657

658-
## invalid(32): await Promise.all([(x,y)]) [0].toString()
658+
## invalid(32): const results = await Promise.all([promise])
659+
660+
> Input
661+
662+
`␊
663+
1 | const results = await Promise.all([promise])␊
664+
`
665+
666+
> Error 1/1
667+
668+
`␊
669+
> 1 | const results = await Promise.all([promise])␊
670+
| ^^^^^^^^^ Wrapping single-element array with \`Promise.all()\` is unnecessary.␊
671+
672+
--------------------------------------------------------------------------------␊
673+
Suggestion 1/1: Use the value directly.␊
674+
1 | const results = await promise␊
675+
`
676+
677+
## invalid(33): results = await Promise.all([promise])
678+
679+
> Input
680+
681+
`␊
682+
1 | results = await Promise.all([promise])␊
683+
`
684+
685+
> Error 1/1
686+
687+
`␊
688+
> 1 | results = await Promise.all([promise])␊
689+
| ^^^^^^^^^ Wrapping single-element array with \`Promise.all()\` is unnecessary.␊
690+
691+
--------------------------------------------------------------------------------␊
692+
Suggestion 1/1: Use the value directly.␊
693+
1 | results = await promise␊
694+
`
695+
696+
## invalid(34): const results = await Promise.any([promise])
697+
698+
> Input
699+
700+
`␊
701+
1 | const results = await Promise.any([promise])␊
702+
`
703+
704+
> Output
705+
706+
`␊
707+
1 | const results = await promise␊
708+
`
709+
710+
> Error 1/1
711+
712+
`␊
713+
> 1 | const results = await Promise.any([promise])␊
714+
| ^^^^^^^^^ Wrapping single-element array with \`Promise.any()\` is unnecessary.␊
715+
`
716+
717+
## invalid(35): const results = await Promise.race([promise])
718+
719+
> Input
720+
721+
`␊
722+
1 | const results = await Promise.race([promise])␊
723+
`
724+
725+
> Output
726+
727+
`␊
728+
1 | const results = await promise␊
729+
`
730+
731+
> Error 1/1
732+
733+
`␊
734+
> 1 | const results = await Promise.race([promise])␊
735+
| ^^^^^^^^^ Wrapping single-element array with \`Promise.race()\` is unnecessary.␊
736+
`
737+
738+
## invalid(36): await Promise.all([(x,y)]) [0].toString()
659739

660740
> Input
661741
174 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)