|
| 1 | +import { |
| 2 | + AST_NODE_TYPES, |
| 3 | + TSESTree, |
| 4 | +} from "@typescript-eslint/experimental-utils"; |
| 5 | +import { boolean, option, apply } from "fp-ts"; |
| 6 | +import { constVoid, pipe } from "fp-ts/function"; |
| 7 | +import { |
| 8 | + calleeIdentifier, |
| 9 | + contextUtils, |
| 10 | + createRule, |
| 11 | + getAdjacentCombinators, |
| 12 | + inferIndent, |
| 13 | + prettyPrint, |
| 14 | +} from "../utils"; |
| 15 | + |
| 16 | +export default createRule({ |
| 17 | + name: "prefer-bimap", |
| 18 | + meta: { |
| 19 | + type: "suggestion", |
| 20 | + fixable: "code", |
| 21 | + schema: [], |
| 22 | + docs: { |
| 23 | + category: "Best Practices", |
| 24 | + description: "Replace map + mapLeft with bimap", |
| 25 | + recommended: "warn", |
| 26 | + }, |
| 27 | + messages: { |
| 28 | + mapMapLeftIsBimap: |
| 29 | + "{{firstNode}} followed by {{secondNode}} can be replaced by bimap", |
| 30 | + replaceMapMapLeftBimap: |
| 31 | + "replace {{firstNode}} and {{secondNode}} with bimap", |
| 32 | + }, |
| 33 | + }, |
| 34 | + defaultOptions: [], |
| 35 | + create(context) { |
| 36 | + const { isPipeOrFlowExpression } = contextUtils(context); |
| 37 | + |
| 38 | + return { |
| 39 | + CallExpression(node) { |
| 40 | + const mapThenMapLeft = () => |
| 41 | + getAdjacentCombinators< |
| 42 | + TSESTree.CallExpression, |
| 43 | + TSESTree.CallExpression |
| 44 | + >( |
| 45 | + node, |
| 46 | + { |
| 47 | + name: "map", |
| 48 | + types: [AST_NODE_TYPES.CallExpression], |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "mapLeft", |
| 52 | + types: [AST_NODE_TYPES.CallExpression], |
| 53 | + }, |
| 54 | + true |
| 55 | + ); |
| 56 | + |
| 57 | + const mapLeftThenMap = () => |
| 58 | + getAdjacentCombinators< |
| 59 | + TSESTree.CallExpression, |
| 60 | + TSESTree.CallExpression |
| 61 | + >( |
| 62 | + node, |
| 63 | + { |
| 64 | + name: "mapLeft", |
| 65 | + types: [AST_NODE_TYPES.CallExpression], |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "map", |
| 69 | + types: [AST_NODE_TYPES.CallExpression], |
| 70 | + }, |
| 71 | + true |
| 72 | + ); |
| 73 | + |
| 74 | + pipe( |
| 75 | + node, |
| 76 | + isPipeOrFlowExpression, |
| 77 | + boolean.fold(constVoid, () => |
| 78 | + pipe( |
| 79 | + mapThenMapLeft(), |
| 80 | + option.alt(mapLeftThenMap), |
| 81 | + option.bindTo("combinators"), |
| 82 | + option.bind("calleeIdentifiers", ({ combinators }) => |
| 83 | + apply.sequenceT(option.option)( |
| 84 | + calleeIdentifier(combinators[0]), |
| 85 | + calleeIdentifier(combinators[1]) |
| 86 | + ) |
| 87 | + ), |
| 88 | + option.map(({ combinators, calleeIdentifiers }) => { |
| 89 | + context.report({ |
| 90 | + loc: { |
| 91 | + start: combinators[0].loc.start, |
| 92 | + end: combinators[1].loc.end, |
| 93 | + }, |
| 94 | + messageId: "mapMapLeftIsBimap", |
| 95 | + data: { |
| 96 | + firstNode: calleeIdentifiers[0].name, |
| 97 | + secondNode: calleeIdentifiers[1].name, |
| 98 | + }, |
| 99 | + suggest: [ |
| 100 | + { |
| 101 | + messageId: "replaceMapMapLeftBimap", |
| 102 | + data: { |
| 103 | + firstNode: calleeIdentifiers[0].name, |
| 104 | + secondNode: calleeIdentifiers[1].name, |
| 105 | + }, |
| 106 | + fix(fixer) { |
| 107 | + const mapFirst = calleeIdentifiers[0].name === "map"; |
| 108 | + const mapNode = mapFirst |
| 109 | + ? combinators[0] |
| 110 | + : combinators[1]; |
| 111 | + const mapLeftNode = mapFirst |
| 112 | + ? combinators[1] |
| 113 | + : combinators[0]; |
| 114 | + return [ |
| 115 | + fixer.replaceTextRange( |
| 116 | + [combinators[0].range[0], combinators[1].range[1]], |
| 117 | + `${prettyPrint(mapNode.callee).replace( |
| 118 | + /map$/, |
| 119 | + "bimap" |
| 120 | + )}(\n${inferIndent(mapNode)} ${prettyPrint( |
| 121 | + mapLeftNode.arguments[0]! |
| 122 | + )},\n${inferIndent(mapNode)} ${prettyPrint( |
| 123 | + mapNode.arguments[0]! |
| 124 | + )}\n${inferIndent(mapNode)})` |
| 125 | + ), |
| 126 | + ]; |
| 127 | + }, |
| 128 | + }, |
| 129 | + ], |
| 130 | + }); |
| 131 | + }) |
| 132 | + ) |
| 133 | + ) |
| 134 | + ); |
| 135 | + }, |
| 136 | + }; |
| 137 | + }, |
| 138 | +}); |
0 commit comments