From b76044afa46f006869624450daacc46bf2034ed5 Mon Sep 17 00:00:00 2001 From: Oliver Joseph Ash Date: Sat, 11 Dec 2021 10:13:33 +0000 Subject: [PATCH] Code for fix is generated by AST node rather than text manipulation Fixes https://github.com/buildo/eslint-plugin-fp-ts/issues/222 --- src/rules/no-redundant-flow.ts | 26 ++++++++++++++++++++------ tests/rules/no-redundant-flow.test.ts | 8 ++------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/rules/no-redundant-flow.ts b/src/rules/no-redundant-flow.ts index e013bbb..7054527 100644 --- a/src/rules/no-redundant-flow.ts +++ b/src/rules/no-redundant-flow.ts @@ -5,7 +5,7 @@ import { import { pipe } from "fp-ts/function"; import * as NonEmptyArray from "fp-ts/NonEmptyArray"; import * as O from "fp-ts/Option"; -import { contextUtils, createRule } from "../utils"; +import { contextUtils, createRule, prettyPrint } from "../utils"; const getArgumentExpression = ( x: TSESTree.CallExpressionArgument @@ -54,6 +54,19 @@ export default createRule({ ) : O.none; + const createSequenceExpressionFromFlowCall = ( + flowCall: FlowCallWithExpressionArgs + ): TSESTree.SequenceExpression => { + const firstArg = pipe(flowCall.args, NonEmptyArray.head); + const lastArg = pipe(flowCall.args, NonEmptyArray.last); + return { + loc: flowCall.node.loc, + range: [firstArg.range[0], lastArg.range[1]], + type: AST_NODE_TYPES.SequenceExpression, + expressions: flowCall.args, + }; + }; + return { CallExpression(node) { pipe( @@ -68,12 +81,13 @@ export default createRule({ { messageId: "removeFlow", fix(fixer) { + const sequenceExpression = + createSequenceExpressionFromFlowCall(redundantFlowCall); return [ - fixer.removeRange([ - node.callee.range[0], - node.callee.range[1] + 1, - ]), - fixer.removeRange([node.range[1] - 1, node.range[1]]), + fixer.replaceText( + redundantFlowCall.node, + prettyPrint(sequenceExpression) + ), ]; }, }, diff --git a/tests/rules/no-redundant-flow.test.ts b/tests/rules/no-redundant-flow.test.ts index b0a4777..2618be3 100644 --- a/tests/rules/no-redundant-flow.test.ts +++ b/tests/rules/no-redundant-flow.test.ts @@ -59,9 +59,7 @@ const a = flow( messageId: "removeFlow", output: ` import { flow } from "fp-ts/function" -const a = ${""} - foo - +const a = foo `, }, ], @@ -83,9 +81,7 @@ const a = flow( messageId: "removeFlow", output: ` import { flow } from "fp-ts/function" -const a = - foo -; +const a = foo; `, }, ],