import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function _createSuper(Derived) { return function () { var Super = _getPrototypeOf(Derived), result; if (_isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } function _createForOfIteratorHelper(o) { if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (o = _unsupportedIterableToArray(o))) { var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var it, normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } import { Type } from '../constants'; import { YAMLReferenceError } from '../errors'; import { toJSON as _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.get(node.source); return anchor.count * anchor.aliasCount; } else if (node instanceof Collection) { var count = 0; var _iterator = _createForOfIteratorHelper(node.items), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var item = _step.value; var c = getAliasCount(item, anchors); if (c > count) count = c; } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } 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; }; export var Alias = /*#__PURE__*/function (_Node) { _inherits(Alias, _Node); var _super = _createSuper(Alias); _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 = _super.call(this); _this.source = source; _this.type = Type.ALIAS; return _this; } _createClass(Alias, [{ key: "toJSON", value: function toJSON(arg, ctx) { if (!ctx) return _toJSON(this.source, arg, ctx); var anchors = ctx.anchors, maxAliasCount = ctx.maxAliasCount; var anchor = anchors.get(this.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);