123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- "use strict";
-
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = parseMap;
-
- var _constants = require("../constants");
-
- var _PlainValue = _interopRequireDefault(require("../cst/PlainValue"));
-
- var _errors = require("../errors");
-
- var _Map = _interopRequireDefault(require("./Map"));
-
- var _Merge = _interopRequireWildcard(require("./Merge"));
-
- var _Pair = _interopRequireDefault(require("./Pair"));
-
- var _parseUtils = require("./parseUtils");
-
- var _Alias = _interopRequireDefault(require("./Alias"));
-
- var _Collection = _interopRequireDefault(require("./Collection"));
-
- function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
-
- function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
-
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
-
- function parseMap(doc, cst) {
- if (cst.type !== _constants.Type.MAP && cst.type !== _constants.Type.FLOW_MAP) {
- const msg = `A ${cst.type} node cannot be resolved as a mapping`;
- doc.errors.push(new _errors.YAMLSyntaxError(cst, msg));
- return null;
- }
-
- const {
- comments,
- items
- } = cst.type === _constants.Type.FLOW_MAP ? resolveFlowMapItems(doc, cst) : resolveBlockMapItems(doc, cst);
- const map = new _Map.default();
- map.items = items;
- (0, _parseUtils.resolveComments)(map, comments);
- let hasCollectionKey = false;
-
- for (let i = 0; i < items.length; ++i) {
- const {
- key: iKey
- } = items[i];
- if (iKey instanceof _Collection.default) hasCollectionKey = true;
-
- if (doc.schema.merge && iKey && iKey.value === _Merge.MERGE_KEY) {
- items[i] = new _Merge.default(items[i]);
- const sources = items[i].value.items;
- let error = null;
- sources.some(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.
- const {
- type
- } = node.source;
- if (type === _constants.Type.MAP || type === _constants.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));
- } else {
- for (let j = i + 1; j < items.length; ++j) {
- const {
- key: jKey
- } = items[j];
-
- if (iKey === jKey || iKey && jKey && Object.prototype.hasOwnProperty.call(iKey, 'value') && iKey.value === jKey.value) {
- const msg = `Map keys must be unique; "${iKey}" is repeated`;
- doc.errors.push(new _errors.YAMLSemanticError(cst, msg));
- break;
- }
- }
- }
- }
-
- if (hasCollectionKey && !doc.options.mapAsMap) {
- 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 = map;
- return map;
- }
-
- const valueHasPairComment = ({
- context: {
- lineStart,
- node,
- src
- },
- props
- }) => {
- if (props.length === 0) return false;
- const {
- start
- } = props[0];
- if (node && start > node.valueRange.start) return false;
- if (src[start] !== _constants.Char.COMMENT) return false;
-
- for (let i = lineStart; i < start; ++i) if (src[i] === '\n') return false;
-
- return true;
- };
-
- function resolvePairComment(item, pair) {
- if (!valueHasPairComment(item)) return;
- const comment = item.getPropValue(0, _constants.Char.COMMENT, true);
- let found = false;
- const cb = pair.value.commentBefore;
-
- if (cb && cb.startsWith(comment)) {
- pair.value.commentBefore = cb.substr(comment.length + 1);
- found = true;
- } else {
- const cc = pair.value.comment;
-
- if (!item.node && cc && cc.startsWith(comment)) {
- pair.value.comment = cc.substr(comment.length + 1);
- found = true;
- }
- }
-
- if (found) pair.comment = comment;
- }
-
- function resolveBlockMapItems(doc, cst) {
- const comments = [];
- const items = [];
- let key = undefined;
- let keyStart = null;
-
- for (let i = 0; i < cst.items.length; ++i) {
- const item = cst.items[i];
-
- switch (item.type) {
- case _constants.Type.BLANK_LINE:
- comments.push({
- afterKey: !!key,
- before: items.length
- });
- break;
-
- case _constants.Type.COMMENT:
- comments.push({
- afterKey: !!key,
- before: items.length,
- comment: item.comment
- });
- break;
-
- case _constants.Type.MAP_KEY:
- if (key !== undefined) items.push(new _Pair.default(key));
- if (item.error) doc.errors.push(item.error);
- key = doc.resolveNode(item.node);
- keyStart = null;
- break;
-
- case _constants.Type.MAP_VALUE:
- {
- if (key === undefined) key = null;
- if (item.error) doc.errors.push(item.error);
-
- if (!item.context.atLineStart && item.node && item.node.type === _constants.Type.MAP && !item.node.context.atLineStart) {
- const msg = 'Nested mappings are not allowed in compact mappings';
- doc.errors.push(new _errors.YAMLSemanticError(item.node, msg));
- }
-
- let valueNode = item.node;
-
- if (!valueNode && item.props.length > 0) {
- // Comments on an empty mapping value need to be preserved, so we
- // need to construct a minimal empty node here to use instead of the
- // missing `item.node`. -- eemeli/yaml#19
- valueNode = new _PlainValue.default(_constants.Type.PLAIN, []);
- valueNode.context = {
- parent: item,
- src: item.context.src
- };
- const pos = item.range.start + 1;
- valueNode.range = {
- start: pos,
- end: pos
- };
- valueNode.valueRange = {
- start: pos,
- end: pos
- };
-
- if (typeof item.range.origStart === 'number') {
- const origPos = item.range.origStart + 1;
- valueNode.range.origStart = valueNode.range.origEnd = origPos;
- valueNode.valueRange.origStart = valueNode.valueRange.origEnd = origPos;
- }
- }
-
- const pair = new _Pair.default(key, doc.resolveNode(valueNode));
- resolvePairComment(item, pair);
- items.push(pair);
- (0, _parseUtils.checkKeyLength)(doc.errors, cst, i, key, keyStart);
- key = undefined;
- keyStart = null;
- }
- break;
-
- default:
- if (key !== undefined) items.push(new _Pair.default(key));
- key = doc.resolveNode(item);
- keyStart = item.range.start;
- if (item.error) doc.errors.push(item.error);
-
- next: for (let j = i + 1;; ++j) {
- const nextItem = cst.items[j];
-
- switch (nextItem && nextItem.type) {
- case _constants.Type.BLANK_LINE:
- case _constants.Type.COMMENT:
- continue next;
-
- case _constants.Type.MAP_VALUE:
- break next;
-
- default:
- doc.errors.push(new _errors.YAMLSemanticError(item, 'Implicit map keys need to be followed by map values'));
- break next;
- }
- }
-
- if (item.valueRangeContainsNewline) {
- const msg = 'Implicit map keys need to be on a single line';
- doc.errors.push(new _errors.YAMLSemanticError(item, msg));
- }
-
- }
- }
-
- if (key !== undefined) items.push(new _Pair.default(key));
- return {
- comments,
- items
- };
- }
-
- function resolveFlowMapItems(doc, cst) {
- const comments = [];
- const items = [];
- let key = undefined;
- let keyStart = null;
- let explicitKey = false;
- let next = '{';
-
- for (let i = 0; i < cst.items.length; ++i) {
- (0, _parseUtils.checkKeyLength)(doc.errors, cst, i, key, keyStart);
- const item = cst.items[i];
-
- if (typeof item.char === 'string') {
- const {
- char,
- offset
- } = item;
-
- if (char === '?' && key === undefined && !explicitKey) {
- explicitKey = true;
- next = ':';
- continue;
- }
-
- if (char === ':') {
- if (key === undefined) key = null;
-
- if (next === ':') {
- next = ',';
- continue;
- }
- } else {
- if (explicitKey) {
- if (key === undefined && char !== ',') key = null;
- explicitKey = false;
- }
-
- if (key !== undefined) {
- items.push(new _Pair.default(key));
- key = undefined;
- keyStart = null;
-
- if (char === ',') {
- next = ':';
- continue;
- }
- }
- }
-
- if (char === '}') {
- if (i === cst.items.length - 1) continue;
- } else if (char === next) {
- next = ':';
- continue;
- }
-
- const msg = `Flow map 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({
- afterKey: !!key,
- before: items.length
- });
- } else if (item.type === _constants.Type.COMMENT) {
- comments.push({
- afterKey: !!key,
- before: items.length,
- comment: item.comment
- });
- } else if (key === undefined) {
- if (next === ',') doc.errors.push(new _errors.YAMLSemanticError(item, 'Separator , missing in flow map'));
- key = doc.resolveNode(item);
- keyStart = explicitKey ? null : item.range.start; // TODO: add error for non-explicit multiline plain key
- } else {
- if (next !== ',') doc.errors.push(new _errors.YAMLSemanticError(item, 'Indicator : missing in flow map entry'));
- items.push(new _Pair.default(key, doc.resolveNode(item)));
- key = undefined;
- explicitKey = false;
- }
- }
-
- (0, _parseUtils.checkFlowCollectionEnd)(doc.errors, cst);
- if (key !== undefined) items.push(new _Pair.default(key));
- return {
- comments,
- items
- };
- }
|