11'use strict' ;
22const methodSelector = require ( './utils/method-selector' ) ;
33const getDocumentationUrl = require ( './utils/get-documentation-url' ) ;
4+ const { notFunctionSelector} = require ( './utils/not-function' ) ;
45
56const MESSAGE_ID_REDUCE = 'reduce' ;
67const MESSAGE_ID_REDUCE_RIGHT = 'reduceRight' ;
78
8- const ignoredFirstArgumentSelector = `:not(${
9- [
10- '[arguments.0.type="Literal"]' ,
11- '[arguments.0.type="Identifier"][arguments.0.name="undefined"]'
12- ] . join ( ',' )
13- } )`;
14-
15- const PROTOTYPE_SELECTOR = [
16- methodSelector ( { names : [ 'call' , 'apply' ] } ) ,
17- ignoredFirstArgumentSelector ,
9+ const prototypeSelector = method => [
10+ methodSelector ( { name : method } ) ,
1811 '[callee.object.type="MemberExpression"]' ,
1912 '[callee.object.computed=false]' ,
2013 `:matches(${
@@ -45,9 +38,16 @@ const PROTOTYPE_SELECTOR = [
4538 } )`
4639] . join ( '' ) ;
4740
41+ const PROTOTYPE_CALL_SELECTOR = [
42+ prototypeSelector ( 'call' ) ,
43+ notFunctionSelector ( 'arguments.1' )
44+ ] . join ( '' ) ;
45+
46+ const PROTOTYPE_APPLY_SELECTOR = prototypeSelector ( 'apply' ) ;
47+
4848const METHOD_SELECTOR = [
4949 methodSelector ( { names : [ 'reduce' , 'reduceRight' ] , min : 1 , max : 2 } ) ,
50- ignoredFirstArgumentSelector
50+ notFunctionSelector ( 'arguments.0' )
5151] . join ( '' ) ;
5252
5353const create = context => {
@@ -56,9 +56,13 @@ const create = context => {
5656 // For arr.reduce()
5757 context . report ( { node : node . callee . property , messageId : node . callee . property . name } ) ;
5858 } ,
59- [ PROTOTYPE_SELECTOR ] ( node ) {
59+ [ PROTOTYPE_CALL_SELECTOR ] ( node ) {
6060 // For cases [].reduce.call() and Array.prototype.reduce.call()
6161 context . report ( { node : node . callee . object . property , messageId : node . callee . object . property . name } ) ;
62+ } ,
63+ [ PROTOTYPE_APPLY_SELECTOR ] ( node ) {
64+ // For cases [].reduce.apply() and Array.prototype.reduce.apply()
65+ context . report ( { node : node . callee . object . property , messageId : node . callee . object . property . name } ) ;
6266 }
6367 } ;
6468} ;
0 commit comments