Skip to content

Commit

Permalink
Distinguish error types
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverJAsh committed Dec 13, 2021
1 parent fd6b86d commit 2a88e76
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
37 changes: 30 additions & 7 deletions src/rules/no-redundant-pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import {
CallExpressionWithExpressionArgs,
} from "../utils";

const errorMessages = {
redundantPipeWithSingleArg:
"pipe can be removed because it takes only one argument",
redundantPipeWithSingleArgInsidePipe:
"pipe can be removed because it is used as the first argument inside another pipe",
};

export default createRule({
name: "no-redundant-pipe",
meta: {
Expand All @@ -23,8 +30,7 @@ export default createRule({
recommended: "warn",
},
messages: {
redundantPipe:
"pipe can be removed because it takes only one argument or it is used as the first argument inside another pipe",
...errorMessages,
removePipe: "remove pipe",
},
},
Expand All @@ -40,15 +46,32 @@ export default createRule({
O.chain(getCallExpressionWithExpressionArgs)
);

type RedundantPipeCallAndMessage = {
redundantPipeCall: CallExpressionWithExpressionArgs;
errorMessageId: keyof typeof errorMessages;
};

const getRedundantPipeCall = (
pipeCall: CallExpressionWithExpressionArgs
): O.Option<CallExpressionWithExpressionArgs> => {
): O.Option<RedundantPipeCallAndMessage> => {
const firstArg = pipe(pipeCall.args, NonEmptyArray.head);

if (pipeCall.args.length === 1) {
return O.some(pipeCall);
const result: RedundantPipeCallAndMessage = {
redundantPipeCall: pipeCall,
errorMessageId: "redundantPipeWithSingleArg",
};
return O.some(result);
} else if (firstArg.type === AST_NODE_TYPES.CallExpression) {
return getPipeCallExpressionWithExpressionArgs(firstArg);
return pipe(
getPipeCallExpressionWithExpressionArgs(firstArg),
O.map(
(redundantPipeCall): RedundantPipeCallAndMessage => ({
redundantPipeCall,
errorMessageId: "redundantPipeWithSingleArgInsidePipe",
})
)
);
} else {
return O.none;
}
Expand All @@ -60,10 +83,10 @@ export default createRule({
node,
getPipeCallExpressionWithExpressionArgs,
O.chain(getRedundantPipeCall),
O.map((redundantPipeCall) => {
O.map(({ redundantPipeCall, errorMessageId }) => {
context.report({
node: redundantPipeCall.node,
messageId: "redundantPipe",
messageId: errorMessageId,
suggest: [
{
messageId: "removePipe",
Expand Down
14 changes: 7 additions & 7 deletions tests/rules/no-redundant-pipe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pipe(value);
`,
errors: [
{
messageId: "redundantPipe",
messageId: "redundantPipeWithSingleArg",
suggestions: [
{
messageId: "removePipe",
Expand All @@ -48,7 +48,7 @@ pipe(pipe(pipe(value, fn1), fn2), fn3);
`,
errors: [
{
messageId: "redundantPipe",
messageId: "redundantPipeWithSingleArgInsidePipe",
suggestions: [
{
messageId: "removePipe",
Expand All @@ -60,7 +60,7 @@ pipe(pipe(value, fn1), fn2, fn3);
],
},
{
messageId: "redundantPipe",
messageId: "redundantPipeWithSingleArgInsidePipe",
suggestions: [
{
messageId: "removePipe",
Expand All @@ -80,7 +80,7 @@ pipe(pipe(value, fn1), fn2, fn3);
`,
errors: [
{
messageId: "redundantPipe",
messageId: "redundantPipeWithSingleArgInsidePipe",
suggestions: [
{
messageId: "removePipe",
Expand All @@ -103,7 +103,7 @@ pipe(
);`,
errors: [
{
messageId: "redundantPipe",
messageId: "redundantPipeWithSingleArgInsidePipe",
suggestions: [
{
messageId: "removePipe",
Expand All @@ -128,7 +128,7 @@ pipe(
`,
errors: [
{
messageId: "redundantPipe",
messageId: "redundantPipeWithSingleArg",
suggestions: [
{
messageId: "removePipe",
Expand All @@ -151,7 +151,7 @@ pipe(
`,
errors: [
{
messageId: "redundantPipe",
messageId: "redundantPipeWithSingleArg",
suggestions: [
{
messageId: "removePipe",
Expand Down

0 comments on commit 2a88e76

Please sign in to comment.