diff --git a/src/rules/no-redundant-pipe.ts b/src/rules/no-redundant-pipe.ts index 1206ef6..c7305d3 100644 --- a/src/rules/no-redundant-pipe.ts +++ b/src/rules/no-redundant-pipe.ts @@ -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: { @@ -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", }, }, @@ -40,15 +46,32 @@ export default createRule({ O.chain(getCallExpressionWithExpressionArgs) ); + type RedundantPipeCallAndMessage = { + redundantPipeCall: CallExpressionWithExpressionArgs; + errorMessageId: keyof typeof errorMessages; + }; + const getRedundantPipeCall = ( pipeCall: CallExpressionWithExpressionArgs - ): O.Option => { + ): O.Option => { 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; } @@ -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", diff --git a/tests/rules/no-redundant-pipe.test.ts b/tests/rules/no-redundant-pipe.test.ts index b663348..b56114c 100644 --- a/tests/rules/no-redundant-pipe.test.ts +++ b/tests/rules/no-redundant-pipe.test.ts @@ -28,7 +28,7 @@ pipe(value); `, errors: [ { - messageId: "redundantPipe", + messageId: "redundantPipeWithSingleArg", suggestions: [ { messageId: "removePipe", @@ -48,7 +48,7 @@ pipe(pipe(pipe(value, fn1), fn2), fn3); `, errors: [ { - messageId: "redundantPipe", + messageId: "redundantPipeWithSingleArgInsidePipe", suggestions: [ { messageId: "removePipe", @@ -60,7 +60,7 @@ pipe(pipe(value, fn1), fn2, fn3); ], }, { - messageId: "redundantPipe", + messageId: "redundantPipeWithSingleArgInsidePipe", suggestions: [ { messageId: "removePipe", @@ -80,7 +80,7 @@ pipe(pipe(value, fn1), fn2, fn3); `, errors: [ { - messageId: "redundantPipe", + messageId: "redundantPipeWithSingleArgInsidePipe", suggestions: [ { messageId: "removePipe", @@ -103,7 +103,7 @@ pipe( );`, errors: [ { - messageId: "redundantPipe", + messageId: "redundantPipeWithSingleArgInsidePipe", suggestions: [ { messageId: "removePipe", @@ -128,7 +128,7 @@ pipe( `, errors: [ { - messageId: "redundantPipe", + messageId: "redundantPipeWithSingleArg", suggestions: [ { messageId: "removePipe", @@ -151,7 +151,7 @@ pipe( `, errors: [ { - messageId: "redundantPipe", + messageId: "redundantPipeWithSingleArg", suggestions: [ { messageId: "removePipe",