Skip to content

Commit

Permalink
Use isMethodCall and isNodeValueNotFunction in `no-array-method-t…
Browse files Browse the repository at this point in the history
…his-argument`
  • Loading branch information
fisker committed May 12, 2023
1 parent 00f86e7 commit 4e7ab4c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
49 changes: 26 additions & 23 deletions rules/no-array-method-this-argument.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';
const {hasSideEffect} = require('@eslint-community/eslint-utils');
const {methodCallSelector, notFunctionSelector} = require('./selectors/index.js');
const {removeArgument} = require('./fix/index.js');
const {getParentheses, getParenthesizedText} = require('./utils/parentheses.js');
const shouldAddParenthesesToMemberExpressionObject = require('./utils/should-add-parentheses-to-member-expression-object.js');
const {isNodeMatches} = require('./utils/is-node-matches.js');
const {isNodeValueNotFunction} = require('./utils/index.js');
const {isMethodCall} = require('./ast/index.js')

Check failure on line 8 in rules/no-array-method-this-argument.js

View workflow job for this annotation

GitHub Actions / lint-test

Missing semicolon.

const ERROR = 'error';
const SUGGESTION_BIND = 'suggestion-bind';
Expand Down Expand Up @@ -69,25 +70,6 @@ const ignored = [
'underscore.some',
];

const selector = [
methodCallSelector({
methods: [
'every',
'filter',
'find',
'findLast',
'findIndex',
'findLastIndex',
'flatMap',
'forEach',
'map',
'some',
],
argumentsLength: 2,
}),
notFunctionSelector('arguments.0'),
].join('');

function removeThisArgument(callExpression, sourceCode) {
return fixer => removeArgument(fixer, callExpression.arguments[1], sourceCode);
}
Expand Down Expand Up @@ -122,12 +104,33 @@ const create = context => {
const {sourceCode} = context;

return {
[selector](callExpression) {
const {callee} = callExpression;
if (isNodeMatches(callee, ignored)) {
CallExpression(callExpression) {
if (
!isMethodCall({
methods: [
'every',
'filter',
'find',
'findLast',
'findIndex',
'findLastIndex',
'flatMap',
'forEach',
'map',
'some',
],
argumentsLength: 2,
optionalCall: false,
optionalMember: false,
computed: false,
})
|| isNodeMatches(callExpression.callee, ignored)
|| !isNodeValueNotFunction(callExpression.arguments[0])
) {
return;
}

const {callee} = callExpression;
const method = callee.property.name;
const [callback, thisArgument] = callExpression.arguments;

Expand Down
5 changes: 5 additions & 0 deletions rules/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = {
isNodeValueNotFunction: require('./is-node-value-not-function.js')

Check failure on line 4 in rules/utils/index.js

View workflow job for this annotation

GitHub Actions / lint-test

Missing trailing comma.
};
5 changes: 3 additions & 2 deletions rules/utils/is-node-value-not-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const mostLikelyNotNodeTypes = new Set([
'ThisExpression',
]);

const isNodeValueNotFunction = node =>
const isNodeValueNotFunction = node => !(
impossibleNodeTypes.has(node.type)
|| mostLikelyNotNodeTypes.has(node.type)
|| isUndefined(node)
Expand All @@ -37,6 +37,7 @@ const isNodeValueNotFunction = node =>
optionalMember: false,
computed: false,
}))
);
)
);

module.exports = isNodeValueNotFunction;

0 comments on commit 4e7ab4c

Please sign in to comment.