canner-compiler
Version:
Compiler for Canner CMS schema
131 lines (111 loc) • 3.63 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _tree = _interopRequireDefault(require("./tree"));
var _visitor = _interopRequireDefault(require("./visitor"));
var Traverser =
/*#__PURE__*/
function () {
function Traverser(tree, visitor) {
(0, _classCallCheck2.default)(this, Traverser);
(0, _defineProperty2.default)(this, "tree", void 0);
(0, _defineProperty2.default)(this, "visitor", void 0);
this.tree = new _tree.default(tree);
this.visitor = new _visitor.default(visitor);
}
(0, _createClass2.default)(Traverser, [{
key: "addVisitor",
value: function addVisitor(visitor) {
this.visitor.merge(visitor);
}
}, {
key: "traverse",
value: function traverse() {
var _this = this;
Object.keys(this.tree.getTree()).forEach(function (key) {
_this.traverseNode(_this.tree.getTree()[key], null, key);
});
return this.tree.getTree();
}
}, {
key: "traverseNode",
value: function traverseNode(node, parent, route) {
var path = {
node: node,
parent: parent,
tree: this.tree,
route: route
};
this.nodeCallEnter(path);
if (node.children && node.children.length) {
this.traverseChildren(node, 0, route);
}
this.nodeCallExit(path);
}
}, {
key: "traverseChildren",
value: function traverseChildren(parent, childIndex, route) {
var _this2 = this;
if (childIndex >= parent.children.length) {
return;
}
parent.children.forEach(function (child, i) {
if (!parent.children[i]) {
return;
}
_this2.traverseNode(parent.children[i], parent, "".concat(route, ".").concat(i));
}); // this.traverseNode(parent.children[childIndex], parent, `${route}.${childIndex}`);
// this.traverseChildren(parent, this.childIndex + 1, route);
}
}, {
key: "nodeCallEnter",
value: function nodeCallEnter(path) {
var visitor = this.visitor.getVisitor();
var nodeTypes = path.node.nodeType.split('.');
var nodeType = '';
nodeTypes.forEach(function (type) {
if (nodeType === '') {
nodeType = type;
} else {
nodeType += ".".concat(type);
}
var nodeVisitor = visitor[nodeType];
if (nodeVisitor && 'enter' in nodeVisitor) {
nodeVisitor.enter.forEach(function (method) {
return method(path);
});
}
});
}
}, {
key: "nodeCallExit",
value: function nodeCallExit(path) {
var visitor = this.visitor.getVisitor();
var nodeTypes = path.node.nodeType.split('.');
var nodeType = '';
nodeTypes.map(function (type) {
if (nodeType === '') {
nodeType = type;
} else {
nodeType += ".".concat(type);
}
return nodeType;
}).reverse().forEach(function (type) {
var nodeVisitor = visitor[type];
if (nodeVisitor && 'exit' in nodeVisitor) {
nodeVisitor.exit.forEach(function (method) {
return method(path);
});
}
});
}
}]);
return Traverser;
}();
exports.default = Traverser;