1
1
'use strict' ;
2
2
const { getFunctionHeadLocation} = require ( '@eslint-community/eslint-utils' ) ;
3
- const getDocumentationUrl = require ( './utils/get-documentation-url.js' ) ;
4
- const { methodCallSelector, matches} = require ( './selectors/index.js' ) ;
3
+ const { isMethodCall} = require ( './ast/index.js' ) ;
5
4
6
5
const MESSAGE_ID = 'no-invalid-remove-event-listener' ;
7
6
const messages = {
8
7
[ MESSAGE_ID ] : 'The listener argument should be a function reference.' ,
9
8
} ;
10
9
11
- const removeEventListenerSelector = [
12
- methodCallSelector ( {
13
- method : 'removeEventListener' ,
14
- minimumArguments : 2 ,
15
- } ) ,
16
- '[arguments.0.type!="SpreadElement"]' ,
17
- matches ( [
18
- '[arguments.1.type="FunctionExpression"]' ,
19
- '[arguments.1.type="ArrowFunctionExpression"]' ,
20
- methodCallSelector ( { method : 'bind' , path : 'arguments.1' } ) ,
21
- ] ) ,
22
- ] . join ( '' ) ;
23
-
24
10
/** @param {import('eslint').Rule.RuleContext } context */
25
11
const create = context => ( {
26
- [ removeEventListenerSelector ] ( node ) {
27
- const listener = node . arguments [ 1 ] ;
12
+ CallExpression ( callExpression ) {
13
+ if ( ! (
14
+ isMethodCall ( callExpression , {
15
+ method : 'removeEventListener' ,
16
+ minimumArguments : 2 ,
17
+ optionalCall : false ,
18
+ optionalMember : false ,
19
+ } )
20
+ && callExpression . arguments [ 0 ] . type !== 'SpreadElement'
21
+ && (
22
+ callExpression . arguments [ 1 ] . type === 'FunctionExpression'
23
+ || callExpression . arguments [ 1 ] . type === 'ArrowFunctionExpression'
24
+ || isMethodCall ( callExpression . arguments [ 1 ] , {
25
+ method : 'bind' ,
26
+ optionalCall : false ,
27
+ optionalMember : false ,
28
+ } )
29
+ )
30
+ ) ) {
31
+ return ;
32
+ }
33
+
34
+ const [ , listener ] = callExpression . arguments ;
28
35
if ( [ 'ArrowFunctionExpression' , 'FunctionExpression' ] . includes ( listener . type ) ) {
29
36
return {
30
37
node : listener ,
@@ -47,7 +54,6 @@ module.exports = {
47
54
type : 'problem' ,
48
55
docs : {
49
56
description : 'Prevent calling `EventTarget#removeEventListener()` with the result of an expression.' ,
50
- url : getDocumentationUrl ( __filename ) ,
51
57
} ,
52
58
messages,
53
59
} ,
0 commit comments