1
1
'use strict' ;
2
2
const { findVariable, getFunctionHeadLocation} = require ( '@eslint-community/eslint-utils' ) ;
3
- const { isFunction, isMemberExpression} = require ( './ast/index.js' ) ;
3
+ const { isFunction, isMemberExpression, isMethodCall } = require ( './ast/index.js' ) ;
4
4
5
5
const ERROR_PROMISE = 'promise' ;
6
6
const ERROR_IIFE = 'iife' ;
@@ -13,7 +13,7 @@ const messages = {
13
13
[ SUGGESTION_ADD_AWAIT ] : 'Insert `await`.' ,
14
14
} ;
15
15
16
- const promiseMethods = [ 'then' , 'catch' , 'finally' ] ;
16
+ const promisePrototypeMethods = [ 'then' , 'catch' , 'finally' ] ;
17
17
const isTopLevelCallExpression = node => {
18
18
if ( node . type !== 'CallExpression' ) {
19
19
return false ;
@@ -37,17 +37,28 @@ const isPromiseMethodCalleeObject = node =>
37
37
&& node . parent . object === node
38
38
&& ! node . parent . computed
39
39
&& node . parent . property . type === 'Identifier'
40
- && promiseMethods . includes ( node . parent . property . name )
40
+ && promisePrototypeMethods . includes ( node . parent . property . name )
41
41
&& node . parent . parent . type === 'CallExpression'
42
42
&& node . parent . parent . callee === node . parent ;
43
- const isAwaitArgument = node => {
43
+ const isAwaitExpressionArgument = node => {
44
44
if ( node . parent . type === 'ChainExpression' ) {
45
45
node = node . parent ;
46
46
}
47
47
48
48
return node . parent . type === 'AwaitExpression' && node . parent . argument === node ;
49
49
} ;
50
50
51
+ // `Promise.{all,allSettled,any,race}([foo()])`
52
+ const isInPromiseMethods = node =>
53
+ node . parent . type === 'ArrayExpression'
54
+ && node . parent . elements . includes ( node )
55
+ && isMethodCall ( node . parent . parent , {
56
+ object : 'Promise' ,
57
+ methods : [ 'all' , 'allSettled' , 'any' , 'race' ] ,
58
+ argumentsLength : 1 ,
59
+ } )
60
+ && node . parent . parent . arguments [ 0 ] === node . parent ;
61
+
51
62
/** @param {import('eslint').Rule.RuleContext } context */
52
63
function create ( context ) {
53
64
if ( context . getFilename ( ) . toLowerCase ( ) . endsWith ( '.cjs' ) ) {
@@ -59,14 +70,15 @@ function create(context) {
59
70
if (
60
71
! isTopLevelCallExpression ( node )
61
72
|| isPromiseMethodCalleeObject ( node )
62
- || isAwaitArgument ( node )
73
+ || isAwaitExpressionArgument ( node )
74
+ || isInPromiseMethods ( node )
63
75
) {
64
76
return ;
65
77
}
66
78
67
79
// Promises
68
80
if ( isMemberExpression ( node . callee , {
69
- properties : promiseMethods ,
81
+ properties : promisePrototypeMethods ,
70
82
computed : false ,
71
83
} ) ) {
72
84
return {
0 commit comments