UNPKG

eslint-plugin-fp-ts

Version:

fp-ts ESLint rules

89 lines (88 loc) 4.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var utils_1 = require("@typescript-eslint/utils"); var fp_ts_1 = require("fp-ts"); var function_1 = require("fp-ts/function"); var utils_2 = require("../utils"); exports.default = (0, utils_2.createRule)({ name: "prefer-bimap", meta: { type: "suggestion", fixable: "code", hasSuggestions: true, schema: [], docs: { description: "Replace map + mapLeft with bimap", }, messages: { mapMapLeftIsBimap: "{{firstNode}} followed by {{secondNode}} can be replaced by bimap", replaceMapMapLeftBimap: "replace {{firstNode}} and {{secondNode}} with bimap", }, }, defaultOptions: [], create: function (context) { var isPipeOrFlowExpression = (0, utils_2.contextUtils)(context).isPipeOrFlowExpression; return { CallExpression: function (node) { var mapThenMapLeft = function () { return (0, utils_2.getAdjacentCombinators)(node, { name: "map", types: [utils_1.AST_NODE_TYPES.CallExpression], }, { name: "mapLeft", types: [utils_1.AST_NODE_TYPES.CallExpression], }, true); }; var mapLeftThenMap = function () { return (0, utils_2.getAdjacentCombinators)(node, { name: "mapLeft", types: [utils_1.AST_NODE_TYPES.CallExpression], }, { name: "map", types: [utils_1.AST_NODE_TYPES.CallExpression], }, true); }; (0, function_1.pipe)(node, isPipeOrFlowExpression, fp_ts_1.boolean.fold(function_1.constVoid, function () { return (0, function_1.pipe)(mapThenMapLeft(), fp_ts_1.option.alt(mapLeftThenMap), fp_ts_1.option.bindTo("combinators"), fp_ts_1.option.bind("calleeIdentifiers", function (_a) { var combinators = _a.combinators; return fp_ts_1.apply.sequenceT(fp_ts_1.option.option)((0, utils_2.calleeIdentifier)(combinators[0]), (0, utils_2.calleeIdentifier)(combinators[1])); }), fp_ts_1.option.map(function (_a) { var combinators = _a.combinators, calleeIdentifiers = _a.calleeIdentifiers; context.report({ loc: { start: combinators[0].loc.start, end: combinators[1].loc.end, }, messageId: "mapMapLeftIsBimap", data: { firstNode: calleeIdentifiers[0].name, secondNode: calleeIdentifiers[1].name, }, suggest: [ { messageId: "replaceMapMapLeftBimap", data: { firstNode: calleeIdentifiers[0].name, secondNode: calleeIdentifiers[1].name, }, fix: function (fixer) { var mapFirst = calleeIdentifiers[0].name === "map"; var mapNode = mapFirst ? combinators[0] : combinators[1]; var mapLeftNode = mapFirst ? combinators[1] : combinators[0]; return [ fixer.replaceTextRange([combinators[0].range[0], combinators[1].range[1]], "".concat((0, utils_2.prettyPrint)(mapNode.callee).replace(/map$/, "bimap"), "(\n").concat((0, utils_2.inferIndent)(mapNode), " ").concat((0, utils_2.prettyPrint)(mapLeftNode.arguments[0]), ",\n").concat((0, utils_2.inferIndent)(mapNode), " ").concat((0, utils_2.prettyPrint)(mapNode.arguments[0]), "\n").concat((0, utils_2.inferIndent)(mapNode), ")")), ]; }, }, ], }); })); })); }, }; }, });