-
Notifications
You must be signed in to change notification settings - Fork 9
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
thewilkybarkid
wants to merge
32
commits into
buildo:main
Choose a base branch
from
thewilkybarkid:either-fold-to-option
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 25 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 415aa5b
Only work on option.fold
thewilkybarkid a864859
Match project style more
thewilkybarkid dcecac3
Extract some functions
thewilkybarkid d7c8729
Make use of functions
thewilkybarkid 89ea792
Use a pipe
thewilkybarkid 7ec2f9f
More pipes
thewilkybarkid a3346ab
Filter out undefined
thewilkybarkid c696acf
Separate as a filter
thewilkybarkid f5c0bcf
Refactoring
thewilkybarkid 345146b
Use Do notation
thewilkybarkid 484b51e
Use a simpler pipe
thewilkybarkid c0175cc
Extract some functions
thewilkybarkid b388177
Extract a pipe
thewilkybarkid 5a48836
Improve scoping and use short returns
thewilkybarkid ff96c47
Use types to check Either
thewilkybarkid 3d5a844
Safely determine Option and function modules
thewilkybarkid 2847883
Separate finding the member expression from whether it's option.none
thewilkybarkid d30b8a4
Set the right namespace
thewilkybarkid 93332ee
Extract isCall and isValue
thewilkybarkid fa53426
Extract isLazyValue and isConstantCall
thewilkybarkid 8683809
Use isCall everywhere
thewilkybarkid 121bd6f
Rewrite isOptionSomeValue
thewilkybarkid e81e8e3
Really use isCall everywhere
thewilkybarkid c22df28
Simplify ensuring arguments
thewilkybarkid fc4e4ef
Move utilities out of the module
thewilkybarkid 9cf00de
Add to documentation and configuration
thewilkybarkid ebf3ad7
Move context-aware utilities into contextUtils
thewilkybarkid a4a384e
Simplify utility changes
thewilkybarkid 3fbaedb
Merge branch 'main' into either-fold-to-option
thewilkybarkid cb92266
CS tweaks
thewilkybarkid 8b5d086
Merge branch 'main' into either-fold-to-option
thewilkybarkid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/experimental-utils" | ||
import { option, readonlyArray, readonlyNonEmptyArray } from "fp-ts" | ||
import { constant, flow, pipe } from "fp-ts/function" | ||
import { calleeIdentifier, ContextUtils, contextUtils, createRule, isCallee, isFromModule, Module } from "../utils" | ||
|
||
const hasName = (name: string) => (identifier: TSESTree.Identifier) => identifier.name === name | ||
|
||
const hasLength = (length: number) => <T>(array: ReadonlyArray<T>) => array.length === length | ||
|
||
const isArrowFunctionExpression = (node: TSESTree.Node): node is TSESTree.ArrowFunctionExpression => node.type === AST_NODE_TYPES.ArrowFunctionExpression | ||
|
||
const isCallExpression = (node: TSESTree.Node): node is TSESTree.CallExpression => node.type === AST_NODE_TYPES.CallExpression | ||
|
||
const isMemberExpression = (node: TSESTree.Node): node is TSESTree.MemberExpression => node.type === AST_NODE_TYPES.MemberExpression | ||
|
||
const isIdentifier = (node: TSESTree.Node): node is TSESTree.Identifier => node.type === AST_NODE_TYPES.Identifier | ||
|
||
const getArguments = (call: TSESTree.CallExpression) => pipe( | ||
call.arguments, | ||
readonlyNonEmptyArray.fromArray | ||
) | ||
|
||
const getFirstArgument = (call: TSESTree.CallExpression) => pipe( | ||
call, | ||
getArguments, | ||
option.map(readonlyNonEmptyArray.head) | ||
) | ||
|
||
const getParams = (call: TSESTree.FunctionLike) => pipe( | ||
call.params, | ||
readonlyNonEmptyArray.fromArray | ||
) | ||
|
||
const getFirstParam = (call: TSESTree.FunctionLike) => pipe( | ||
call, | ||
getParams, | ||
option.map(readonlyNonEmptyArray.head) | ||
) | ||
|
||
const isIdentifierWithName = (name: string) => (node: TSESTree.Node) => pipe( | ||
node, | ||
option.of, | ||
option.filter(isIdentifier), | ||
option.exists(hasName(name)) | ||
) | ||
|
||
const hasPropertyIdentifierWithName = (name: string) => (node: TSESTree.MemberExpression) => pipe( | ||
node.property, | ||
isIdentifierWithName(name) | ||
) | ||
|
||
const getWrappedCall = (node: TSESTree.ArrowFunctionExpression) => pipe( | ||
node.body, | ||
option.of, | ||
option.filter(isCallExpression), | ||
option.filter(flow( | ||
option.of, | ||
option.bindTo("call"), | ||
option.bind("param", () => pipe(node, getFirstParam, option.filter(isIdentifier))), | ||
option.exists(({ call, param }) => pipe(call, hasArguments([isIdentifierWithName(param.name)]))) | ||
)) | ||
) | ||
|
||
const isCall = <T extends TSESTree.Node>(utils: ContextUtils, module: Module, name: string) => (node: T) => pipe( | ||
node, | ||
option.fromPredicate(isArrowFunctionExpression), | ||
option.chain(getWrappedCall), | ||
option.altW(constant(option.some(node))), | ||
option.filter(isCallee), | ||
option.exists(isCallTo(utils, module, name)) | ||
) | ||
|
||
const isCallTo = ({ typeOfNode }: ContextUtils, module: Module, name: string) => flow( | ||
calleeIdentifier, | ||
option.filter(hasName(name)), | ||
option.chain(typeOfNode), | ||
option.exists(isFromModule(module)) | ||
) | ||
|
||
const isLazyValue = (utils: ContextUtils, module: Module, name: string) => flow( | ||
findMemberExpression(utils), | ||
option.exists(isValue(utils, module, name)) | ||
) | ||
|
||
const isValue = ({ typeOfNode }: ContextUtils, module: Module, name: string) => (node: TSESTree.MemberExpression) => pipe( | ||
node, | ||
option.of, | ||
option.filter(hasPropertyIdentifierWithName(name)), | ||
option.chain(typeOfNode), | ||
option.exists(isFromModule(module)) | ||
) | ||
|
||
const findMemberExpressionFromArrowFunctionExpression = (node: TSESTree.ArrowFunctionExpression) => pipe( | ||
node.body, | ||
option.of, | ||
option.filter(isMemberExpression) | ||
) | ||
|
||
const isConstantCall = ({ typeOfNode }: ContextUtils) => (node: TSESTree.CallExpression) => pipe( | ||
node, | ||
calleeIdentifier, | ||
option.filter(hasName("constant")), | ||
option.chain(typeOfNode), | ||
option.exists(isFromModule("function")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could probably just check it’s from fp-ts rather than the type check. |
||
) | ||
|
||
const findMemberExpressionFromCallExpression = (utils: ContextUtils) => (node: TSESTree.CallExpression) => pipe( | ||
node, | ||
option.of, | ||
option.filter(isConstantCall(utils)), | ||
option.chain(getFirstArgument), | ||
option.filter(isMemberExpression) | ||
) | ||
|
||
const findMemberExpression = (utils: ContextUtils) => (node: TSESTree.Expression) => { | ||
switch (node.type) { | ||
case AST_NODE_TYPES.ArrowFunctionExpression: | ||
return findMemberExpressionFromArrowFunctionExpression(node) | ||
case AST_NODE_TYPES.CallExpression: | ||
return findMemberExpressionFromCallExpression(utils)(node) | ||
default: | ||
return option.none | ||
} | ||
} | ||
|
||
const findNamespace = (utils: ContextUtils) => flow( | ||
findMemberExpression(utils), | ||
option.map((node) => node.object), | ||
option.filter(isIdentifier), | ||
option.map((identifier) => identifier.name) | ||
) | ||
|
||
const hasArguments = (args: ReadonlyArray<(node: TSESTree.Expression) => boolean>) => flow( | ||
ensureArguments(args), | ||
option.isSome | ||
) | ||
|
||
const ensureArguments = (args: ReadonlyArray<(node: TSESTree.Expression) => boolean>) => flow( | ||
getArguments, | ||
option.filter(pipe(args.length, hasLength)), | ||
option.chain( | ||
readonlyNonEmptyArray.traverseWithIndex(option.option)( | ||
(i, value) => pipe(args, readonlyArray.lookup(i), option.filter(test => test(value)), option.map(constant(value))) | ||
) | ||
) | ||
) | ||
|
||
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 utils = contextUtils(context) | ||
return { | ||
CallExpression(node) { | ||
pipe( | ||
node, | ||
option.of, | ||
option.filter(isCall(utils, "Either", "fold")), | ||
option.chain(ensureArguments([ | ||
isLazyValue(utils, "Option", "none"), | ||
isCall(utils, "Option", "some") | ||
])), | ||
option.bind("namespace", flow(readonlyNonEmptyArray.head, findNamespace(utils))), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't ideal, would rather use the imports than reading the code. |
||
option.map(({ namespace }) => { | ||
context.report({ | ||
loc: { | ||
start: node.loc.start, | ||
end: node.loc.end | ||
}, | ||
messageId: "eitherFoldIsOptionFromEither", | ||
suggest: [ | ||
{ | ||
messageId: "replaceEitherFoldWithOptionFromEither", | ||
fix(fixer) { | ||
return [ | ||
fixer.replaceTextRange( | ||
node.range, | ||
`${namespace}.fromEither` | ||
) | ||
] | ||
} | ||
} | ||
] | ||
}) | ||
}) | ||
) | ||
} | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There’s a generic here due to type inference problems.