Skip to content

Commit

Permalink
Merge pull request #224 from OliverJAsh/oja/no-redundant-flow/fix-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gabro authored Dec 13, 2021
2 parents 58d6998 + 66cbbfc commit 419166b
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 23 deletions.
60 changes: 40 additions & 20 deletions src/rules/no-redundant-flow.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { contextUtils, createRule } from "../utils";
import { pipe } from "fp-ts/function";
import * as O from "fp-ts/Option";
import {
contextUtils,
createRule,
createSequenceExpressionFromCallExpressionWithExpressionArgs,
getCallExpressionWithExpressionArgs,
prettyPrint,
} from "../utils";

export default createRule({
name: "no-redundant-flow",
Expand All @@ -22,26 +30,38 @@ export default createRule({

return {
CallExpression(node) {
if (node.arguments.length === 1 && isFlowExpression(node)) {
context.report({
node,
messageId: "redundantFlow",
suggest: [
{
messageId: "removeFlow",
fix(fixer) {
return [
fixer.removeRange([
node.callee.range[0],
node.callee.range[1] + 1,
]),
fixer.removeRange([node.range[1] - 1, node.range[1]]),
];
pipe(
node,
O.fromPredicate(isFlowExpression),
/**
* We ignore flow calls which contain a spread argument because these are never invalid.
*/
O.chain(getCallExpressionWithExpressionArgs),
O.filter((flowCall) => flowCall.node.arguments.length === 1),
O.map((redundantFlowCall) => {
context.report({
node: redundantFlowCall.node,
messageId: "redundantFlow",
suggest: [
{
messageId: "removeFlow",
fix(fixer) {
const sequenceExpression =
createSequenceExpressionFromCallExpressionWithExpressionArgs(
redundantFlowCall
);
return [
fixer.replaceText(
redundantFlowCall.node,
prettyPrint(sequenceExpression)
),
];
},
},
},
],
});
}
],
});
})
);
},
};
},
Expand Down
50 changes: 50 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
} from "@typescript-eslint/experimental-utils/dist/ts-eslint";
import ts from "typescript";
import { Option } from "fp-ts/Option";
import * as NonEmptyArray from "fp-ts/NonEmptyArray";
import * as O from "fp-ts/Option";

declare module "typescript" {
interface TypeChecker {
Expand Down Expand Up @@ -471,3 +473,51 @@ export const contextUtils = <
parserServices,
};
};

/**
* Ideally we could implement this predicate in terms of an existing
* `isExpression` predicate but it seems like this doesn't exist anywhere.
*
* There is an `isExpression` in `tsutils`. However, in the TS AST, spread is
* classed as an expression (!).
*/
const getArgumentExpression = (
x: TSESTree.CallExpressionArgument
): O.Option<TSESTree.Expression> =>
x.type !== AST_NODE_TYPES.SpreadElement ? O.some(x) : O.none;

const checkIsArgumentExpression = O.getRefinement(getArgumentExpression);

type CallExpressionWithExpressionArgs = {
node: TSESTree.CallExpression;
args: NonEmptyArray.NonEmptyArray<TSESTree.Expression>;
};

export const getCallExpressionWithExpressionArgs = (
node: TSESTree.CallExpression
): O.Option<CallExpressionWithExpressionArgs> =>
node.arguments.every(checkIsArgumentExpression)
? pipe(
node.arguments,
NonEmptyArray.fromArray,
O.map(
(args): CallExpressionWithExpressionArgs => ({
node,
args,
})
)
)
: O.none;

export const createSequenceExpressionFromCallExpressionWithExpressionArgs = (
call: CallExpressionWithExpressionArgs
): TSESTree.SequenceExpression => {
const firstArg = pipe(call.args, NonEmptyArray.head);
const lastArg = pipe(call.args, NonEmptyArray.last);
return {
loc: call.node.loc,
range: [firstArg.range[0], lastArg.range[1]],
type: AST_NODE_TYPES.SequenceExpression,
expressions: call.args,
};
};
29 changes: 26 additions & 3 deletions tests/rules/no-redundant-flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ ruleTester.run("no-redundant-flow", rule, {
bar
)
`,
`import { flow } from "fp-ts/function"
flow(...fns)
`,
],
invalid: [
{
Expand Down Expand Up @@ -56,14 +59,34 @@ const a = flow(
messageId: "removeFlow",
output: `
import { flow } from "fp-ts/function"
const a = ${""}
foo
const a = foo
`,
},
],
},
],
},
{
code: `
import { flow } from "fp-ts/function"
const a = flow(
foo,
);
`,
errors: [
{
messageId: "redundantFlow",
suggestions: [
{
messageId: "removeFlow",
output: `
import { flow } from "fp-ts/function"
const a = foo;
`,
},
],
},
],
}
],
});

0 comments on commit 419166b

Please sign in to comment.