Skip to content

Commit 7564135

Browse files
authored
Remove selector helpers (#2128)
1 parent 9be9ec4 commit 7564135

14 files changed

+85
-493
lines changed

rules/no-negated-condition.js

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ Based on ESLint builtin `no-negated-condition` rule
33
https://github.com/eslint/eslint/blob/5c39425fc55ecc0b97bbd07ac22654c0eb4f789c/lib/rules/no-negated-condition.js
44
*/
55
'use strict';
6-
const {matches} = require('./selectors/index.js');
76
const {
87
removeParentheses,
98
fixSpaceAroundKeyword,
@@ -21,18 +20,6 @@ const messages = {
2120
[MESSAGE_ID]: 'Unexpected negated condition.',
2221
};
2322

24-
const selector = [
25-
matches([
26-
'IfStatement[alternate][alternate.type!="IfStatement"]',
27-
'ConditionalExpression',
28-
]),
29-
matches([
30-
'[test.type="UnaryExpression"][test.operator="!"]',
31-
'[test.type="BinaryExpression"][test.operator="!="]',
32-
'[test.type="BinaryExpression"][test.operator="!=="]',
33-
]),
34-
].join('');
35-
3623
function * convertNegatedCondition(fixer, node, sourceCode) {
3724
const {test} = node;
3825
if (test.type === 'UnaryExpression') {
@@ -82,10 +69,32 @@ function * swapConsequentAndAlternate(fixer, node, sourceCode) {
8269
}
8370

8471
/** @param {import('eslint').Rule.RuleContext} context */
85-
const create = context => ({
86-
[selector](node) {
72+
const create = context => {
73+
context.on([
74+
'IfStatement',
75+
'ConditionalExpression',
76+
], node => {
77+
if (
78+
node.type === 'IfStatement'
79+
&& (
80+
!node.alternate
81+
|| node.alternate.type === 'IfStatement'
82+
)
83+
) {
84+
return;
85+
}
86+
87+
const {test} = node;
88+
89+
if (!(
90+
(test.type === 'UnaryExpression' && test.operator === '!')
91+
|| (test.type === 'BinaryExpression' && (test.operator === '!=' || test.operator === '!=='))
92+
)) {
93+
return;
94+
}
95+
8796
return {
88-
node: node.test,
97+
node: test,
8998
messageId: MESSAGE_ID,
9099
/** @param {import('eslint').Rule.RuleFixer} fixer */
91100
* fix(fixer) {
@@ -95,14 +104,14 @@ const create = context => ({
95104

96105
if (
97106
node.type !== 'ConditionalExpression'
98-
|| node.test.type !== 'UnaryExpression'
107+
|| test.type !== 'UnaryExpression'
99108
) {
100109
return;
101110
}
102111

103112
yield * fixSpaceAroundKeyword(fixer, node, sourceCode);
104113

105-
const {test, parent} = node;
114+
const {parent} = node;
106115
const [firstToken, secondToken] = sourceCode.getFirstTokens(test, 2);
107116
if (
108117
(parent.type === 'ReturnStatement' || parent.type === 'ThrowStatement')
@@ -121,8 +130,8 @@ const create = context => ({
121130
}
122131
},
123132
};
124-
},
125-
});
133+
});
134+
};
126135

127136
/** @type {import('eslint').Rule.RuleModule} */
128137
module.exports = {

rules/prefer-native-coercion-functions.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22
const {getFunctionHeadLocation, getFunctionNameWithKind} = require('@eslint-community/eslint-utils');
3-
const {not} = require('./selectors/index.js');
43

54
const MESSAGE_ID = 'prefer-native-coercion-functions';
65
const messages = {
@@ -69,19 +68,6 @@ function getCallExpression(node) {
6968
}
7069
}
7170

72-
const functionsSelector = [
73-
':function',
74-
'[async!=true]',
75-
'[generator!=true]',
76-
'[params.length>0]',
77-
'[params.0.type="Identifier"]',
78-
not([
79-
'MethodDefinition[kind="constructor"] > .value',
80-
'MethodDefinition[kind="set"] > .value',
81-
'Property[kind="set"] > .value',
82-
]),
83-
].join('');
84-
8571
function getArrayCallbackProblem(node) {
8672
if (!isArrayIdentityCallback(node)) {
8773
return;
@@ -129,8 +115,31 @@ function getCoercionFunctionProblem(node) {
129115
}
130116

131117
/** @param {import('eslint').Rule.RuleContext} context */
132-
const create = context => ({
133-
[functionsSelector](node) {
118+
const create = context => {
119+
context.on([
120+
'FunctionDeclaration',
121+
'FunctionExpression',
122+
'ArrowFunctionExpression',
123+
], node => {
124+
if (
125+
node.async
126+
|| node.generator
127+
|| node.params.length === 0
128+
|| node.params[0].type !== 'Identifier'
129+
|| (
130+
(
131+
(
132+
node.parent.type === 'MethodDefinition'
133+
&& (node.parent.kind === 'constructor' || node.parent.kind === 'set')
134+
)
135+
|| (node.parent.type === 'Property' && node.parent.kind === 'set')
136+
)
137+
&& node.parent.value === node
138+
)
139+
) {
140+
return;
141+
}
142+
134143
let problem = getArrayCallbackProblem(node) || getCoercionFunctionProblem(node);
135144

136145
if (!problem) {
@@ -162,8 +171,8 @@ const create = context => ({
162171
problem.fix = fix;
163172

164173
return problem;
165-
},
166-
});
174+
});
175+
};
167176

168177
/** @type {import('eslint').Rule.RuleModule} */
169178
module.exports = {

rules/selectors/call-or-new-expression-selector.js

Lines changed: 0 additions & 106 deletions
This file was deleted.

rules/selectors/index.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

rules/selectors/matches-any.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

rules/selectors/member-expression-selector.js

Lines changed: 0 additions & 83 deletions
This file was deleted.

0 commit comments

Comments
 (0)