@@ -11,33 +11,29 @@ const utils = require('../utils')
11
11
* @typedef {import('../utils').ComponentComputedProperty } ComponentComputedProperty
12
12
*/
13
13
14
- const PROMISE_FUNCTIONS = [ 'then' , 'catch' , 'finally' ]
14
+ const PROMISE_FUNCTIONS = new Set ( [ 'then' , 'catch' , 'finally' ] )
15
15
16
- const PROMISE_METHODS = [ 'all' , 'race' , 'reject' , 'resolve' ]
16
+ const PROMISE_METHODS = new Set ( [ 'all' , 'race' , 'reject' , 'resolve' ] )
17
17
18
- const TIMED_FUNCTIONS = [
18
+ const TIMED_FUNCTIONS = new Set ( [
19
19
'setTimeout' ,
20
20
'setInterval' ,
21
21
'setImmediate' ,
22
22
'requestAnimationFrame'
23
- ]
23
+ ] )
24
24
25
25
/**
26
26
* @param {CallExpression } node
27
27
*/
28
28
function isTimedFunction ( node ) {
29
29
const callee = utils . skipChainExpression ( node . callee )
30
30
return (
31
- ( ( node . type === 'CallExpression' &&
32
- callee . type === 'Identifier' &&
33
- TIMED_FUNCTIONS . indexOf ( callee . name ) !== - 1 ) ||
34
- ( node . type === 'CallExpression' &&
35
- callee . type === 'MemberExpression' &&
31
+ ( ( callee . type === 'Identifier' && TIMED_FUNCTIONS . has ( callee . name ) ) ||
32
+ ( callee . type === 'MemberExpression' &&
36
33
callee . object . type === 'Identifier' &&
37
34
callee . object . name === 'window' &&
38
- callee . property . type === 'Identifier' &&
39
- TIMED_FUNCTIONS . indexOf ( callee . property . name ) !== - 1 ) ) &&
40
- node . arguments . length
35
+ TIMED_FUNCTIONS . has ( utils . getStaticPropertyName ( callee ) || '' ) ) ) &&
36
+ node . arguments . length > 0
41
37
)
42
38
}
43
39
@@ -46,15 +42,16 @@ function isTimedFunction(node) {
46
42
*/
47
43
function isPromise ( node ) {
48
44
const callee = utils . skipChainExpression ( node . callee )
49
- if ( node . type === 'CallExpression' && callee . type === 'MemberExpression' ) {
45
+ if ( callee . type === 'MemberExpression' ) {
46
+ const name = utils . getStaticPropertyName ( callee )
50
47
return (
48
+ name &&
51
49
// hello.PROMISE_FUNCTION()
52
- ( callee . property . type === 'Identifier' &&
53
- PROMISE_FUNCTIONS . indexOf ( callee . property . name ) !== - 1 ) || // Promise.PROMISE_METHOD()
54
- ( callee . object . type === 'Identifier' &&
55
- callee . object . name === 'Promise' &&
56
- callee . property . type === 'Identifier' &&
57
- PROMISE_METHODS . indexOf ( callee . property . name ) !== - 1 )
50
+ ( PROMISE_FUNCTIONS . has ( name ) ||
51
+ // Promise.PROMISE_METHOD()
52
+ ( callee . object . type === 'Identifier' &&
53
+ callee . object . name === 'Promise' &&
54
+ PROMISE_METHODS . has ( name ) ) )
58
55
)
59
56
}
60
57
return false
@@ -79,7 +76,7 @@ module.exports = {
79
76
create ( context ) {
80
77
/** @type {Map<ObjectExpression, ComponentComputedProperty[]> } */
81
78
const computedPropertiesMap = new Map ( )
82
- /** @type {Array< FunctionExpression | ArrowFunctionExpression> } */
79
+ /** @type {( FunctionExpression | ArrowFunctionExpression)[] } */
83
80
const computedFunctionNodes = [ ]
84
81
85
82
/**
@@ -124,7 +121,7 @@ module.exports = {
124
121
* @param {ComponentComputedProperty[] } computedProperties
125
122
*/
126
123
function verify ( node , targetBody , type , computedProperties = [ ] ) {
127
- computedProperties . forEach ( ( cp ) => {
124
+ for ( const cp of computedProperties ) {
128
125
if (
129
126
cp . value &&
130
127
node . loc . start . line >= cp . value . loc . start . line &&
@@ -140,14 +137,15 @@ module.exports = {
140
137
propertyName : cp . key || 'unknown'
141
138
}
142
139
} )
140
+ return
143
141
}
144
- } )
142
+ }
145
143
146
- computedFunctionNodes . forEach ( ( c ) => {
144
+ for ( const cf of computedFunctionNodes ) {
147
145
if (
148
- node . loc . start . line >= c . loc . start . line &&
149
- node . loc . end . line <= c . loc . end . line &&
150
- targetBody === c . body
146
+ node . loc . start . line >= cf . body . loc . start . line &&
147
+ node . loc . end . line <= cf . body . loc . end . line &&
148
+ targetBody === cf . body
151
149
) {
152
150
context . report ( {
153
151
node,
@@ -156,8 +154,9 @@ module.exports = {
156
154
expressionName : expressionTypes [ type ]
157
155
}
158
156
} )
157
+ return
159
158
}
160
- } )
159
+ }
161
160
}
162
161
return Object . assign (
163
162
{
0 commit comments