Skip to content

Commit 61ae8d6

Browse files
committed
1.0.0-beta.3
1 parent ebedaa2 commit 61ae8d6

32 files changed

+4466
-1
lines changed

dist/Document.js

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
"use strict";
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.default = void 0;
7+
8+
var _Node = require("./ast/Node");
9+
10+
var _errors = require("./errors");
11+
12+
var _Tags = require("./Tags");
13+
14+
var _Collection = require("./schema/Collection");
15+
16+
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; }
17+
18+
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"); } }
19+
20+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
21+
22+
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); } }
23+
24+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
25+
26+
var Document =
27+
/*#__PURE__*/
28+
function () {
29+
_createClass(Document, [{
30+
key: "parseTagDirective",
31+
value: function parseTagDirective(directive) {
32+
var _directive$parameters = _slicedToArray(directive.parameters, 2),
33+
handle = _directive$parameters[0],
34+
prefix = _directive$parameters[1];
35+
36+
if (handle && prefix) {
37+
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.'));
38+
this.tagPrefixes[handle] = prefix;
39+
} else {
40+
this.errors.push(new _errors.YAMLSyntaxError(directive, 'Insufficient parameters given for TAG directive'));
41+
}
42+
}
43+
}, {
44+
key: "parseYamlDirective",
45+
value: function parseYamlDirective(directive) {
46+
var _directive$parameters2 = _slicedToArray(directive.parameters, 1),
47+
version = _directive$parameters2[0];
48+
49+
if (this.version) this.errors.push(new _errors.YAMLSyntaxError(directive, 'The YAML directive must only be given at most once per document.'));
50+
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)));
51+
this.version = version;
52+
}
53+
}]);
54+
55+
function Document(tags) {
56+
var _this = this;
57+
58+
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
59+
_ref$directives = _ref.directives,
60+
directives = _ref$directives === void 0 ? [] : _ref$directives,
61+
_ref$contents = _ref.contents,
62+
contents = _ref$contents === void 0 ? [] : _ref$contents,
63+
error = _ref.error;
64+
65+
var _ref2 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
66+
merge = _ref2.merge;
67+
68+
_classCallCheck(this, Document);
69+
70+
this.anchors = [];
71+
this.directives = directives;
72+
this.errors = [];
73+
74+
if (error) {
75+
if (!error.source) error.source = this;
76+
this.errors.push(error);
77+
}
78+
79+
this.options = {
80+
merge: merge
81+
};
82+
this.rawContents = contents;
83+
this.tagPrefixes = {};
84+
this.tags = tags;
85+
this.version = null;
86+
directives.forEach(function (directive) {
87+
var name = directive.name;
88+
89+
switch (name) {
90+
case 'TAG':
91+
_this.parseTagDirective(directive);
92+
93+
break;
94+
95+
case 'YAML':
96+
_this.parseYamlDirective(directive);
97+
98+
break;
99+
100+
default:
101+
if (name) _this.errors.push(new _errors.YAMLWarning(directive, "YAML 1.2 only supports TAG and YAML directives, and not ".concat(name)));
102+
}
103+
});
104+
var contentNodes = contents.filter(function (node) {
105+
return node.valueRange && !node.valueRange.isEmpty;
106+
}).map(function (node) {
107+
return _this.resolveNode(node);
108+
});
109+
110+
switch (contentNodes.length) {
111+
case 0:
112+
this.contents = null;
113+
break;
114+
115+
case 1:
116+
this.contents = contentNodes[0];
117+
break;
118+
119+
default:
120+
this.errors.push(new _errors.YAMLSyntaxError(null, 'Document is not valid YAML (bad indentation?)'));
121+
this.contents = contentNodes;
122+
}
123+
}
124+
125+
_createClass(Document, [{
126+
key: "resolveTagName",
127+
value: function resolveTagName(node) {
128+
var tag = node.tag,
129+
type = node.type;
130+
var nonSpecific = false;
131+
132+
if (tag) {
133+
var handle = tag.handle,
134+
suffix = tag.suffix,
135+
verbatim = tag.verbatim;
136+
137+
if (verbatim) {
138+
if (verbatim !== '!' && verbatim !== '!!') return verbatim;
139+
this.errors.push(new _errors.YAMLSyntaxError(node, "Verbatim tags aren't resolved, so ".concat(verbatim, " is invalid.")));
140+
} else if (handle === '!' && !suffix) {
141+
nonSpecific = true;
142+
} else {
143+
var prefix = this.tagPrefixes[handle] || _Tags.DefaultTagPrefixes[handle];
144+
145+
if (prefix) {
146+
if (suffix) return prefix + suffix;
147+
this.errors.push(new _errors.YAMLSyntaxError(node, "The ".concat(handle, " tag has no suffix.")));
148+
} else {
149+
this.errors.push(new _errors.YAMLSyntaxError(node, "The ".concat(handle, " tag handle is non-default and was not declared.")));
150+
}
151+
}
152+
}
153+
154+
switch (type) {
155+
case _Node.Type.BLOCK_FOLDED:
156+
case _Node.Type.BLOCK_LITERAL:
157+
case _Node.Type.QUOTE_DOUBLE:
158+
case _Node.Type.QUOTE_SINGLE:
159+
return _Tags.DefaultTags.STR;
160+
161+
case _Node.Type.FLOW_MAP:
162+
case _Node.Type.MAP:
163+
return _Tags.DefaultTags.MAP;
164+
165+
case _Node.Type.FLOW_SEQ:
166+
case _Node.Type.SEQ:
167+
return _Tags.DefaultTags.SEQ;
168+
169+
case _Node.Type.PLAIN:
170+
return nonSpecific ? _Tags.DefaultTags.STR : null;
171+
172+
default:
173+
return null;
174+
}
175+
}
176+
}, {
177+
key: "resolveNode",
178+
value: function resolveNode(node) {
179+
if (!node) return null;
180+
var anchors = this.anchors,
181+
errors = this.errors,
182+
tags = this.tags;
183+
var hasAnchor = false;
184+
var hasTag = false;
185+
node.props.forEach(function (_ref3) {
186+
var start = _ref3.start,
187+
end = _ref3.end;
188+
189+
switch (node.context.src[start]) {
190+
case _Node.Char.COMMENT:
191+
if (!node.commentHasRequiredWhitespace(start)) errors.push(new _errors.YAMLSyntaxError(node, 'Comments must be separated from other tokens by white space characters'));
192+
break;
193+
194+
case _Node.Char.ANCHOR:
195+
if (hasAnchor) errors.push(new _errors.YAMLSyntaxError(node, 'A node can have at most one anchor'));
196+
hasAnchor = true;
197+
break;
198+
199+
case _Node.Char.TAG:
200+
if (hasTag) errors.push(new _errors.YAMLSyntaxError(node, 'A node can have at most one tag'));
201+
hasTag = true;
202+
break;
203+
}
204+
});
205+
var anchor = node.anchor;
206+
if (anchor) anchors[anchor] = node;
207+
208+
if (node.type === _Node.Type.ALIAS) {
209+
if (hasAnchor || hasTag) errors.push(new _errors.YAMLSyntaxError(node, 'An alias node must not specify any properties'));
210+
var src = anchors[node.rawValue];
211+
if (src) return node.resolved = src.resolved;
212+
errors.push(new _errors.YAMLReferenceError(node, "Aliased anchor not found: ".concat(node.rawValue)));
213+
return null;
214+
}
215+
216+
var tagName = this.resolveTagName(node);
217+
if (tagName) return node.resolved = tags.resolve(this, node, tagName);
218+
if (node.type === _Node.Type.PLAIN) try {
219+
return node.resolved = tags.resolveScalar(node.strValue || '');
220+
} catch (error) {
221+
if (!error.source) error.source = node;
222+
errors.push(error);
223+
return null;
224+
}
225+
errors.push(new _errors.YAMLSyntaxError(node, "Failed to resolve ".concat(node.type, " node here")));
226+
return null;
227+
}
228+
}, {
229+
key: "toJSON",
230+
value: function toJSON() {
231+
return (0, _Collection.toJSON)(this.contents);
232+
}
233+
}, {
234+
key: "toString",
235+
value: function toString() {
236+
var _this2 = this;
237+
238+
if (Array.isArray(this.contents)) {
239+
return this.contents.map(function (c) {
240+
return _this2.tags.stringify(c, {
241+
indent: ''
242+
});
243+
}).join('\n---\n') + '\n';
244+
}
245+
246+
return this.tags.stringify(this.contents, {
247+
indent: ''
248+
}) + '\n';
249+
}
250+
}]);
251+
252+
return Document;
253+
}();
254+
255+
exports.default = Document;

0 commit comments

Comments
 (0)