Skip to content

Commit

Permalink
removed some aliases calls
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaPontrandolfo committed Jul 14, 2024
1 parent 5005fa1 commit e024050
Show file tree
Hide file tree
Showing 10 changed files with 14 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Enable all of the rules that you would like to use. All rules are off by default
- [collection-return](docs/rules/collection-return.md): Always return a value in iteratees of Remeda collection methods that aren't `forEach`.
- [prefer-filter](docs/rules/prefer-filter.md): Prefer `R.filter` over `R.forEach` with an `if` statement inside.
- [prefer-find](docs/rules/prefer-find.md): Prefer `R.find` over `R.filter` followed by selecting the first result.
- [prefer-flat-map](docs/rules/prefer-flat-map.md): Prefer `R.flatMap` over consecutive `map` and `flatten`.
- [prefer-flat-map](docs/rules/prefer-flat-map.md): Prefer `R.flatMap` over consecutive `R.map` and `R.flat`.
- [prefer-map](docs/rules/prefer-map.md): Prefer `R.map` over `R.forEach` with a `push` inside.
- [prefer-nullish-coalescing](docs/rules/prefer-nullish-coalescing.md): Prefer `??` when doing a comparison with a non-nullish value as test.
- [prefer-constant](docs/rules/prefer-constant.md): Prefer `R.constant` over functions returning literals.
Expand Down
8 changes: 4 additions & 4 deletions docs/rules/prefer-flat-map.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Prefer [`R.flatMap`] over consecutive [`R.map`] and [`R.flatten`]
# Prefer [`R.flatMap`] over consecutive [`R.map`] and [`R.flat`]

When using [`R.map`] and [`R.flatten`], it can be more concise to use [`R.flatMap`] instead.
When using [`R.map`] and [`R.flat`], it can be more concise to use [`R.flatMap`] instead.

## Rule Details

Expand All @@ -9,7 +9,7 @@ This rule takes no arguments.
The following patterns are considered warnings:

```js
t = R.flatten(R.map(a, f));
t = R.flat(R.map(a, f));
```

The following patterns are not considered warnings:
Expand All @@ -22,4 +22,4 @@ t = R.flatMap(a, f);

## When Not To Use It

If you do not want to enforce using [`R.flatMap`], and prefer [`R.map`] and [`R.flatten`] instead, you should not use this rule.
If you do not want to enforce using [`R.flatMap`], and prefer [`R.map`] and [`R.flat`] instead, you should not use this rule.
10 changes: 1 addition & 9 deletions src/rules/collection-method-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ module.exports = {
const { getMethodName } = require("../util/astUtil");
const {
isCollectionMethod,
isAliasOfMethod,
getSideEffectIterationMethods,
} = require("../util/methodDataUtil");
const includes = require("lodash/includes");
Expand All @@ -41,10 +40,6 @@ module.exports = {
);
}

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

