UNPKG

@qualifyze/airtable-formulator

Version:
62 lines 3.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.reduceFunctionCalls = exports.isFunctionCallNode = void 0; const primitives_1 = require("./primitives"); const argument_list_1 = require("./argument-list"); const function_reference_1 = require("./function-reference"); const node_reducer_1 = require("./node-reducer"); const functionCallType = "functionCall"; function isFunctionCallType(type) { return type === functionCallType; } function isFunctionCallMember(node) { return ((0, primitives_1.isNode)(node) && ((0, primitives_1.isSpace)(node) || (0, function_reference_1.isFunctionReference)(node) || (0, argument_list_1.isArgumentListNode)(node))); } function isFunctionCallNode(node) { const { reference, argumentList } = node; return ((0, primitives_1.isGroupNode)(node) && node.members.every(isFunctionCallMember) && node.type === "functionCall" && (0, primitives_1.isNode)(reference) && (0, function_reference_1.isFunctionReference)(reference) && (0, primitives_1.isNode)(argumentList) && (0, argument_list_1.isArgumentListNode)(argumentList)); } exports.isFunctionCallNode = isFunctionCallNode; function createFunctionCall(nodes) { const [reference, argumentList] = (0, primitives_1.filterMeaningfulNodes)(nodes); const functionCall = (0, primitives_1.createGroup)("functionCall", nodes); if (!(0, primitives_1.isGroupNode)(functionCall, isFunctionCallType, isFunctionCallMember)) { throw new Error((0, primitives_1.createNodeErrorMessage)(functionCall, `expected function call to have a function reference and argument list, but got ${nodes.map((node) => node.type)}`)); } if (!(0, function_reference_1.isFunctionReference)(reference)) { throw new Error((0, primitives_1.createNodeErrorMessage)(functionCall, `Function call must have a function reference, but encountered ${reference.type} instead`)); } if (!(0, argument_list_1.isArgumentListNode)(argumentList)) { throw new Error((0, primitives_1.createNodeErrorMessage)(functionCall, `Function call must have an argument list, but encountered ${argumentList.type} instead`)); } return { ...functionCall, reference, argumentList, }; } exports.reduceFunctionCalls = (0, node_reducer_1.eagerlyRepeat)(([...nodes]) => { const meaningfulNodes = (0, primitives_1.filterMeaningfulNodes)(nodes); const argumentList = meaningfulNodes .filter(argument_list_1.isArgumentListNode) .find((node) => { const previousNode = meaningfulNodes[meaningfulNodes.indexOf(node) - 1]; return (0, function_reference_1.isFunctionReference)(previousNode); }); if (argumentList) { const reference = meaningfulNodes[meaningfulNodes.indexOf(argumentList) - 1]; const referenceIndex = nodes.indexOf(reference); const argumentListIndex = nodes.indexOf(argumentList); const members = nodes.slice(referenceIndex, argumentListIndex + 1); nodes.splice(referenceIndex, members.length, createFunctionCall(members)); } return nodes; }); //# sourceMappingURL=function-call.js.map