UNPKG

@atomist/rug

Version:

TypeScript model for Atomist Rugs, see http://docs.atomist.com/

79 lines (78 loc) 3.4 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var TextTreeNodeOps_1 = require("../ast/TextTreeNodeOps"); var TransformingPathExpressionEngine_1 = require("../tree/TransformingPathExpressionEngine"); /** * Can be used directly or via a subclass that provides a strategy * for resolving an ops class for a given node. * Tries to find an "Ops" class for the given node, and mix in * its methods with the node type methods. If an ops class can't * be found, mix in TextTreeNodeOps to return RichTextTreeNode. */ var DecoratingPathExpressionEngine = (function (_super) { __extends(DecoratingPathExpressionEngine, _super); function DecoratingPathExpressionEngine(delegate) { var _this = _super.call(this, delegate, function (n) { // Also need to parameterize module var ops = _this.decoratorFor(n); if (ops == null) { ops = new TextTreeNodeOps_1.TextTreeNodeOps(n, delegate); } var combined = _this.unify(n, ops); // console.log(`ops=${ops}, combined=${combined}`) return combined; }) || this; return _this; } DecoratingPathExpressionEngine.prototype.save = function (n, expr) { var hits = []; this.with(n, expr, function (node) { hits.push(node); }); return hits; }; /** * Template method subclasses can use to find the decorator for this node. * Implementations will typically use the decoratorClassName method. * Implementations don't need to catch the error if a class cannot * be instantiated: It will be caught in this class. They can also * return null if unresolved. Default implementation returns null. * @param n TreeNode to decorate */ DecoratingPathExpressionEngine.prototype.decoratorFor = function (n) { return null; }; /** * Convenience method returning the conventional decorator * class name for the node name. Simply adds "Ops" suffix. * @param n TreeNode we wish to decorate */ DecoratingPathExpressionEngine.prototype.decoratorClassName = function (n) { return n.nodeName().charAt(0).toUpperCase() + n.nodeName().substr(1) + "Ops"; }; /** * Add all functions from right to left. * Also copies state, which may be needed for functions to work. */ DecoratingPathExpressionEngine.prototype.unify = function (base, enricher) { var monkeyableBase = base; // tslint:disable-next-line:forin for (var id in enricher) { var fun = enricher[id]; monkeyableBase[id] = fun; } return monkeyableBase; }; return DecoratingPathExpressionEngine; }(TransformingPathExpressionEngine_1.TransformingPathExpressionEngine)); exports.DecoratingPathExpressionEngine = DecoratingPathExpressionEngine;