Skip to content

Commit 808ec47

Browse files
authored
no-invalid-remove-event-listener: Use simple selector (#2113)
1 parent 95e551d commit 808ec47

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

rules/no-invalid-remove-event-listener.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,37 @@
11
'use strict';
22
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');
54

65
const MESSAGE_ID = 'no-invalid-remove-event-listener';
76
const messages = {
87
[MESSAGE_ID]: 'The listener argument should be a function reference.',
98
};
109

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-
2410
/** @param {import('eslint').Rule.RuleContext} context */
2511
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;
2835
if (['ArrowFunctionExpression', 'FunctionExpression'].includes(listener.type)) {
2936
return {
3037
node: listener,
@@ -47,7 +54,6 @@ module.exports = {
4754
type: 'problem',
4855
docs: {
4956
description: 'Prevent calling `EventTarget#removeEventListener()` with the result of an expression.',
50-
url: getDocumentationUrl(__filename),
5157
},
5258
messages,
5359
},

0 commit comments

Comments
 (0)