Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn folding an either into an option into an option.fromEither #64

Open
wants to merge 32 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
f6bb3fe
Start turning folding an either into an option into an option.fromEither
thewilkybarkid Feb 11, 2021
415aa5b
Only work on option.fold
thewilkybarkid Feb 12, 2021
a864859
Match project style more
thewilkybarkid Feb 12, 2021
dcecac3
Extract some functions
thewilkybarkid Feb 12, 2021
d7c8729
Make use of functions
thewilkybarkid Feb 12, 2021
89ea792
Use a pipe
thewilkybarkid Feb 12, 2021
7ec2f9f
More pipes
thewilkybarkid Feb 12, 2021
a3346ab
Filter out undefined
thewilkybarkid Feb 12, 2021
c696acf
Separate as a filter
thewilkybarkid Feb 12, 2021
f5c0bcf
Refactoring
thewilkybarkid Feb 12, 2021
345146b
Use Do notation
thewilkybarkid Feb 12, 2021
484b51e
Use a simpler pipe
thewilkybarkid Feb 12, 2021
c0175cc
Extract some functions
thewilkybarkid Feb 12, 2021
b388177
Extract a pipe
thewilkybarkid Feb 12, 2021
5a48836
Improve scoping and use short returns
thewilkybarkid Feb 12, 2021
ff96c47
Use types to check Either
thewilkybarkid Feb 13, 2021
3d5a844
Safely determine Option and function modules
thewilkybarkid Feb 15, 2021
2847883
Separate finding the member expression from whether it's option.none
thewilkybarkid Feb 15, 2021
d30b8a4
Set the right namespace
thewilkybarkid Feb 15, 2021
93332ee
Extract isCall and isValue
thewilkybarkid Feb 16, 2021
fa53426
Extract isLazyValue and isConstantCall
thewilkybarkid Feb 16, 2021
8683809
Use isCall everywhere
thewilkybarkid Feb 16, 2021
121bd6f
Rewrite isOptionSomeValue
thewilkybarkid Feb 16, 2021
e81e8e3
Really use isCall everywhere
thewilkybarkid Feb 16, 2021
c22df28
Simplify ensuring arguments
thewilkybarkid Feb 16, 2021
fc4e4ef
Move utilities out of the module
thewilkybarkid Feb 17, 2021
9cf00de
Add to documentation and configuration
thewilkybarkid Feb 17, 2021
ebf3ad7
Move context-aware utilities into contextUtils
thewilkybarkid Feb 17, 2021
a4a384e
Simplify utility changes
thewilkybarkid Feb 17, 2021
3fbaedb
Merge branch 'main' into either-fold-to-option
thewilkybarkid Feb 17, 2021
cb92266
CS tweaks
thewilkybarkid Feb 17, 2021
8b5d086
Merge branch 'main' into either-fold-to-option
thewilkybarkid May 5, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 150 additions & 0 deletions src/rules/prefer-constructor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import {
AST_NODE_TYPES,
TSESTree,
} from "@typescript-eslint/experimental-utils";
import { constant, constVoid, flow, pipe } from 'fp-ts/function';
import { boolean, option, readonlyNonEmptyArray } from "fp-ts";
import {
calleeIdentifier,
contextUtils,
createRule
} from '../utils';

