Skip to content

Commit

Permalink
removed chains refrences
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaPontrandolfo committed Jul 6, 2024
1 parent 64a314b commit 9d88510
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 108 deletions.
4 changes: 2 additions & 2 deletions src/rules/collection-method-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module.exports = {
);
}

function isPureLodashCollectionMethod(method) {
function isPureRemedaCollectionMethod(method) {
return isCollectionMethod(method) && !isAliasOfMethod("remove", method);
}

Expand All @@ -59,7 +59,7 @@ module.exports = {
context,
(node, iteratee, { method, callType }) => {
if (
isPureLodashCollectionMethod(method) &&
isPureRemedaCollectionMethod(method) &&
!parentUsesValue(node, callType)
) {
context.report({
Expand Down
45 changes: 0 additions & 45 deletions src/util/LodashContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,51 +114,6 @@ module.exports = class {
);
}

/**
* Returns whether the node is an implicit chain start, _()...
* @param node
* @returns {boolean|undefined}
*/
isImplicitChainStart(node) {
return (
(this.pragma && node.callee.name === this.pragma) ||
(this.isImportedRemeda(node.callee) && node.callee.name !== "chain")
);
}

/**
* Returns whether the node is an explicit chain start, _.chain()...
* @param node
* @returns {boolean|undefined}
*/
isExplicitChainStart(node) {
return this.isLodashCall(node) && getMethodName(node) === "chain";
}

/**
* Returns whether the node is chain start imported as member, chain()... import { chain } from 'lodash'
* @param node
* @returns {boolean|undefined}
*/
isImportedChainStart(node) {
return node.callee.name === "chain" && this.isImportedRemeda(node.callee);
}

/**
* Returns whether the node is a Lodash chain start, implicit or explicit
* @param node
* @returns {*|boolean|boolean|undefined}
*/
isLodashChainStart(node) {
return (
node &&
node.type === "CallExpression" &&
(this.isImplicitChainStart(node) ||
this.isExplicitChainStart(node) ||
this.isImportedChainStart(node))
);
}

/**
*
* @returns {string|undefined} the current Remeda pragma
Expand Down
52 changes: 5 additions & 47 deletions src/util/lodashUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,7 @@ function getIsTypeMethod(name) {
function getRemedaMethodCallExpVisitor(lodashContext, reporter) {
return function (node) {
let iterateeIndex;
if (lodashContext.isLodashChainStart(node)) {
let prevNode = node;
node = node.parent.parent;
while (
astUtil.getCaller(node) === prevNode &&
astUtil.isMethodCall(node) &&
!isChainBreaker(node)
) {
const method = astUtil.getMethodName(node);
iterateeIndex = methodDataUtil.getIterateeIndex(method);
reporter(node, node.arguments[iterateeIndex - 1], {
callType: "chained",
method,
lodashContext,
});
prevNode = node;
node = node.parent.parent;
}
} else if (lodashContext.isLodashCall(node)) {
if (lodashContext.isLodashCall(node)) {
const method = astUtil.getMethodName(node);
iterateeIndex = methodDataUtil.getIterateeIndex(method);
reporter(node, node.arguments[iterateeIndex], {
Expand All @@ -94,16 +76,16 @@ function getRemedaMethodCallExpVisitor(lodashContext, reporter) {
};
}

function isLodashCallToMethod(node, method, lodashContext) {
function isRemedaCallToMethod(node, method, lodashContext) {
return lodashContext.isLodashCall(node) && isCallToMethod(node, method);
}

function isCallToLodashMethod(node, method, lodashContext) {
function isCallToRemedaMethod(node, method, lodashContext) {
if (!node || node.type !== "CallExpression") {
return false;
}
return (
isLodashCallToMethod(node, method, lodashContext) ||
isRemedaCallToMethod(node, method, lodashContext) ||
methodDataUtil.isAliasOfMethod(
method,
lodashContext.getImportedRemedaMethod(node),
Expand Down Expand Up @@ -135,31 +117,7 @@ module.exports = {
isCallToMethod,
getIsTypeMethod,
getRemedaMethodCallExpVisitor,
isCallToLodashMethod,
isCallToLodashMethod: isCallToRemedaMethod,
getRemedaMethodVisitors,
getLodashContext,
};

/**
@callback LodashReporter
@param {Object} node
@param {Object} iteratee
@param {Object?} options
*/

/**
@callback NodeTypeVisitor
@param {Object} node
*/

/**
* @typedef {Object} ShorthandChecks
* @property {function} canUseShorthand
* @property {function} usesShorthand
*/

/**
* @typedef {object} ShorthandMessages
* @property {string} always
* @property {string} never
*/
14 changes: 0 additions & 14 deletions tests/lib/util/LodashContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,20 +188,6 @@ describe("LodashContext", () => {
}),
);
});
it("should not consider Object.prototype methods as Remeda", (done) => {
visitWithContext(
'const {toString} = require("remeda"); const x = toString(y)',
undefined,
(lodashContext) => ({
CallExpression(node) {
if (node.callee.name === "toString") {
assert(!lodashContext.isLodashChainStart(node));
done();
}
},
}),
);
});
});
});
describe("isImportedRemeda", () => {
Expand Down

0 comments on commit 9d88510

Please sign in to comment.