UNPKG

eslint-plugin-fp-ts

Version:

fp-ts ESLint rules

73 lines (72 loc) 3.37 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-chain", meta: { type: "suggestion", fixable: "code", hasSuggestions: true, schema: [], docs: { description: "Replace map + flatten with chain", }, messages: { mapFlattenIsChain: "map followed by flatten can be replaced by chain", replaceMapFlattenWithChain: "replace map and flatten with chain", }, }, defaultOptions: [], create: function (context) { var isPipeOrFlowExpression = (0, utils_2.contextUtils)(context).isPipeOrFlowExpression; return { CallExpression: function (node) { (0, function_1.pipe)(node, isPipeOrFlowExpression, fp_ts_1.boolean.fold(function_1.constVoid, function () { return (0, function_1.pipe)((0, utils_2.getAdjacentCombinators)(node, { name: /map|mapWithIndex/, types: [utils_1.AST_NODE_TYPES.CallExpression], }, { name: "flatten", types: [ utils_1.AST_NODE_TYPES.Identifier, utils_1.AST_NODE_TYPES.MemberExpression, ], }, true), fp_ts_1.option.bindTo("combinators"), fp_ts_1.option.bind("mapCalleeIdentifier", function (_a) { var mapNode = _a.combinators[0]; return (0, utils_2.calleeIdentifier)(mapNode); }), fp_ts_1.option.map(function (_a) { var _b = _a.combinators, mapNode = _b[0], flattenNode = _b[1], mapCalleeIdentifier = _a.mapCalleeIdentifier; var chainIndentifier = mapCalleeIdentifier.name === "mapWithIndex" ? "chainWithIndex" : "chain"; context.report({ loc: { start: mapNode.loc.start, end: flattenNode.loc.end, }, messageId: "mapFlattenIsChain", suggest: [ { messageId: "replaceMapFlattenWithChain", fix: function (fixer) { return [ fixer.remove(flattenNode), fixer.removeRange([ mapNode.range[1], flattenNode.range[0], ]), fixer.replaceText(mapCalleeIdentifier, chainIndentifier), ]; }, }, ], }); })); })); }, }; }, });