Comparing version 1.0.0-rc.4 to 1.0.0-rc.5
@@ -10,3 +10,3 @@ "use strict"; | ||
var _listTagNames = _interopRequireDefault(require("./listTagNames")); | ||
var _Anchors = _interopRequireDefault(require("./Anchors")); | ||
@@ -17,6 +17,12 @@ var _Node = require("./cst/Node"); | ||
var _listTagNames = _interopRequireDefault(require("./listTagNames")); | ||
var _schema = _interopRequireWildcard(require("./schema")); | ||
var _Collection = _interopRequireWildcard(require("./schema/Collection")); | ||
var _Alias = _interopRequireDefault(require("./schema/Alias")); | ||
var _Collection = _interopRequireDefault(require("./schema/Collection")); | ||
var _toJSON2 = _interopRequireDefault(require("./toJSON")); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } | ||
@@ -50,3 +56,3 @@ | ||
this.anchors = {}; | ||
this.anchors = new _Anchors.default(); | ||
this.commentBefore = null; | ||
@@ -100,2 +106,3 @@ this.comment = null; | ||
}); | ||
this.anchors._cstAliases = []; | ||
this.commentBefore = directiveComments.join('\n') || null; | ||
@@ -154,2 +161,3 @@ var comments = { | ||
this.comment = comments.after.join('\n') || null; | ||
this.anchors.resolveNodes(); | ||
return this; | ||
@@ -288,3 +296,15 @@ } | ||
}); | ||
if (hasAnchor) anchors[node.anchor] = node; | ||
if (hasAnchor) { | ||
var name = node.anchor; | ||
var prev = anchors.getNode(name); // At this point, aliases for any preceding node with the same anchor | ||
// name have already been resolved, so it may safely be renamed. | ||
if (prev) anchors.map[anchors.newName(name)] = prev; // During parsing, we need to store the CST node in anchors.map as | ||
// anchors need to be available during resolution to allow for | ||
// circular references. | ||
anchors.map[name] = node; | ||
} | ||
var res; | ||
@@ -294,10 +314,18 @@ | ||
if (hasAnchor || hasTag) errors.push(new _errors.YAMLSemanticError(node, 'An alias node must not specify any properties')); | ||
var src = anchors[node.rawValue]; | ||
var _name = node.rawValue; | ||
var src = anchors.getNode(_name); | ||
if (!src) { | ||
errors.push(new _errors.YAMLReferenceError(node, "Aliased anchor not found: ".concat(node.rawValue))); | ||
errors.push(new _errors.YAMLReferenceError(node, "Aliased anchor not found: ".concat(_name))); | ||
return null; | ||
} // Lazy resolution for circular references | ||
res = new _Alias.default(src); | ||
anchors._cstAliases.push(res); | ||
if (!src.resolved) { | ||
this.warnings.push(new _errors.YAMLWarning(node, 'Alias node contains a circular reference, which cannot be resolved as JSON')); | ||
} | ||
res = src.resolved; | ||
} else { | ||
@@ -372,3 +400,7 @@ var tagName = this.resolveTagName(node); | ||
value: function toJSON() { | ||
return (0, _Collection.toJSON)(this.contents); | ||
var cr = this.warnings.find(function (w) { | ||
return /circular reference/.test(w.message); | ||
}); | ||
if (cr) throw new _errors.YAMLSemanticError(cr.source, cr.message); | ||
return (0, _toJSON2.default)(this.contents); | ||
} | ||
@@ -402,2 +434,3 @@ }, { | ||
var ctx = { | ||
anchors: {}, | ||
doc: this, | ||
@@ -404,0 +437,0 @@ indent: '' |
@@ -6,3 +6,3 @@ "use strict"; | ||
}); | ||
exports.default = exports.toJSON = void 0; | ||
exports.default = void 0; | ||
@@ -15,2 +15,4 @@ var _addComment = _interopRequireDefault(require("../addComment")); | ||
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
@@ -34,10 +36,2 @@ | ||
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
var toJSON = function toJSON(value) { | ||
return Array.isArray(value) ? value.map(toJSON) : value && _typeof(value) === 'object' && 'toJSON' in value ? value.toJSON() : value; | ||
}; | ||
exports.toJSON = toJSON; | ||
var Collection = | ||
@@ -68,18 +62,16 @@ /*#__PURE__*/ | ||
key: "toString", | ||
value: function toString(_ref, onComment) { | ||
value: function toString(ctx, _ref, onComment) { | ||
var _this2 = this; | ||
var blockItem = _ref.blockItem, | ||
doc = _ref.doc, | ||
flowChars = _ref.flowChars, | ||
indent = _ref.indent, | ||
inFlow = _ref.inFlow, | ||
itemIndent = _ref.itemIndent; | ||
if (!doc) return JSON.stringify(this); | ||
var ctx = { | ||
doc: doc, | ||
var _ctx = ctx, | ||
doc = _ctx.doc, | ||
indent = _ctx.indent, | ||
inFlow = _ctx.inFlow; | ||
ctx = Object.assign({}, ctx, { | ||
indent: itemIndent, | ||
inFlow: inFlow, | ||
type: null | ||
}; | ||
}); | ||
var hasItemWithComment = false; | ||
@@ -107,3 +99,3 @@ var hasItemWithNewLine = false; | ||
if (inFlow && i < _this2.items.length - 1) str += ','; | ||
str = (0, _addComment.default)(str, ctx.indent, comment); | ||
str = (0, _addComment.default)(str, itemIndent, comment); | ||
nodes.push({ | ||
@@ -110,0 +102,0 @@ type: 'item', |
@@ -14,2 +14,4 @@ "use strict"; | ||
var _Alias = _interopRequireDefault(require("./Alias")); | ||
var _Collection = _interopRequireDefault(require("./Collection")); | ||
@@ -189,2 +191,3 @@ | ||
value: function getStringifier(item) { | ||
if (item instanceof _Alias.default) return _Alias.default.stringify; | ||
var match; | ||
@@ -239,2 +242,27 @@ | ||
return match.stringify || Schema.defaultStringifier; | ||
} // needs to be called before stringifier to allow for circular anchor refs | ||
}, { | ||
key: "stringifyProps", | ||
value: function stringifyProps(node, _ref7) { | ||
var anchors = _ref7.anchors, | ||
doc = _ref7.doc; | ||
var props = []; | ||
var anchor = doc.anchors.getName(node); | ||
if (anchor) { | ||
anchors[anchor] = node; | ||
props.push("&".concat(anchor)); | ||
} | ||
var tag = node.tag; // FIXME: should read prefix-skip from schema | ||
if (tag && tag.indexOf(defaultPrefix) !== 0) { | ||
var p = doc.tagPrefixes.find(function (p) { | ||
return tag.indexOf(p.prefix) === 0; | ||
}); | ||
props.push(p ? p.handle + tag.substr(p.prefix.length) : tag[0] === '!' ? tag : "!<".concat(tag, ">")); | ||
} | ||
return props.join(' '); | ||
} | ||
@@ -248,20 +276,5 @@ }, { | ||
var stringify = this.getStringifier(item); | ||
var props = this.stringifyProps(item, ctx); | ||
var str = stringify(item, ctx, onComment); | ||
var _item = item, | ||
tag = _item.tag; | ||
if (tag && tag.indexOf(defaultPrefix) !== 0) { | ||
var p = ctx.doc.tagPrefixes.find(function (p) { | ||
return tag.indexOf(p.prefix) === 0; | ||
}); | ||
var tagProp = p ? p.handle + tag.substr(p.prefix.length) : tag[0] === '!' ? tag : "!<".concat(tag, ">"); | ||
if (item instanceof _Collection.default && !ctx.inFlow && item.items.length > 0) { | ||
return "".concat(tagProp, "\n").concat(ctx.indent).concat(str); | ||
} else { | ||
return "".concat(tagProp, " ").concat(str); | ||
} | ||
} | ||
return str; | ||
return props ? item instanceof _Collection.default && !ctx.inFlow && item.items.length > 0 ? "".concat(props, "\n").concat(ctx.indent).concat(str) : "".concat(props, " ").concat(str) : str; | ||
} | ||
@@ -268,0 +281,0 @@ }]); |
@@ -8,4 +8,8 @@ "use strict"; | ||
var _Collection2 = _interopRequireWildcard(require("./Collection")); | ||
var _toJSON2 = _interopRequireDefault(require("../toJSON")); | ||
var _Collection2 = _interopRequireDefault(require("./Collection")); | ||
var _Merge = _interopRequireDefault(require("./Merge")); | ||
var _Pair = _interopRequireDefault(require("./Pair")); | ||
@@ -15,4 +19,2 @@ | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } | ||
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
@@ -52,6 +54,37 @@ | ||
value: function toJSON() { | ||
return this.items.reduce(function (map, _ref) { | ||
var stringKey = _ref.stringKey, | ||
value = _ref.value; | ||
map[stringKey] = (0, _Collection2.toJSON)(value); | ||
return this.items.reduce(function (map, item) { | ||
if (item instanceof _Merge.default) { | ||
(function () { | ||
// If the value associated with a merge key is a single mapping node, | ||
// each of its key/value pairs is inserted into the current mapping, | ||
// unless the key already exists in it. If the value associated with the | ||
// merge key is a sequence, then this sequence is expected to contain | ||
// mapping nodes and each of these nodes is merged in turn according to | ||
// its order in the sequence. Keys in mapping nodes earlier in the | ||
// sequence override keys specified in later mapping nodes. | ||
// -- http://yaml.org/type/merge.html | ||
var keys = Object.keys(map); | ||
var items = item.value.items; | ||
for (var i = items.length - 1; i >= 0; --i) { | ||
var source = items[i].source; | ||
if (source instanceof YAMLMap) { | ||
(function () { | ||
var obj = source.toJSON(); | ||
Object.keys(obj).forEach(function (key) { | ||
if (!keys.includes(key)) map[key] = obj[key]; | ||
}); | ||
})(); | ||
} else { | ||
throw new Error('Merge sources must be maps'); | ||
} | ||
} | ||
})(); | ||
} else { | ||
var stringKey = item.stringKey, | ||
value = item.value; | ||
map[stringKey] = (0, _toJSON2.default)(value); | ||
} | ||
return map; | ||
@@ -62,20 +95,14 @@ }, {}); | ||
key: "toString", | ||
value: function toString() { | ||
var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
doc = _ref2.doc, | ||
_ref2$indent = _ref2.indent, | ||
indent = _ref2$indent === void 0 ? '' : _ref2$indent, | ||
_ref2$inFlow = _ref2.inFlow, | ||
inFlow = _ref2$inFlow === void 0 ? false : _ref2$inFlow; | ||
var onComment = arguments.length > 1 ? arguments[1] : undefined; | ||
value: function toString(ctx, onComment) { | ||
if (!ctx) return JSON.stringify(this); | ||
this.items.forEach(function (item) { | ||
if (!(item instanceof _Pair.default)) throw new Error("Map items must all be pairs; found ".concat(JSON.stringify(item), " instead")); | ||
}); | ||
return _get(_getPrototypeOf(YAMLMap.prototype), "toString", this).call(this, { | ||
blockItem: function blockItem(_ref3) { | ||
var str = _ref3.str; | ||
var itemIndent = ctx.indent || ''; | ||
if (ctx.inFlow) itemIndent += ' '; | ||
return _get(_getPrototypeOf(YAMLMap.prototype), "toString", this).call(this, ctx, { | ||
blockItem: function blockItem(_ref) { | ||
var str = _ref.str; | ||
return str; | ||
}, | ||
doc: doc, | ||
flowChars: { | ||
@@ -85,5 +112,3 @@ start: '{', | ||
}, | ||
indent: indent, | ||
inFlow: inFlow, | ||
itemIndent: indent + (inFlow ? ' ' : '') | ||
itemIndent: itemIndent | ||
}, onComment); | ||
@@ -90,0 +115,0 @@ } |
@@ -10,4 +10,6 @@ "use strict"; | ||
var _Collection = _interopRequireWildcard(require("./Collection")); | ||
var _toJSON2 = _interopRequireDefault(require("../toJSON")); | ||
var _Collection = _interopRequireDefault(require("./Collection")); | ||
var _Node2 = _interopRequireDefault(require("./Node")); | ||
@@ -17,4 +19,2 @@ | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -60,3 +60,3 @@ | ||
var pair = {}; | ||
pair[this.stringKey] = (0, _Collection.toJSON)(this.value); | ||
pair[this.stringKey] = (0, _toJSON2.default)(this.value); | ||
return pair; | ||
@@ -116,3 +116,3 @@ } | ||
get: function get() { | ||
var key = (0, _Collection.toJSON)(this.key); | ||
var key = (0, _toJSON2.default)(this.key); | ||
if (key === null) return ''; | ||
@@ -119,0 +119,0 @@ if (_typeof(key) === 'object') try { |
@@ -14,2 +14,4 @@ "use strict"; | ||
var _Merge = _interopRequireWildcard(require("./Merge")); | ||
var _Pair = _interopRequireDefault(require("./Pair")); | ||
@@ -19,14 +21,8 @@ | ||
var _Seq = _interopRequireDefault(require("./Seq")); | ||
var _Alias = _interopRequireDefault(require("./Alias")); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); } | ||
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); } | ||
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); } | ||
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } } | ||
function parseMap(doc, cst) { | ||
@@ -53,20 +49,18 @@ var _ref = cst.type === _Node.Type.FLOW_MAP ? resolveFlowMapItems(doc, cst) : resolveBlockMapItems(doc, cst), | ||
if (doc.schema.merge && iKey.value === '<<') { | ||
var src = items[i].value; | ||
var srcItems = src instanceof _Seq.default ? src.items.reduce(function (acc, _ref2) { | ||
var items = _ref2.items; | ||
return acc.concat(items); | ||
}, []) : src.items; | ||
var toAdd = srcItems.reduce(function (toAdd, pair) { | ||
var exists = items.some(function (_ref3) { | ||
var key = _ref3.key; | ||
return key.value === pair.key.value; | ||
}) || toAdd.some(function (_ref4) { | ||
var key = _ref4.key; | ||
return key.value === pair.key.value; | ||
}); | ||
return exists ? toAdd : toAdd.concat(pair); | ||
}, []); | ||
Array.prototype.splice.apply(items, [i, 1].concat(_toConsumableArray(toAdd))); | ||
i += toAdd.length - 1; | ||
if (doc.schema.merge && iKey.value === _Merge.MERGE_KEY) { | ||
items[i] = new _Merge.default(items[i]); | ||
var sources = items[i].value.items; | ||
var error = null; | ||
sources.some(function (node) { | ||
if (node instanceof _Alias.default) { | ||
// During parsing, alias sources are CST nodes; to account for | ||
// circular references their resolved values can't be used here. | ||
var type = node.source.type; | ||
if (type === _Node.Type.MAP || type === _Node.Type.FLOW_MAP) return false; | ||
return error = 'Merge nodes aliases can only point to maps'; | ||
} | ||
return error = 'Merge nodes can only have Alias nodes as values'; | ||
}); | ||
if (error) doc.errors.push(new _errors.YAMLSemanticError(cst, error)); | ||
} | ||
@@ -73,0 +67,0 @@ } |
@@ -8,3 +8,3 @@ "use strict"; | ||
var _Collection = require("./Collection"); | ||
var _toJSON2 = _interopRequireDefault(require("../toJSON")); | ||
@@ -49,3 +49,3 @@ var _Node2 = _interopRequireDefault(require("./Node")); | ||
value: function toJSON() { | ||
return (0, _Collection.toJSON)(this.value); | ||
return (0, _toJSON2.default)(this.value); | ||
} | ||
@@ -52,0 +52,0 @@ }, { |
@@ -8,6 +8,8 @@ "use strict"; | ||
var _Collection2 = _interopRequireWildcard(require("./Collection")); | ||
var _toJSON2 = _interopRequireDefault(require("../toJSON")); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } | ||
var _Collection2 = _interopRequireDefault(require("./Collection")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
@@ -47,22 +49,15 @@ | ||
value: function toJSON() { | ||
return this.items.map(_Collection2.toJSON); | ||
return this.items.map(_toJSON2.default); | ||
} | ||
}, { | ||
key: "toString", | ||
value: function toString() { | ||
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
doc = _ref.doc, | ||
_ref$indent = _ref.indent, | ||
indent = _ref$indent === void 0 ? '' : _ref$indent, | ||
_ref$inFlow = _ref.inFlow, | ||
inFlow = _ref$inFlow === void 0 ? false : _ref$inFlow; | ||
var onComment = arguments.length > 1 ? arguments[1] : undefined; | ||
return _get(_getPrototypeOf(YAMLSeq.prototype), "toString", this).call(this, { | ||
blockItem: function blockItem(_ref2) { | ||
var type = _ref2.type, | ||
str = _ref2.str; | ||
value: function toString(ctx, onComment) { | ||
if (!ctx) return JSON.stringify(this); | ||
var itemIndent = (ctx.indent || '') + (ctx.inFlow ? ' ' : ' '); | ||
return _get(_getPrototypeOf(YAMLSeq.prototype), "toString", this).call(this, ctx, { | ||
blockItem: function blockItem(_ref) { | ||
var type = _ref.type, | ||
str = _ref.str; | ||
return type === 'comment' ? str : "- ".concat(str); | ||
}, | ||
doc: doc, | ||
flowChars: { | ||
@@ -72,5 +67,3 @@ start: '[', | ||
}, | ||
indent: indent, | ||
inFlow: inFlow, | ||
itemIndent: indent + (inFlow ? ' ' : ' ') | ||
itemIndent: itemIndent | ||
}, onComment); | ||
@@ -77,0 +70,0 @@ } |
{ | ||
"name": "yaml", | ||
"version": "1.0.0-rc.4", | ||
"version": "1.0.0-rc.5", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "license": "ISC", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
212728
53
4723