Skip to content

Commit

Permalink
1.0.0-beta.1
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Feb 13, 2018
1 parent ac6371f commit 23f382d
Show file tree
Hide file tree
Showing 25 changed files with 2,957 additions and 1 deletion.
101 changes: 101 additions & 0 deletions dist/Collection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.Comment = exports.Pair = void 0;

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"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

var Pair =
/*#__PURE__*/
function () {
function Pair(key) {
var value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;

_classCallCheck(this, Pair);

this.key = key;
this.value = value;
}

_createClass(Pair, [{
key: "asPlainObject",
get: function get() {
var pair = {};
pair[this.stringKey] = this.plainValue;
return pair;
}
}, {
key: "plainKey",
get: function get() {
var key = this.key;
return key && 'asPlainObject' in key ? key.asPlainObject : key;
}
}, {
key: "plainValue",
get: function get() {
var value = this.value;
return value && 'asPlainObject' in value ? value.asPlainObject : value;
}
}, {
key: "stringKey",
get: function get() {
var key = this.plainKey;
if (_typeof(key) === 'object') try {
return JSON.stringify(key);
} catch (e) {
/* should not happen, but let's ignore in any case */
}
return String(key);
}
}]);

return Pair;
}();

exports.Pair = Pair;

var Comment = function Comment(comment, before) {
_classCallCheck(this, Comment);

this.before = before;
this.comment = comment;
};

exports.Comment = Comment;

var Collection =
/*#__PURE__*/
function () {
function Collection() {
_classCallCheck(this, Collection);

this.comments = []; // TODO: include collection & item comments

this.items = [];
}

_createClass(Collection, [{
key: "addComment",
value: function addComment(comment) {
this.comments.push(new Comment(comment, this.items.length));
} // overridden in implementations

}, {
key: "asPlainObject",
get: function get() {
return null;
}
}]);

return Collection;
}();

exports.default = Collection;
225 changes: 225 additions & 0 deletions dist/Document.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;

var _rawYaml = require("raw-yaml");

var _errors = require("./errors");

var _Tags = require("./Tags");

var _Collection = require("./schema/Collection");

function _sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return _sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

