@@ -11,33 +11,29 @@ const utils = require('../utils')
1111 * @typedef {import('../utils').ComponentComputedProperty } ComponentComputedProperty
1212 */
1313
14- const PROMISE_FUNCTIONS = [ 'then' , 'catch' , 'finally' ]
14+ const PROMISE_FUNCTIONS = new Set ( [ 'then' , 'catch' , 'finally' ] )
1515
16- const PROMISE_METHODS = [ 'all' , 'race' , 'reject' , 'resolve' ]
16+ const PROMISE_METHODS = new Set ( [ 'all' , 'race' , 'reject' , 'resolve' ] )
1717
18- const TIMED_FUNCTIONS = [
18+ const TIMED_FUNCTIONS = new Set ( [
1919 'setTimeout' ,
2020 'setInterval' ,
2121 'setImmediate' ,
2222 'requestAnimationFrame'
23- ]
23+ ] )
2424
2525/**
2626 * @param {CallExpression } node
2727 */
2828function isTimedFunction ( node ) {
2929 const callee = utils . skipChainExpression ( node . callee )
3030 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' &&
3633 callee . object . type === 'Identifier' &&
3734 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
4137 )
4238}
4339
@@ -46,15 +42,16 @@ function isTimedFunction(node) {
4642 */
4743function isPromise ( node ) {
4844 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 )
5047 return (
48+ name &&
5149 // 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 ) ) )
5855 )
5956 }
6057 return false
@@ -79,7 +76,7 @@ module.exports = {
7976 create ( context ) {
8077 /** @type {Map<ObjectExpression, ComponentComputedProperty[]> } */
8178 const computedPropertiesMap = new Map ( )
82- /** @type {Array< FunctionExpression | ArrowFunctionExpression> } */
79+ /** @type {( FunctionExpression | ArrowFunctionExpression)[] } */
8380 const computedFunctionNodes = [ ]
8481
8582 /**
@@ -124,7 +121,7 @@ module.exports = {
124121 * @param {ComponentComputedProperty[] } computedProperties
125122 */
126123 function verify ( node , targetBody , type , computedProperties = [ ] ) {
127- computedProperties . forEach ( ( cp ) => {
124+ for ( const cp of computedProperties ) {
128125 if (
129126 cp . value &&
130127 node . loc . start . line >= cp . value . loc . start . line &&
@@ -140,14 +137,15 @@ module.exports = {
140137 propertyName : cp . key || 'unknown'
141138 }
142139 } )
140+ return
143141 }
144- } )
142+ }
145143
146- computedFunctionNodes . forEach ( ( c ) => {
144+ for ( const cf of computedFunctionNodes ) {
147145 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
151149 ) {
152150 context . report ( {
153151 node,
@@ -156,8 +154,9 @@ module.exports = {
156154 expressionName : expressionTypes [ type ]
157155 }
158156 } )
157+ return
159158 }
160- } )
159+ }
161160 }
162161 return Object . assign (
163162 {
0 commit comments