UNPKG

@react-awesome-query-builder/core

Version:
259 lines (253 loc) 9.55 kB
import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import _typeof from "@babel/runtime/helpers/typeof"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import * as Utils from "../../utils"; var logger = Utils.OtherUtils.logger; var _postprocessCompiled = function postprocessCompiled(expr, meta) { var parentExpr = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; var type = expr.getType(); var children = expr.getChildren().map(function (child) { return _postprocessCompiled(child, meta, expr); }); // flatize OR/AND if (type == "op-or" || type == "op-and") { children = children.reduce(function (acc, child) { var isBetweenNormal = child.type == "op-and" && child.children.length == 2 && child.children[0].type == "op-ge" && child.children[1].type == "op-le"; var isBetweenRev = child.type == "op-or" && child.children.length == 2 && child.children[0].type == "op-lt" && child.children[1].type == "op-gt"; var isBetween = isBetweenNormal || isBetweenRev; var canFlatize = child.type == type && !child.not && !isBetween; var flat = canFlatize ? child.children : [child]; return [].concat(_toConsumableArray(acc), _toConsumableArray(flat)); }, []); } // unwrap NOT if (type == "op-not") { if (children.length != 1) { meta.errors.push("Operator NOT should have 1 child, but got ".concat(children.length, "}")); } return _objectSpread(_objectSpread({}, children[0]), {}, { not: !(children[0].not || false) }); } if (type == "compound") { // remove `.?[true]` children = children.filter(function (child) { var isListFix = child.type == "selection" && child.children.length == 1 && child.children[0].type == "boolean" && child.children[0].val == true; return !isListFix; }); // aggregation // eg. `results.?[product == 'abc'].length` var selection = children.find(function (child) { return child.type == "selection"; }); if (selection && selection.children.length != 1) { meta.errors.push("Selection should have 1 child, but got ".concat(selection.children.length)); } var filter = selection ? selection.children[0] : null; var lastChild = children[children.length - 1]; var isSize = lastChild.type == "method" && lastChild.val.methodName == "size" || lastChild.type == "!func" && lastChild.methodName == "size"; var isLength = lastChild.type == "property" && lastChild.val == "length"; var sourceParts = children.filter(function (child) { return child !== selection && child !== lastChild; }); var source = { type: "compound", children: sourceParts }; var isAggr = (isSize || isLength) && convertPath(sourceParts) != null; if (isAggr) { return { type: "!aggr", filter: filter, source: source }; } // remove `#this`, `#root` children = children.filter(function (child) { var isThis = child.type == "variable" && child.val == "this"; var isRoot = child.type == "variable" && child.val == "root"; return !(isThis || isRoot); }); // indexer children = children.map(function (child) { if (child.type == "indexer" && child.children.length == 1) { return { type: "indexer", val: child.children[0].val, itype: child.children[0].type }; } else { return child; } }); // method // if (lastChild.type == "method") { // // seems like obsolete code! // debugger // const obj = children.filter(child => // child !== lastChild // ); // return { // type: "!func", // obj, // methodName: lastChild.val.methodName, // args: lastChild.val.args // }; // } // !func if (lastChild.type == "!func") { var ret = {}; var curr = ret; do { var _lastChild, _lastChild2; Object.assign(curr, lastChild); children = children.filter(function (child) { return child !== lastChild; }); lastChild = children[children.length - 1]; if (((_lastChild = lastChild) === null || _lastChild === void 0 ? void 0 : _lastChild.type) == "!func") { curr.obj = {}; curr = curr.obj; } else { if (children.length > 1) { curr.obj = { type: "compound", children: children }; } else { curr.obj = lastChild; } } } while (((_lastChild2 = lastChild) === null || _lastChild2 === void 0 ? void 0 : _lastChild2.type) == "!func"); return ret; } } // getRaw || getValue var val; try { if (expr.getRaw) { // use my fork val = expr.getRaw(); } else if (expr.getValue.length == 0) { // getValue not requires context arg -> can use val = expr.getValue(); } } catch (e) { logger.error("[spel2js] Error in getValue()", e); } // ternary if (type == "ternary") { val = flatizeTernary(children); } // convert method/function args if (_typeof(val) === "object" && val !== null) { if (val.methodName || val.functionName) { val.args = val.args.map(function (child) { return _postprocessCompiled(child, meta, expr); }); } } // convert list if (type == "list") { val = val.map(function (item) { return _postprocessCompiled(item, meta, expr); }); // fix whole expression wrapped in `{}` if (!parentExpr && val.length == 1) { return val[0]; } } // convert constructor if (type == "constructorref") { var qid = children.find(function (child) { return child.type == "qualifiedidentifier"; }); var cls = qid === null || qid === void 0 ? void 0 : qid.val; if (!cls) { meta.errors.push("Can't find qualifiedidentifier in constructorref children: ".concat(JSON.stringify(children))); return undefined; } var args = children.filter(function (child) { return child.type != "qualifiedidentifier"; }); return { type: "!new", cls: cls, args: args }; } // convert type if (type == "typeref") { var _qid = children.find(function (child) { return child.type == "qualifiedidentifier"; }); var _cls = _qid === null || _qid === void 0 ? void 0 : _qid.val; if (!_cls) { meta.errors.push("Can't find qualifiedidentifier in typeref children: ".concat(JSON.stringify(children))); return undefined; } var _args = children.filter(function (child) { return child.type != "qualifiedidentifier"; }); return { type: "!type", cls: _cls }; } // convert function/method if (type == "function" || type == "method") { // `foo()` is method, `#foo()` is function // let's use common property `methodName` and just add `isVar` for function var _val = val, functionName = _val.functionName, methodName = _val.methodName, _args2 = _val.args; return { type: "!func", methodName: functionName || methodName, isVar: type == "function", args: _args2 }; } return { type: type, children: children, val: val }; }; export { _postprocessCompiled as postprocessCompiled }; var flatizeTernary = function flatizeTernary(children) { var flat = []; function _processTernaryChildren(tern) { var _tern = _slicedToArray(tern, 3), cond = _tern[0], if_val = _tern[1], else_val = _tern[2]; flat.push([cond, if_val]); if ((else_val === null || else_val === void 0 ? void 0 : else_val.type) == "ternary") { _processTernaryChildren(else_val.children); } else { flat.push([undefined, else_val]); } } _processTernaryChildren(children); return flat; }; export var convertPath = function convertPath(parts) { var meta = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var expectingField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; var isError = false; var res = parts.map(function (c) { if (c.type == "variable" || c.type == "property" || c.type == "indexer" && c.itype == "string") { return c.val; } else { var _meta$errors, _meta$errors$push; isError = true; expectingField && (meta === null || meta === void 0 || (_meta$errors = meta.errors) === null || _meta$errors === void 0 || (_meta$errors$push = _meta$errors.push) === null || _meta$errors$push === void 0 ? void 0 : _meta$errors$push.call(_meta$errors, "Unexpected item in field path compound: ".concat(JSON.stringify(c)))); } }); return !isError ? res : undefined; };