export default createRule({
name: "prefer-constructor",
meta: {
type: "suggestion",
fixable: "code",
schema: [],
docs: {
category: "Best Practices",
description: "afsafaf",
recommended: "warn",
},
messages: {
eitherFoldIsOptionFromEither: "cacsaffg",
replaceEitherFoldWithOptionFromEither: "dsagdgdsg",
},
},
defaultOptions: [],
create(context) {
const { isIdentifierImportedFrom } = contextUtils(context);
return {
CallExpression(node) {
const isEitherFold = (node: TSESTree.CallExpression) => {
return pipe(
node,
calleeIdentifier,
option.filter((callee) => callee.name === "fold"),
option.chain(flow((callee) => callee.parent, option.fromNullable)),
option.filter((parent): parent is TSESTree.MemberExpression => parent.type === AST_NODE_TYPES.MemberExpression),
option.map((parent) => parent.object),
option.filter((object): object is TSESTree.Identifier => object.type === AST_NODE_TYPES.Identifier),
option.exists((object) => object.name === 'either')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently looking for a way to determine if it's either.fold, even if it's E.fold or whatever.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two ways:

  • use the type information (more accurate, but slower)
  • determine whether the "prefix" is imported from fp-ts (accurate enough, and faster because it's only a syntactic lookup)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take a closer look as soon as I have minute

);
}

const isOptionNoneArrowFunctionExpression = (node: TSESTree.ArrowFunctionExpression) => {
return pipe(
node.body,
option.of,
option.filter((arg): arg is TSESTree.MemberExpression => arg.type === AST_NODE_TYPES.MemberExpression),
option.exists(isOptionNoneMemberExpression)
)
}

const isOptionNoneMemberExpression = (node: TSESTree.MemberExpression) => {
return pipe(
node,
option.of,
option.filter(() => node.object?.type === AST_NODE_TYPES.Identifier && node.object.name === 'option'),
option.filter(() => node.property?.type === AST_NODE_TYPES.Identifier && node.property.name === 'none'),
option.isSome
)
}

const isOptionNoneCallExpression = (node: TSESTree.CallExpression) => {
return pipe(
node,
calleeIdentifier,
option.filter(
(callee) =>
callee.name === "constant" &&
isIdentifierImportedFrom(callee, /fp-ts\//, context)
),
option.map(constant(node.arguments[0])),
option.chain(option.fromNullable),
option.filter((arg): arg is TSESTree.MemberExpression => arg.type === AST_NODE_TYPES.MemberExpression),
option.exists(isOptionNoneMemberExpression)
)
}

const isOptionNone = (node: TSESTree.Expression) => {
switch (node.type) {
case AST_NODE_TYPES.ArrowFunctionExpression:
return isOptionNoneArrowFunctionExpression(node);
case AST_NODE_TYPES.CallExpression:
return isOptionNoneCallExpression(node);
case AST_NODE_TYPES.MemberExpression:
return isOptionNoneMemberExpression(node);
default:
return false;
}
};

const isOptionSomeValue = (node: TSESTree.Expression) => {
return pipe(
node,
option.of,
option.map((n) => n.type === AST_NODE_TYPES.ArrowFunctionExpression && n.body.type === AST_NODE_TYPES.CallExpression ? n.body.callee : n),
option.filter((n): n is TSESTree.MemberExpression => n.type === AST_NODE_TYPES.MemberExpression),
option.exists(
(body) =>
body.object?.type === AST_NODE_TYPES.Identifier && body.object.name === 'option'
&&
body.property?.type === AST_NODE_TYPES.Identifier && body.property.name === 'some'
&&
(body.parent?.type !== AST_NODE_TYPES.CallExpression || body.parent.parent?.type !== AST_NODE_TYPES.ArrowFunctionExpression
||
(body.parent.arguments.length === 1 && body.parent.parent.params.length === 1 && body.parent.arguments[0]?.type === AST_NODE_TYPES.Identifier && body.parent.parent.params[0]?.type === AST_NODE_TYPES.Identifier && body.parent.arguments[0].name === body.parent.parent.params[0].name))
),
);
}
pipe(
node,
isEitherFold,
boolean.fold(constVoid, () =>
pipe(
readonlyNonEmptyArray.fromArray(node.arguments),
option.filter((args) => args.length === 2),
option.filter(flow(readonlyNonEmptyArray.head, isOptionNone)),
option.filter(flow(readonlyNonEmptyArray.last, isOptionSomeValue)),
option.map(() => {
context.report({
loc: {
start: node.loc.start,
end: node.loc.end,
},
messageId: "eitherFoldIsOptionFromEither",
suggest: [
{
messageId: "replaceEitherFoldWithOptionFromEither",
fix(fixer) {
return [
fixer.replaceTextRange(
node.range,
`option.fromEither`
),
];
},
},
],
});
})
)
)
);
},
};
},
});
137 changes: 137 additions & 0 deletions tests/rules/prefer-constructor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { stripIndent } from 'common-tags';
import rule from "../../src/rules/prefer-constructor";
import { ESLintUtils } from "@typescript-eslint/experimental-utils";

const ruleTester = new ESLintUtils.RuleTester({
parser: "@typescript-eslint/parser",
});

ruleTester.run("prefer-constructor", rule, {
valid: [
{
code: stripIndent`
import { either, option } from "fp-ts"
import { pipe } from "fp-ts/function"

pipe(
either.of(1),
either.fold(() => option.none, (value) => option.some(otherValue))
)
`,
},
{
code: stripIndent`
import { either, option } from "fp-ts"
import { pipe } from "fp-ts/function"

pipe(
either.of(1),
either.fold(() => option.some(otherValue), (value) => option.some(value))
)
`,
},
{
code: stripIndent`
import { either, option } from "fp-ts"
import { pipe } from "fp-ts/function"

pipe(
either.of(1),
either.fold(() => option.some(otherValue), () => option.none)
)
`,
},
],
invalid: [
{
code: stripIndent`
import { either, option } from "fp-ts"
import { pipe } from "fp-ts/function"

pipe(
either.of(1),
either.fold(() => option.none, (value) => option.some(value))
)
`,
errors: [
{
messageId: "eitherFoldIsOptionFromEither",
suggestions: [
{
messageId: "replaceEitherFoldWithOptionFromEither",
output: stripIndent`
import { either, option } from "fp-ts"
import { pipe } from "fp-ts/function"

pipe(
either.of(1),
option.fromEither
)
`,
},
],
},
],
},
{
code: stripIndent`
import { either, option } from "fp-ts"
import { pipe } from "fp-ts/function"

pipe(
either.of(1),
either.fold(() => option.none, option.some)
)
`,
errors: [
{
messageId: "eitherFoldIsOptionFromEither",
suggestions: [
{
messageId: "replaceEitherFoldWithOptionFromEither",
output: stripIndent`
import { either, option } from "fp-ts"
import { pipe } from "fp-ts/function"

pipe(
either.of(1),
option.fromEither
)
`,
},
],
},
],
},
{
code: stripIndent`
import { either, option } from "fp-ts"
import { constant, pipe } from "fp-ts/function"

pipe(
either.of(1),
either.fold(constant(option.none), option.some)
)
`,
errors: [
{
messageId: "eitherFoldIsOptionFromEither",
suggestions: [
{
messageId: "replaceEitherFoldWithOptionFromEither",
output: stripIndent`
import { either, option } from "fp-ts"
import { constant, pipe } from "fp-ts/function"

pipe(
either.of(1),
option.fromEither
)
`,
},
],
},
],
},
],
});