import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _createClass from "@babel/runtime/helpers/createClass"; import _inherits from "@babel/runtime/helpers/inherits"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import { Type } from '../constants'; import { YAMLReferenceError } from '../errors'; import _toJSON from '../toJSON'; import Collection from './Collection'; import Node from './Node'; import Pair from './Pair'; var getAliasCount = function getAliasCount(node, anchors) { if (node instanceof Alias) { var anchor = anchors.find(function (a) { return a.node === node.source; }); return anchor.count * anchor.aliasCount; } else if (node instanceof Collection) { var count = 0; var _iteratorNormalCompletion = true; var _didIteratorError = false; var _iteratorError = undefined; try { for (var _iterator = node.items[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var item = _step.value; var c = getAliasCount(item, anchors); if (c > count) count = c; } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return != null) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } return count; } else if (node instanceof Pair) { var kc = getAliasCount(node.key, anchors); var vc = getAliasCount(node.value, anchors); return Math.max(kc, vc); } return 1; }; var Alias = /*#__PURE__*/function (_Node) { _inherits(Alias, _Node); _createClass(Alias, null, [{ key: "stringify", value: function stringify(_ref, _ref2) { var range = _ref.range, source = _ref.source; var anchors = _ref2.anchors, doc = _ref2.doc, implicitKey = _ref2.implicitKey, inStringifyKey = _ref2.inStringifyKey; var anchor = Object.keys(anchors).find(function (a) { return anchors[a] === source; }); if (!anchor && inStringifyKey) anchor = doc.anchors.getName(source) || doc.anchors.newName(); if (anchor) return "*".concat(anchor).concat(implicitKey ? ' ' : ''); var msg = doc.anchors.getName(source) ? 'Alias node must be after source node' : 'Source node not found for alias node'; throw new Error("".concat(msg, " [").concat(range, "]")); } }]); function Alias(source) { var _this; _classCallCheck(this, Alias); _this = _possibleConstructorReturn(this, _getPrototypeOf(Alias).call(this)); _this.source = source; _this.type = Type.ALIAS; return _this; } _createClass(Alias, [{ key: "toJSON", value: function toJSON(arg, ctx) { var _this2 = this; if (!ctx) return _toJSON(this.source, arg, ctx); var anchors = ctx.anchors, maxAliasCount = ctx.maxAliasCount; var anchor = anchors.find(function (a) { return a.node === _this2.source; }); /* istanbul ignore if */ if (!anchor || anchor.res === undefined) { var msg = 'This should not happen: Alias anchor was not resolved?'; if (this.cstNode) throw new YAMLReferenceError(this.cstNode, msg);else throw new ReferenceError(msg); } if (maxAliasCount >= 0) { anchor.count += 1; if (anchor.aliasCount === 0) anchor.aliasCount = getAliasCount(this.source, anchors); if (anchor.count * anchor.aliasCount > maxAliasCount) { var _msg = 'Excessive alias count indicates a resource exhaustion attack'; if (this.cstNode) throw new YAMLReferenceError(this.cstNode, _msg);else throw new ReferenceError(_msg); } } return anchor.res; } // Only called when stringifying an alias mapping key while constructing // Object output. }, { key: "toString", value: function toString(ctx) { return Alias.stringify(this, ctx); } }, { key: "tag", set: function set(t) { throw new Error('Alias nodes cannot have tags'); } }]); return Alias; }(Node); _defineProperty(Alias, "default", true); export { Alias as default };