Skip to content

Commit

Permalink
Code for fix is generated by AST node rather than text manipulation
Browse files Browse the repository at this point in the history
Fixes #222
  • Loading branch information
OliverJAsh committed Dec 11, 2021
1 parent 06168f2 commit b76044a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
26 changes: 20 additions & 6 deletions src/rules/no-redundant-flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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)
),
];
},
},
Expand Down
8 changes: 2 additions & 6 deletions tests/rules/no-redundant-flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ const a = flow(
messageId: "removeFlow",
output: `
import { flow } from "fp-ts/function"
const a = ${""}
foo
const a = foo
`,
},
],
Expand All @@ -83,9 +81,7 @@ const a = flow(
messageId: "removeFlow",
output: `
import { flow } from "fp-ts/function"
const a =
foo
;
const a = foo;
`,
},
],
Expand Down

0 comments on commit b76044a

Please sign in to comment.