Skip to content

Commit 3646efb

Browse files
authored
Reuse existing AST check utilities (#2650)
1 parent 8a5142f commit 3646efb

File tree

3 files changed

+37
-46
lines changed

3 files changed

+37
-46
lines changed

rules/prefer-at.js

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ import {
1717
removeLengthNode,
1818
} from './shared/negative-index.js';
1919
import {removeMemberExpressionProperty, removeMethodCall} from './fix/index.js';
20-
import {isLiteral, isCallExpression, isMethodCall} from './ast/index.js';
20+
import {
21+
isLiteral,
22+
isCallExpression,
23+
isMethodCall,
24+
isMemberExpression,
25+
} from './ast/index.js';
2126

2227
const MESSAGE_ID_NEGATIVE_INDEX = 'negative-index';
2328
const MESSAGE_ID_INDEX = 'index';
@@ -45,28 +50,22 @@ const isLiteralNegativeInteger = node =>
4550
&& node.argument.type === 'Literal'
4651
&& Number.isInteger(node.argument.value)
4752
&& node.argument.value > 0;
48-
const isZeroIndexAccess = node => {
49-
const {parent} = node;
50-
return parent.type === 'MemberExpression'
51-
&& !parent.optional
52-
&& parent.computed
53-
&& parent.object === node
54-
&& isLiteral(parent.property, 0);
55-
};
56-
57-
const isArrayPopOrShiftCall = (node, method) => {
58-
const {parent} = node;
59-
return parent.type === 'MemberExpression'
60-
&& !parent.optional
61-
&& !parent.computed
62-
&& parent.object === node
63-
&& parent.property.type === 'Identifier'
64-
&& parent.property.name === method
65-
&& parent.parent.type === 'CallExpression'
66-
&& parent.parent.callee === parent
67-
&& !parent.parent.optional
68-
&& parent.parent.arguments.length === 0;
69-
};
53+
const isZeroIndexAccess = node =>
54+
isMemberExpression(node.parent, {
55+
optional: false,
56+
computed: true,
57+
})
58+
&& node.parent.object === node
59+
&& isLiteral(node.parent.property, 0);
60+
61+
const isArrayPopOrShiftCall = (node, method) =>
62+
isMethodCall(node.parent.parent, {
63+
method,
64+
argumentsLength: 0,
65+
optionalCall: false,
66+
optionalMember: false,
67+
})
68+
&& node.parent.object === node;
7069

7170
const isArrayPopCall = node => isArrayPopOrShiftCall(node, 'pop');
7271
const isArrayShiftCall = node => isArrayPopOrShiftCall(node, 'shift');

rules/prefer-set-has.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,14 @@ const arrayMethodsReturnsArray = [
2727
'with',
2828
];
2929

30-
const isIncludesCall = node => {
31-
const {type, optional, callee, arguments: includesArguments} = node.parent.parent ?? {};
32-
return (
33-
type === 'CallExpression'
34-
&& !optional
35-
&& callee.type === 'MemberExpression'
36-
&& !callee.computed
37-
&& !callee.optional
38-
&& callee.object === node
39-
&& callee.property.type === 'Identifier'
40-
&& callee.property.name === 'includes'
41-
&& includesArguments.length === 1
42-
&& includesArguments[0].type !== 'SpreadElement'
43-
);
44-
};
30+
const isIncludesCall = node =>
31+
isMethodCall(node.parent.parent, {
32+
method: 'includes',
33+
optionalCall: false,
34+
optionalMember: false,
35+
argumentsLength: 1,
36+
})
37+
&& node.parent.object === node;
4538

4639
const multipleCallNodeTypes = new Set([
4740
'ForOfStatement',

rules/text-encoding-identifier-case.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {replaceStringRaw} from './fix/index.js';
2+
import {isMethodCall} from './ast/index.js';
23

34
const MESSAGE_ID_ERROR = 'text-encoding-identifier/error';
45
const MESSAGE_ID_SUGGESTION = 'text-encoding-identifier/suggestion';
@@ -24,15 +25,13 @@ const getReplacement = encoding => {
2425

2526
// `fs.{readFile,readFileSync}()`
2627
const isFsReadFileEncoding = node =>
27-
node.parent.type === 'CallExpression'
28-
&& !node.parent.optional
28+
isMethodCall(node.parent, {
29+
methods: ['readFile', 'readFileSync'],
30+
optionalCall: false,
31+
optionalMember: false,
32+
})
2933
&& node.parent.arguments[1] === node
30-
&& node.parent.arguments[0].type !== 'SpreadElement'
31-
&& node.parent.callee.type === 'MemberExpression'
32-
&& !node.parent.callee.optional
33-
&& !node.parent.callee.computed
34-
&& node.parent.callee.property.type === 'Identifier'
35-
&& (node.parent.callee.property.name === 'readFile' || node.parent.callee.property.name === 'readFileSync');
34+
&& node.parent.arguments[0].type !== 'SpreadElement';
3635

3736
/** @param {import('eslint').Rule.RuleContext} context */
3837
const create = () => ({

0 commit comments

Comments
 (0)