| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- "use strict";
-
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = parseSeq;
-
- var _constants = require("../constants");
-
- var _errors = require("../errors");
-
- var _Pair = _interopRequireDefault(require("./Pair"));
-
- var _parseUtils = require("./parseUtils");
-
- var _Seq = _interopRequireDefault(require("./Seq"));
-
- var _Collection = _interopRequireDefault(require("./Collection"));
-
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
-
- function parseSeq(doc, cst) {
- if (cst.type !== _constants.Type.SEQ && cst.type !== _constants.Type.FLOW_SEQ) {
- const msg = `A ${cst.type} node cannot be resolved as a sequence`;
- doc.errors.push(new _errors.YAMLSyntaxError(cst, msg));
- return null;
- }
-
- const {
- comments,
- items
- } = cst.type === _constants.Type.FLOW_SEQ ? resolveFlowSeqItems(doc, cst) : resolveBlockSeqItems(doc, cst);
- const seq = new _Seq.default();
- seq.items = items;
- (0, _parseUtils.resolveComments)(seq, comments);
-
- if (!doc.options.mapAsMap && items.some(it => it instanceof _Pair.default && it.key instanceof _Collection.default)) {
- const warn = 'Keys with collection values will be stringified as YAML due to JS Object restrictions. Use mapAsMap: true to avoid this.';
- doc.warnings.push(new _errors.YAMLWarning(cst, warn));
- }
-
- cst.resolved = seq;
- return seq;
- }
-
- function resolveBlockSeqItems(doc, cst) {
- const comments = [];
- const items = [];
-
- for (let i = 0; i < cst.items.length; ++i) {
- const item = cst.items[i];
-
- switch (item.type) {
- case _constants.Type.BLANK_LINE:
- comments.push({
- before: items.length
- });
- break;
-
- case _constants.Type.COMMENT:
- comments.push({
- comment: item.comment,
- before: items.length
- });
- break;
-
- case _constants.Type.SEQ_ITEM:
- if (item.error) doc.errors.push(item.error);
- items.push(doc.resolveNode(item.node));
-
- if (item.hasProps) {
- const msg = 'Sequence items cannot have tags or anchors before the - indicator';
- doc.errors.push(new _errors.YAMLSemanticError(item, msg));
- }
-
- break;
-
- default:
- if (item.error) doc.errors.push(item.error);
- doc.errors.push(new _errors.YAMLSyntaxError(item, `Unexpected ${item.type} node in sequence`));
- }
- }
-
- return {
- comments,
- items
- };
- }
-
- function resolveFlowSeqItems(doc, cst) {
- const comments = [];
- const items = [];
- let explicitKey = false;
- let key = undefined;
- let keyStart = null;
- let next = '[';
-
- for (let i = 0; i < cst.items.length; ++i) {
- const item = cst.items[i];
-
- if (typeof item.char === 'string') {
- const {
- char,
- offset
- } = item;
-
- if (char !== ':' && (explicitKey || key !== undefined)) {
- if (explicitKey && key === undefined) key = next ? items.pop() : null;
- items.push(new _Pair.default(key));
- explicitKey = false;
- key = undefined;
- keyStart = null;
- }
-
- if (char === next) {
- next = null;
- } else if (!next && char === '?') {
- explicitKey = true;
- } else if (next !== '[' && char === ':' && key === undefined) {
- if (next === ',') {
- key = items.pop();
-
- if (key instanceof _Pair.default) {
- const msg = 'Chaining flow sequence pairs is invalid';
- const err = new _errors.YAMLSemanticError(cst, msg);
- err.offset = offset;
- doc.errors.push(err);
- }
-
- if (!explicitKey) (0, _parseUtils.checkKeyLength)(doc.errors, cst, i, key, keyStart);
- } else {
- key = null;
- }
-
- keyStart = null;
- explicitKey = false; // TODO: add error for non-explicit multiline plain key
-
- next = null;
- } else if (next === '[' || char !== ']' || i < cst.items.length - 1) {
- const msg = `Flow sequence contains an unexpected ${char}`;
- const err = new _errors.YAMLSyntaxError(cst, msg);
- err.offset = offset;
- doc.errors.push(err);
- }
- } else if (item.type === _constants.Type.BLANK_LINE) {
- comments.push({
- before: items.length
- });
- } else if (item.type === _constants.Type.COMMENT) {
- comments.push({
- comment: item.comment,
- before: items.length
- });
- } else {
- if (next) {
- const msg = `Expected a ${next} in flow sequence`;
- doc.errors.push(new _errors.YAMLSemanticError(item, msg));
- }
-
- const value = doc.resolveNode(item);
-
- if (key === undefined) {
- items.push(value);
- } else {
- items.push(new _Pair.default(key, value));
- key = undefined;
- }
-
- keyStart = item.range.start;
- next = ',';
- }
- }
-
- (0, _parseUtils.checkFlowCollectionEnd)(doc.errors, cst);
- if (key !== undefined) items.push(new _Pair.default(key));
- return {
- comments,
- items
- };
- }
|