function isSideEffectIterationMethod(method) {
return includes(getSideEffectIterationMethods(), method);
}
Expand All @@ -58,10 +53,7 @@ module.exports = {
return getRemedaMethodVisitors(
context,
(node, iteratee, { method, callType }) => {
if (
isPureRemedaCollectionMethod(method) &&
!parentUsesValue(node, callType)
) {
if (isCollectionMethod(method) && !parentUsesValue(node, callType)) {
context.report({
node,
message: `Use value returned from R.${method}`,
Expand Down
6 changes: 1 addition & 5 deletions src/rules/prefer-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ module.exports = {
hasOnlyOneStatement,
getFirstParamName,
} = require("../util/astUtil");
const { isAliasOfMethod } = require("../util/methodDataUtil");
const DEFAULT_MAX_PROPERTY_PATH_LENGTH = 3;
const maxLength =
parseInt(context.options[0], 10) || DEFAULT_MAX_PROPERTY_PATH_LENGTH;
Expand Down Expand Up @@ -67,10 +66,7 @@ module.exports = {
}

return getRemedaMethodVisitors(context, (node, iteratee, { method }) => {
if (
isAliasOfMethod("forEach", method) &&
onlyHasSimplifiableIf(iteratee)
) {
if (method === "forEach" && onlyHasSimplifiableIf(iteratee)) {
context.report({
node,
message:
Expand Down
3 changes: 1 addition & 2 deletions src/rules/prefer-find.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ module.exports = {
isCallToMethod,
isCallToRemedaMethod,
} = require("../util/remedaUtil");
const { isAliasOfMethod } = require("../util/methodDataUtil");

function isZeroIndexAccess(node) {
return node.type === "MemberExpression" && node.property.value === 0;
Expand All @@ -39,7 +38,7 @@ module.exports = {
return getRemedaMethodVisitors(
context,
(node, iteratee, { method, callType, remedaContext }) => {
if (isAliasOfMethod("filter", method)) {
if (method === "filter") {
if (
isZeroIndexAccess(node.parent) ||
isCallToRemedaMethod(node.parent, "first", remedaContext) ||
Expand Down
5 changes: 2 additions & 3 deletions src/rules/prefer-flat-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ module.exports = {
isCallToRemedaMethod,
} = require("../util/remedaUtil");
const { getCaller } = require("../util/astUtil");
const { isAliasOfMethod } = require("../util/methodDataUtil");

function isChainedMapFlatten(callType, node) {
return callType === "chained" && isCallToMethod(getCaller(node), "map");
Expand All @@ -35,13 +34,13 @@ module.exports = {
context,
(node, iteratee, { method, callType, remedaContext }) => {
if (
isAliasOfMethod("flatten", method) &&
method === "flat" &&
(isChainedMapFlatten(callType, node) ||
isCallToRemedaMethod(node.arguments[0], "map", remedaContext))
) {
context.report({
node,
message: "Prefer R.flatMap over consecutive map and flatten.",
message: "Prefer R.flatMap over consecutive R.map and R.flat.",
});
}
},
Expand Down
3 changes: 1 addition & 2 deletions src/rules/prefer-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ module.exports = {
isFunctionDefinitionWithBlock,
collectParameterValues,
} = require("../util/astUtil");
const { isAliasOfMethod } = require("../util/methodDataUtil");
const get = require("lodash/get");
const includes = require("lodash/includes");

Expand All @@ -50,7 +49,7 @@ module.exports = {
}

return getRemedaMethodVisitors(context, (node, iteratee, { method }) => {
if (isAliasOfMethod("forEach", method) && onlyHasPush(iteratee)) {
if (method === "forEach" && onlyHasPush(iteratee)) {
context.report({
node,
message:
Expand Down
3 changes: 1 addition & 2 deletions src/rules/prefer-some.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ module.exports = {
create(context) {
const { getExpressionComparedToInt } = require("../util/astUtil");
const { getRemedaMethodVisitors } = require("../util/remedaUtil");
const { isAliasOfMethod } = require("../util/methodDataUtil");

const visitors = getRemedaMethodVisitors(
context,
(node, iteratee, { method }) => {
if (
isAliasOfMethod("findIndex", method) &&
method === "findIndex" &&
node === getExpressionComparedToInt(node.parent, -1, true)
) {
context.report({
Expand Down
6 changes: 1 addition & 5 deletions src/rules/prefer-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@ module.exports = {

create(context) {
const { getRemedaMethodVisitors } = require("../util/remedaUtil");
const { isAliasOfMethod } = require("../util/methodDataUtil");
const get = require("lodash/get");
return getRemedaMethodVisitors(context, (node, iteratee, { method }) => {
if (
isAliasOfMethod("map", method) &&
get(iteratee, "params.length") === 0
) {
if (method === "map" && get(iteratee, "params.length") === 0) {
context.report({
node,
message: "Prefer R.times over R.map without using arguments",
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/rules/prefer-flat-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const ruleTesterUtil = require("../testUtil/ruleTesterUtil");
const ruleTester = ruleTesterUtil.getRuleTester();
const { fromMessage, withDefaultPragma } = require("../testUtil/optionsUtil");
const toErrorObject = fromMessage(
"Prefer R.flatMap over consecutive map and flatten.",
"Prefer R.flatMap over consecutive R.map and R.flat.",
);
ruleTester.run("prefer-flat-map", rule, {
valid: ["t = R.map(a, f);", "t = R.flat(a);", "t = R.flatMap(a, f);"].map(
Expand Down

0 comments on commit e024050

Please sign in to comment.