var Document =
/*#__PURE__*/
function () {
_createClass(Document, [{
key: "parseTagDirective",
value: function parseTagDirective(directive) {
var _directive$parameters = _slicedToArray(directive.parameters, 2),
handle = _directive$parameters[0],
prefix = _directive$parameters[1];

if (handle && prefix) {
if (this.tagPrefixes[handle]) this.errors.push(new _errors.YAMLSyntaxError(directive, 'The TAG directive must only be given at most once per handle in the same document.'));
this.tagPrefixes[handle] = prefix;
} else {
this.errors.push(new _errors.YAMLSyntaxError(directive, 'Insufficient parameters given for TAG directive'));
}
}
}, {
key: "parseYamlDirective",
value: function parseYamlDirective(directive) {
var _directive$parameters2 = _slicedToArray(directive.parameters, 1),
version = _directive$parameters2[0];

if (this.version) this.errors.push(new _errors.YAMLSyntaxError(directive, 'The YAML directive must only be given at most once per document.'));
if (!version) this.errors.push(new _errors.YAMLSyntaxError(directive, 'Insufficient parameters given for YAML directive'));else if (version !== '1.2') this.errors.push(new _errors.YAMLWarning(directive, "Document will be parsed as YAML 1.2 rather than YAML ".concat(version)));
this.version = version;
}
}]);

function Document(tags) {
var _this = this;

var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
_ref$directives = _ref.directives,
directives = _ref$directives === void 0 ? [] : _ref$directives,
_ref$contents = _ref.contents,
contents = _ref$contents === void 0 ? [] : _ref$contents;

var _ref2 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
merge = _ref2.merge;

_classCallCheck(this, Document);

this.anchors = [];
this.directives = directives;
this.errors = [];
this.options = {
merge: merge !== false
};
this.rawContents = contents;
this.tagPrefixes = {};
this.tags = tags;
this.version = null;
directives.forEach(function (directive) {
var name = directive.name;

switch (name) {
case 'TAG':
_this.parseTagDirective(directive);

break;

case 'YAML':
_this.parseYamlDirective(directive);

break;

default:
if (name) _this.errors.push(new _errors.YAMLWarning(directive, "YAML 1.2 only supports TAG and YAML directives, and not ".concat(name)));
}
});
var contentNodes = contents.filter(function (node) {
return node.valueRange && !node.valueRange.isEmpty;
}).map(function (node) {
return _this.resolveNode(node);
});

switch (contentNodes.length) {
case 0:
this.contents = null;
break;

case 1:
this.contents = contentNodes[0];
break;

default:
this.errors.push(new _errors.YAMLSyntaxError(null, 'Document is not valid YAML (bad indentation?)'));
this.contents = contentNodes;
}
}

_createClass(Document, [{
key: "resolveTagName",
value: function resolveTagName(node) {
var tag = node.tag,
type = node.type;
var nonSpecific = false;

if (tag) {
var handle = tag.handle,
suffix = tag.suffix,
verbatim = tag.verbatim;

if (verbatim) {
if (verbatim !== '!' && verbatim !== '!!') return verbatim;
this.errors.push(new _errors.YAMLSyntaxError(node, "Verbatim tags aren't resolved, so ".concat(verbatim, " is invalid.")));
} else if (handle === '!' && !suffix) {
nonSpecific = true;
} else {
var prefix = this.tagPrefixes[handle] || _Tags.DefaultTagPrefixes[handle];

if (prefix) {
if (suffix) return prefix + suffix;
this.errors.push(new _errors.YAMLSyntaxError(node, "The ".concat(handle, " tag has no suffix.")));
} else {
this.errors.push(new _errors.YAMLSyntaxError(node, "The ".concat(handle, " tag handle is non-default and was not declared.")));
}
}
}

switch (type) {
case _rawYaml.Type.BLOCK_FOLDED:
case _rawYaml.Type.BLOCK_LITERAL:
case _rawYaml.Type.QUOTE_DOUBLE:
case _rawYaml.Type.QUOTE_SINGLE:
return _Tags.DefaultTags.STR;

case _rawYaml.Type.FLOW_MAP:
case _rawYaml.Type.MAP:
return _Tags.DefaultTags.MAP;

case _rawYaml.Type.FLOW_SEQ:
case _rawYaml.Type.SEQ:
return _Tags.DefaultTags.SEQ;

case _rawYaml.Type.PLAIN:
return nonSpecific ? _Tags.DefaultTags.STR : null;

default:
return null;
}
}
}, {
key: "resolveNode",
value: function resolveNode(node) {
if (!node) return null;
var anchors = this.anchors,
errors = this.errors,
tags = this.tags;
var anchor = node.anchor;
if (anchor) anchors[anchor] = node;

if (node.type === _rawYaml.Type.ALIAS) {
var src = anchors[node.rawValue];
if (src) return node.resolved = src.resolved;
errors.push(new _errors.YAMLReferenceError(node, "Aliased anchor not found: ".concat(node.rawValue)));
return null;
}

var tagName = this.resolveTagName(node);
if (tagName) return node.resolved = tags.resolve(this, node, tagName);
if (node.type === _rawYaml.Type.PLAIN) try {
return node.resolved = tags.resolveScalar(node.strValue || '');
} catch (error) {
if (error instanceof SyntaxError) error = new _errors.YAMLSyntaxError(node, error.message);else error.source = node;
errors.push(error);
return null;
}
errors.push(new _errors.YAMLSyntaxError(node, "Failed to resolve ".concat(node.type, " node here")));
return null;
}
}, {
key: "toJSON",
value: function toJSON() {
return (0, _Collection.toJSON)(this.contents);
}
}, {
key: "toString",
value: function toString() {
var _this2 = this;

if (Array.isArray(this.contents)) {
return this.contents.map(function (c) {
return _this2.tags.stringify(c, {
indent: ''
});
}).join('\n---\n') + '\n';
}

return this.tags.stringify(this.contents, {
indent: ''
}) + '\n';
}
}]);

return Document;
}();

exports.default = Document;
Loading

0 comments on commit 23f382d

Please sign in to comment.