Comparing version 1.6.0 to 1.7.0
@@ -18,3 +18,3 @@ "use strict"; | ||
var _getLinePos = _interopRequireDefault(require("./getLinePos")); | ||
var _sourceUtils = require("./source-utils"); | ||
@@ -341,5 +341,5 @@ var _Range = _interopRequireDefault(require("./Range")); | ||
if (!this.range || !this.context) return undefined; | ||
var start = (0, _getLinePos.default)(this.range.start, this.context.root); | ||
var start = (0, _sourceUtils.getLinePos)(this.range.start, this.context.root); | ||
if (!start) return undefined; | ||
var end = (0, _getLinePos.default)(this.range.end, this.context.root); | ||
var end = (0, _sourceUtils.getLinePos)(this.range.end, this.context.root); | ||
return { | ||
@@ -346,0 +346,0 @@ start: start, |
@@ -171,3 +171,3 @@ "use strict"; | ||
var next = src[offset + 1]; | ||
if (ch === ':' && (next === '\n' || next === '\t' || next === ' ' || inFlow && next === ',')) break; | ||
if (ch === ':' && (!next || next === '\n' || next === '\t' || next === ' ' || inFlow && next === ',')) break; | ||
if ((ch === ' ' || ch === '\t') && next === '#') break; | ||
@@ -174,0 +174,0 @@ offset += 1; |
@@ -143,4 +143,16 @@ "use strict"; | ||
key: "setSchema", | ||
value: function setSchema() { | ||
if (!this.schema) this.schema = new _schema.default(Object.assign({}, this.getDefaults(), this.options)); | ||
value: function setSchema(id, customTags) { | ||
if (!id && !customTags && this.schema) return; | ||
if (typeof id === 'number') id = id.toFixed(1); | ||
if (id === '1.0' || id === '1.1' || id === '1.2') { | ||
if (this.version) this.version = id;else this.options.version = id; | ||
delete this.options.schema; | ||
} else if (id && typeof id === 'string') { | ||
this.options.schema = id; | ||
} | ||
if (Array.isArray(customTags)) this.options.customTags = customTags; | ||
var opt = Object.assign({}, this.getDefaults(), this.options); | ||
this.schema = new _schema.default(opt); | ||
} | ||
@@ -147,0 +159,0 @@ }, { |
@@ -24,2 +24,6 @@ "use strict"; | ||
var _sourceUtils = require("./cst/source-utils"); | ||
var _Range = _interopRequireDefault(require("./cst/Range")); | ||
var YAMLError = | ||
@@ -45,8 +49,37 @@ /*#__PURE__*/ | ||
value: function makePretty() { | ||
if (this.source) { | ||
this.nodeType = this.source.type; | ||
if (!this.source) return; | ||
this.nodeType = this.source.type; | ||
var cst = this.source.context && this.source.context.root; | ||
if (typeof this.offset === 'number') { | ||
this.range = new _Range.default(this.offset, this.offset + 1); | ||
var start = cst && (0, _sourceUtils.getLinePos)(this.offset, cst); | ||
if (start) { | ||
var end = { | ||
line: start.line, | ||
col: start.col + 1 | ||
}; | ||
this.linePos = { | ||
start: start, | ||
end: end | ||
}; | ||
} | ||
delete this.offset; | ||
} else { | ||
this.range = this.source.range; | ||
this.linePos = this.source.rangeAsLinePos; | ||
delete this.source; | ||
} | ||
if (this.linePos) { | ||
var _this$linePos$start = this.linePos.start, | ||
line = _this$linePos$start.line, | ||
col = _this$linePos$start.col; | ||
this.message += " at line ".concat(line, ", column ").concat(col); | ||
var ctx = cst && (0, _sourceUtils.getPrettyContext)(this.linePos, cst); | ||
if (ctx) this.message += ":\n\n".concat(ctx, "\n"); | ||
} | ||
delete this.source; | ||
} | ||
@@ -53,0 +86,0 @@ }]); |
@@ -26,3 +26,4 @@ "use strict"; | ||
/* global console */ | ||
var _warnings = require("./warnings"); | ||
var defaultOptions = { | ||
@@ -38,2 +39,3 @@ anchorPrefix: 'a', | ||
// TODO Set true in v2 | ||
simpleKeys: false, | ||
version: '1.2' | ||
@@ -115,6 +117,5 @@ }; | ||
function parse(src, options) { | ||
var doc = parseDocument(src, options); // eslint-disable-next-line no-console | ||
var doc = parseDocument(src, options); | ||
doc.warnings.forEach(function (warning) { | ||
return console.warn(warning); | ||
return (0, _warnings.warn)(warning); | ||
}); | ||
@@ -121,0 +122,0 @@ if (doc.errors.length > 0) throw doc.errors[0]; |
@@ -122,3 +122,3 @@ "use strict"; | ||
if (!anchor || !anchor.res) { | ||
if (!anchor || anchor.res === undefined) { | ||
var msg = 'This should not happen: Alias anchor was not resolved?'; | ||
@@ -125,0 +125,0 @@ if (this.cstNode) throw new _errors.YAMLReferenceError(this.cstNode, msg);else throw new ReferenceError(msg); |
@@ -18,3 +18,3 @@ "use strict"; | ||
var _deprecation = require("../deprecation"); | ||
var _warnings = require("../warnings"); | ||
@@ -73,3 +73,3 @@ var _constants = require("../constants"); | ||
customTags = deprecatedCustomTags; | ||
(0, _deprecation.warnOptionDeprecation)('tags', 'customTags'); | ||
(0, _warnings.warnOptionDeprecation)('tags', 'customTags'); | ||
} | ||
@@ -255,3 +255,3 @@ | ||
var res = this.resolveNode(doc, node, tagName); | ||
if (node.hasOwnProperty('resolved')) return res; | ||
if (Object.prototype.hasOwnProperty.call(node, 'resolved')) return res; | ||
var fallback = isMap(node) ? Schema.defaultTags.MAP : isSeq(node) ? Schema.defaultTags.SEQ : Schema.defaultTags.STR; | ||
@@ -258,0 +258,0 @@ |
@@ -98,3 +98,3 @@ "use strict"; | ||
} else { | ||
if (!map.hasOwnProperty(key)) map[key] = value; | ||
if (!Object.prototype.hasOwnProperty.call(map, key)) map[key] = value; | ||
} | ||
@@ -101,0 +101,0 @@ } |
@@ -24,2 +24,4 @@ "use strict"; | ||
var _constants = require("../constants"); | ||
var _toJSON = _interopRequireDefault(require("../toJSON")); | ||
@@ -91,6 +93,19 @@ | ||
if (!ctx || !ctx.doc) return JSON.stringify(this); | ||
var simpleKeys = ctx.doc.options.simpleKeys; | ||
var key = this.key, | ||
value = this.value; | ||
var keyComment = key instanceof _Node2.default && key.comment; | ||
var explicitKey = !key || keyComment || key instanceof _Collection.default; | ||
if (simpleKeys) { | ||
if (keyComment) { | ||
throw new Error('With simple keys, key nodes cannot have comments'); | ||
} | ||
if (key instanceof _Collection.default) { | ||
var msg = 'With simple keys, collection cannot be used as a key value'; | ||
throw new Error(msg); | ||
} | ||
} | ||
var explicitKey = !simpleKeys && (!key || keyComment || key instanceof _Collection.default || key.type === _constants.Type.BLOCK_FOLDED || key.type === _constants.Type.BLOCK_LITERAL); | ||
var _ctx = ctx, | ||
@@ -111,3 +126,3 @@ doc = _ctx.doc, | ||
if (ctx.allNullValues) { | ||
if (ctx.allNullValues && !simpleKeys) { | ||
if (this.comment) { | ||
@@ -114,0 +129,0 @@ str = (0, _addComment.default)(str, ctx.indent, this.comment); |
@@ -28,2 +28,4 @@ "use strict"; | ||
var _Collection = _interopRequireDefault(require("./Collection")); | ||
function parseMap(doc, cst) { | ||
@@ -43,5 +45,7 @@ if (cst.type !== _constants.Type.MAP && cst.type !== _constants.Type.FLOW_MAP) { | ||
(0, _parseUtils.resolveComments)(map, comments); | ||
var hasCollectionKey = false; | ||
for (var i = 0; i < items.length; ++i) { | ||
var iKey = items[i].key; | ||
if (iKey instanceof _Collection.default) hasCollectionKey = true; | ||
@@ -68,3 +72,3 @@ if (doc.schema.merge && iKey && iKey.value === _Merge.MERGE_KEY) { | ||
if (iKey === jKey || iKey && jKey && iKey.hasOwnProperty('value') && iKey.value === jKey.value) { | ||
if (iKey === jKey || iKey && jKey && Object.prototype.hasOwnProperty.call(iKey, 'value') && iKey.value === jKey.value) { | ||
var _msg = "Map keys must be unique; \"".concat(iKey, "\" is repeated"); | ||
@@ -79,2 +83,7 @@ | ||
if (hasCollectionKey && !doc.options.mapAsMap) { | ||
var 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; | ||
@@ -253,3 +262,4 @@ return map; | ||
if (typeof item.char === 'string') { | ||
var char = item.char; | ||
var char = item.char, | ||
offset = item.offset; | ||
@@ -294,3 +304,6 @@ if (char === '?' && key === undefined && !explicitKey) { | ||
doc.errors.push(new _errors.YAMLSyntaxError(cst, "Flow map contains an unexpected ".concat(char))); | ||
var msg = "Flow map contains an unexpected ".concat(char); | ||
var err = new _errors.YAMLSyntaxError(cst, msg); | ||
err.offset = offset; | ||
doc.errors.push(err); | ||
} else if (item.type === _constants.Type.BLANK_LINE) { | ||
@@ -319,3 +332,3 @@ comments.push({ | ||
if (cst.items[cst.items.length - 1].char !== '}') doc.errors.push(new _errors.YAMLSemanticError(cst, 'Expected flow map to end with }')); | ||
(0, _parseUtils.checkFlowCollectionEnd)(doc.errors, cst); | ||
if (key !== undefined) items.push(new _Pair.default(key)); | ||
@@ -322,0 +335,0 @@ return { |
@@ -20,2 +20,4 @@ "use strict"; | ||
var _Collection = _interopRequireDefault(require("./Collection")); | ||
function parseSeq(doc, cst) { | ||
@@ -35,2 +37,10 @@ if (cst.type !== _constants.Type.SEQ && cst.type !== _constants.Type.FLOW_SEQ) { | ||
(0, _parseUtils.resolveComments)(seq, comments); | ||
if (!doc.options.mapAsMap && items.some(function (it) { | ||
return it instanceof _Pair.default && it.key instanceof _Collection.default; | ||
})) { | ||
var 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; | ||
@@ -96,3 +106,4 @@ return seq; | ||
if (typeof item.char === 'string') { | ||
var char = item.char; | ||
var char = item.char, | ||
offset = item.offset; | ||
@@ -116,4 +127,6 @@ if (char !== ':' && (explicitKey || key !== undefined)) { | ||
if (key instanceof _Pair.default) { | ||
var msg = 'Chaining flow sequence pairs is invalid (e.g. [ a : b : c ])'; | ||
doc.errors.push(new _errors.YAMLSemanticError(char, msg)); | ||
var msg = 'Chaining flow sequence pairs is invalid'; | ||
var err = new _errors.YAMLSemanticError(cst, msg); | ||
err.offset = offset; | ||
doc.errors.push(err); | ||
} | ||
@@ -133,3 +146,6 @@ | ||
doc.errors.push(new _errors.YAMLSyntaxError(cst, _msg)); | ||
var _err = new _errors.YAMLSyntaxError(cst, _msg); | ||
_err.offset = offset; | ||
doc.errors.push(_err); | ||
} | ||
@@ -147,3 +163,3 @@ } else if (item.type === _constants.Type.BLANK_LINE) { | ||
if (next) { | ||
var _msg2 = "Expected a ".concat(next, " here in flow sequence"); | ||
var _msg2 = "Expected a ".concat(next, " in flow sequence"); | ||
@@ -167,3 +183,3 @@ doc.errors.push(new _errors.YAMLSemanticError(item, _msg2)); | ||
if (cst.items[cst.items.length - 1].char !== ']') doc.errors.push(new _errors.YAMLSemanticError(cst, 'Expected flow sequence to end with ]')); | ||
(0, _parseUtils.checkFlowCollectionEnd)(doc.errors, cst); | ||
if (key !== undefined) items.push(new _Pair.default(key)); | ||
@@ -170,0 +186,0 @@ return { |
@@ -6,2 +6,3 @@ "use strict"; | ||
}); | ||
exports.checkFlowCollectionEnd = checkFlowCollectionEnd; | ||
exports.checkKeyLength = checkKeyLength; | ||
@@ -12,2 +13,50 @@ exports.resolveComments = resolveComments; | ||
var _constants = require("../constants"); | ||
function checkFlowCollectionEnd(errors, cst) { | ||
var char, name; | ||
switch (cst.type) { | ||
case _constants.Type.FLOW_MAP: | ||
char = '}'; | ||
name = 'flow map'; | ||
break; | ||
case _constants.Type.FLOW_SEQ: | ||
char = ']'; | ||
name = 'flow sequence'; | ||
break; | ||
default: | ||
errors.push(new _errors.YAMLSemanticError(cst, 'Not a flow collection!?')); | ||
return; | ||
} | ||
var lastItem; | ||
for (var i = cst.items.length - 1; i >= 0; --i) { | ||
var item = cst.items[i]; | ||
if (!item || item.type !== _constants.Type.COMMENT) { | ||
lastItem = item; | ||
break; | ||
} | ||
} | ||
if (lastItem && lastItem.char !== char) { | ||
var msg = "Expected ".concat(name, " to end with ").concat(char); | ||
var err; | ||
if (typeof lastItem.offset === 'number') { | ||
err = new _errors.YAMLSemanticError(cst, msg); | ||
err.offset = lastItem.offset + 1; | ||
} else { | ||
err = new _errors.YAMLSemanticError(lastItem, msg); | ||
if (lastItem.range && lastItem.range.end) err.offset = lastItem.range.end - lastItem.range.start; | ||
} | ||
errors.push(err); | ||
} | ||
} | ||
function checkKeyLength(errors, node, itemIdx, key, keyStart) { | ||
@@ -14,0 +63,0 @@ if (!key || typeof keyStart !== 'number') return; |
@@ -268,6 +268,7 @@ "use strict"; | ||
var str = value.replace(/\n+/g, "$&\n".concat(indent)); // May need to verify that output will be parsed as a string, as plain numbers | ||
// and booleans get parsed with those types, e.g. '42', 'true' & '0.9e-3'. | ||
var str = value.replace(/\n+/g, "$&\n".concat(indent)); // Verify that output will be parsed as a string, as e.g. plain numbers and | ||
// booleans get parsed with those types in v1.2 (e.g. '42', 'true' & '0.9e-3'), | ||
// and others in v1.1. | ||
if (actualString && /^[\w.+-]+$/.test(str) && typeof tags.resolveScalar(str).value !== 'string') { | ||
if (actualString && typeof tags.resolveScalar(str).value !== 'string') { | ||
return doubleQuotedString(value, ctx); | ||
@@ -274,0 +275,0 @@ } |
module.exports = require('./dist/schema/Map').default | ||
require('./dist/deprecation').warnFileDeprecation(__filename) | ||
require('./dist/warnings').warnFileDeprecation(__filename) |
module.exports = require('./dist/schema/Pair').default | ||
require('./dist/deprecation').warnFileDeprecation(__filename) | ||
require('./dist/warnings').warnFileDeprecation(__filename) |
module.exports = require('./dist/schema/Scalar').default | ||
require('./dist/deprecation').warnFileDeprecation(__filename) | ||
require('./dist/warnings').warnFileDeprecation(__filename) |
@@ -7,2 +7,2 @@ module.exports = require('./dist/schema').default | ||
require('./dist/deprecation').warnFileDeprecation(__filename) | ||
require('./dist/warnings').warnFileDeprecation(__filename) |
module.exports = require('./dist/schema/Seq').default | ||
require('./dist/deprecation').warnFileDeprecation(__filename) | ||
require('./dist/warnings').warnFileDeprecation(__filename) |
@@ -7,2 +7,2 @@ 'use strict' | ||
require('../dist/deprecation').warnFileDeprecation(__filename) | ||
require('../dist/warnings').warnFileDeprecation(__filename) |
module.exports = require('../dist/tags/yaml-1.1/omap').default | ||
require('../dist/deprecation').warnFileDeprecation(__filename) | ||
require('../dist/warnings').warnFileDeprecation(__filename) |
module.exports = require('../dist/tags/yaml-1.1/pairs').default | ||
require('../dist/deprecation').warnFileDeprecation(__filename) | ||
require('../dist/warnings').warnFileDeprecation(__filename) |
module.exports = require('../dist/tags/yaml-1.1/set').default | ||
require('../dist/deprecation').warnFileDeprecation(__filename) | ||
require('../dist/warnings').warnFileDeprecation(__filename) |
@@ -10,2 +10,2 @@ 'use strict' | ||
require('../dist/deprecation').warnFileDeprecation(__filename) | ||
require('../dist/warnings').warnFileDeprecation(__filename) |
@@ -7,2 +7,4 @@ "use strict"; | ||
exports.default = getLinePos; | ||
exports.getLine = getLine; | ||
exports.contextLineIndicator = contextLineIndicator; | ||
@@ -21,3 +23,31 @@ function findLineStarts(src) { | ||
} | ||
function getSrcInfo(cst) { | ||
let lineStarts, src; | ||
if (typeof cst === 'string') { | ||
lineStarts = findLineStarts(cst); | ||
src = cst; | ||
} else { | ||
if (Array.isArray(cst)) cst = cst[0]; | ||
if (cst && cst.context) { | ||
if (!cst.lineStarts) cst.lineStarts = findLineStarts(cst.context.src); | ||
lineStarts = cst.lineStarts; | ||
src = cst.context.src; | ||
} | ||
} | ||
return { | ||
lineStarts, | ||
src | ||
}; | ||
} | ||
/** | ||
* @typedef {Object} LinePos - One-indexed position in the source | ||
* @param {number} line | ||
* @param {number} col | ||
*/ | ||
/** | ||
* Determine the line/col position matching a character offset. | ||
@@ -34,3 +64,3 @@ * | ||
* @param {string|Document|Document[]} cst | ||
* @returns {{ line: number, col: number }|undefined} | ||
* @returns {?LinePos} | ||
*/ | ||
@@ -40,44 +70,106 @@ | ||
function getLinePos(offset, cst) { | ||
if (typeof offset === 'number' && offset >= 0) { | ||
let lineStarts, srcLength; | ||
if (typeof offset !== 'number' || offset < 0) return null; | ||
const { | ||
lineStarts, | ||
src | ||
} = getSrcInfo(cst); | ||
if (!lineStarts || !src || offset > src.length) return null; | ||
if (typeof cst === 'string') { | ||
lineStarts = findLineStarts(cst); | ||
srcLength = cst.length; | ||
} else { | ||
if (Array.isArray(cst)) cst = cst[0]; | ||
for (let i = 0; i < lineStarts.length; ++i) { | ||
const start = lineStarts[i]; | ||
if (cst) { | ||
if (!cst.lineStarts) cst.lineStarts = findLineStarts(cst.context.src); | ||
lineStarts = cst.lineStarts; | ||
srcLength = cst.context.src.length; | ||
} | ||
if (offset < start) { | ||
return { | ||
line: i, | ||
col: offset - lineStarts[i - 1] + 1 | ||
}; | ||
} | ||
if (lineStarts && offset <= srcLength) { | ||
for (let i = 0; i < lineStarts.length; ++i) { | ||
const start = lineStarts[i]; | ||
if (offset === start) return { | ||
line: i + 1, | ||
col: 1 | ||
}; | ||
} | ||
if (offset < start) { | ||
return { | ||
line: i, | ||
col: offset - lineStarts[i - 1] + 1 | ||
}; | ||
} | ||
const line = lineStarts.length; | ||
return { | ||
line, | ||
col: offset - lineStarts[line - 1] + 1 | ||
}; | ||
} | ||
/** | ||
* Get a specified line from the source. | ||
* | ||
* Accepts a source string or a CST document as the second parameter. With | ||
* the latter, starting indices for lines are cached in the document as | ||
* `lineStarts: number[]`. | ||
* | ||
* Returns the line as a string if found, or `null` otherwise. | ||
* | ||
* @param {number} line One-indexed line number | ||
* @param {string|Document|Document[]} cst | ||
* @returns {?string} | ||
*/ | ||
if (offset === start) return { | ||
line: i + 1, | ||
col: 1 | ||
}; | ||
} | ||
const line = lineStarts.length; | ||
return { | ||
line, | ||
col: offset - lineStarts[line - 1] + 1 | ||
}; | ||
function getLine(line, cst) { | ||
const { | ||
lineStarts, | ||
src | ||
} = getSrcInfo(cst); | ||
if (!lineStarts || !(line >= 1) || line > lineStarts.length) return null; | ||
const start = lineStarts[line - 1]; | ||
let end = lineStarts[line]; // undefined for last line; that's ok for slice() | ||
while (end && end > start && src[end - 1] === '\n') --end; | ||
return src.slice(start, end); | ||
} | ||
/** | ||
* | ||
* @param {Object} pos | ||
* @param {LinePos} pos.start | ||
* @param {LinePos} [pos.end] | ||
* @param {string|Document|Document[]*} cst | ||
* @param {number} [maxWidth=80] | ||
* @returns {?string} | ||
*/ | ||
function contextLineIndicator({ | ||
start, | ||
end | ||
}, cst, maxWidth = 80) { | ||
let src = getLine(start.line, cst); | ||
if (!src) return null; | ||
let { | ||
col | ||
} = start; | ||
if (src.length > maxWidth) { | ||
if (col <= maxWidth - 10) { | ||
src = src.substr(0, maxWidth - 1) + '…'; | ||
} else { | ||
const halfWidth = Math.round(maxWidth / 2); | ||
if (src.length > col + halfWidth) src = src.substr(0, col + halfWidth - 1) + '…'; | ||
col -= src.length - maxWidth; | ||
src = '…' + src.substr(1 - maxWidth); | ||
} | ||
} | ||
return undefined; | ||
let errLen = 1; | ||
let errEnd = ''; | ||
if (end) { | ||
if (end.line === start.line && col + (end.col - start.col) <= maxWidth + 1) { | ||
errLen = end.col - start.col; | ||
} else { | ||
errLen = Math.min(src.length + 1, maxWidth) - col; | ||
errEnd = '…'; | ||
} | ||
} | ||
const offset = col > 1 ? ' '.repeat(col - 1) : ''; | ||
const err = '^'.repeat(errLen); | ||
return `${src}\n${offset}${err}${errEnd}`; | ||
} |
@@ -10,3 +10,3 @@ "use strict"; | ||
var _getLinePos = _interopRequireDefault(require("./getLinePos")); | ||
var _sourceUtils = require("./source-utils"); | ||
@@ -264,5 +264,5 @@ var _Range = _interopRequireDefault(require("./Range")); | ||
if (!this.range || !this.context) return undefined; | ||
const start = (0, _getLinePos.default)(this.range.start, this.context.root); | ||
const start = (0, _sourceUtils.getLinePos)(this.range.start, this.context.root); | ||
if (!start) return undefined; | ||
const end = (0, _getLinePos.default)(this.range.end, this.context.root); | ||
const end = (0, _sourceUtils.getLinePos)(this.range.end, this.context.root); | ||
return { | ||
@@ -269,0 +269,0 @@ start, |
@@ -22,3 +22,3 @@ "use strict"; | ||
const next = src[offset + 1]; | ||
if (ch === ':' && (next === '\n' || next === '\t' || next === ' ' || inFlow && next === ',')) break; | ||
if (ch === ':' && (!next || next === '\n' || next === '\t' || next === ' ' || inFlow && next === ',')) break; | ||
if ((ch === ' ' || ch === '\t') && next === '#') break; | ||
@@ -25,0 +25,0 @@ offset += 1; |
@@ -118,4 +118,16 @@ "use strict"; | ||
setSchema() { | ||
if (!this.schema) this.schema = new _schema.default(Object.assign({}, this.getDefaults(), this.options)); | ||
setSchema(id, customTags) { | ||
if (!id && !customTags && this.schema) return; | ||
if (typeof id === 'number') id = id.toFixed(1); | ||
if (id === '1.0' || id === '1.1' || id === '1.2') { | ||
if (this.version) this.version = id;else this.options.version = id; | ||
delete this.options.schema; | ||
} else if (id && typeof id === 'string') { | ||
this.options.schema = id; | ||
} | ||
if (Array.isArray(customTags)) this.options.customTags = customTags; | ||
const opt = Object.assign({}, this.getDefaults(), this.options); | ||
this.schema = new _schema.default(opt); | ||
} | ||
@@ -122,0 +134,0 @@ |
@@ -10,2 +10,6 @@ "use strict"; | ||
var _sourceUtils = require("./cst/source-utils"); | ||
var _Range = _interopRequireDefault(require("./cst/Range")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -23,8 +27,38 @@ | ||
makePretty() { | ||
if (this.source) { | ||
this.nodeType = this.source.type; | ||
if (!this.source) return; | ||
this.nodeType = this.source.type; | ||
const cst = this.source.context && this.source.context.root; | ||
if (typeof this.offset === 'number') { | ||
this.range = new _Range.default(this.offset, this.offset + 1); | ||
const start = cst && (0, _sourceUtils.getLinePos)(this.offset, cst); | ||
if (start) { | ||
const end = { | ||
line: start.line, | ||
col: start.col + 1 | ||
}; | ||
this.linePos = { | ||
start, | ||
end | ||
}; | ||
} | ||
delete this.offset; | ||
} else { | ||
this.range = this.source.range; | ||
this.linePos = this.source.rangeAsLinePos; | ||
delete this.source; | ||
} | ||
if (this.linePos) { | ||
const { | ||
line, | ||
col | ||
} = this.linePos.start; | ||
this.message += ` at line ${line}, column ${col}`; | ||
const ctx = cst && (0, _sourceUtils.getPrettyContext)(this.linePos, cst); | ||
if (ctx) this.message += `:\n\n${ctx}\n`; | ||
} | ||
delete this.source; | ||
} | ||
@@ -31,0 +65,0 @@ |
@@ -16,5 +16,6 @@ "use strict"; | ||
var _warnings = require("./warnings"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/* global console */ | ||
const defaultOptions = { | ||
@@ -30,2 +31,3 @@ anchorPrefix: 'a', | ||
// TODO Set true in v2 | ||
simpleKeys: false, | ||
version: '1.2' | ||
@@ -79,5 +81,4 @@ }; | ||
function parse(src, options) { | ||
const doc = parseDocument(src, options); // eslint-disable-next-line no-console | ||
doc.warnings.forEach(warning => console.warn(warning)); | ||
const doc = parseDocument(src, options); | ||
doc.warnings.forEach(warning => (0, _warnings.warn)(warning)); | ||
if (doc.errors.length > 0) throw doc.errors[0]; | ||
@@ -84,0 +85,0 @@ return doc.toJSON(); |
@@ -81,3 +81,3 @@ "use strict"; | ||
if (!anchor || !anchor.res) { | ||
if (!anchor || anchor.res === undefined) { | ||
const msg = 'This should not happen: Alias anchor was not resolved?'; | ||
@@ -84,0 +84,0 @@ if (this.cstNode) throw new _errors.YAMLReferenceError(this.cstNode, msg);else throw new ReferenceError(msg); |
@@ -8,3 +8,3 @@ "use strict"; | ||
var _deprecation = require("../deprecation"); | ||
var _warnings = require("../warnings"); | ||
@@ -61,3 +61,3 @@ var _constants = require("../constants"); | ||
customTags = deprecatedCustomTags; | ||
(0, _deprecation.warnOptionDeprecation)('tags', 'customTags'); | ||
(0, _warnings.warnOptionDeprecation)('tags', 'customTags'); | ||
} | ||
@@ -203,3 +203,3 @@ | ||
const res = this.resolveNode(doc, node, tagName); | ||
if (node.hasOwnProperty('resolved')) return res; | ||
if (Object.prototype.hasOwnProperty.call(node, 'resolved')) return res; | ||
const fallback = isMap(node) ? Schema.defaultTags.MAP : isSeq(node) ? Schema.defaultTags.SEQ : Schema.defaultTags.STR; | ||
@@ -206,0 +206,0 @@ |
@@ -62,3 +62,3 @@ "use strict"; | ||
} else { | ||
if (!map.hasOwnProperty(key)) map[key] = value; | ||
if (!Object.prototype.hasOwnProperty.call(map, key)) map[key] = value; | ||
} | ||
@@ -65,0 +65,0 @@ } |
@@ -10,2 +10,4 @@ "use strict"; | ||
var _constants = require("../constants"); | ||
var _toJSON = _interopRequireDefault(require("../toJSON")); | ||
@@ -75,2 +77,5 @@ | ||
if (!ctx || !ctx.doc) return JSON.stringify(this); | ||
const { | ||
simpleKeys | ||
} = ctx.doc.options; | ||
let { | ||
@@ -81,3 +86,15 @@ key, | ||
let keyComment = key instanceof _Node.default && key.comment; | ||
const explicitKey = !key || keyComment || key instanceof _Collection.default; | ||
if (simpleKeys) { | ||
if (keyComment) { | ||
throw new Error('With simple keys, key nodes cannot have comments'); | ||
} | ||
if (key instanceof _Collection.default) { | ||
const msg = 'With simple keys, collection cannot be used as a key value'; | ||
throw new Error(msg); | ||
} | ||
} | ||
const explicitKey = !simpleKeys && (!key || keyComment || key instanceof _Collection.default || key.type === _constants.Type.BLOCK_FOLDED || key.type === _constants.Type.BLOCK_LITERAL); | ||
const { | ||
@@ -95,3 +112,3 @@ doc, | ||
if (ctx.allNullValues) { | ||
if (ctx.allNullValues && !simpleKeys) { | ||
if (this.comment) { | ||
@@ -98,0 +115,0 @@ str = (0, _addComment.default)(str, ctx.indent, this.comment); |
@@ -24,2 +24,4 @@ "use strict"; | ||
var _Collection = _interopRequireDefault(require("./Collection")); | ||
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; } } | ||
@@ -43,2 +45,3 @@ | ||
(0, _parseUtils.resolveComments)(map, comments); | ||
let hasCollectionKey = false; | ||
@@ -49,2 +52,3 @@ for (let i = 0; i < items.length; ++i) { | ||
} = items[i]; | ||
if (iKey instanceof _Collection.default) hasCollectionKey = true; | ||
@@ -75,3 +79,3 @@ if (doc.schema.merge && iKey && iKey.value === _Merge.MERGE_KEY) { | ||
if (iKey === jKey || iKey && jKey && iKey.hasOwnProperty('value') && iKey.value === jKey.value) { | ||
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`; | ||
@@ -85,2 +89,7 @@ doc.errors.push(new _errors.YAMLSemanticError(cst, msg)); | ||
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; | ||
@@ -262,3 +271,4 @@ return map; | ||
const { | ||
char | ||
char, | ||
offset | ||
} = item; | ||
@@ -304,3 +314,6 @@ | ||
doc.errors.push(new _errors.YAMLSyntaxError(cst, `Flow map contains an unexpected ${char}`)); | ||
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) { | ||
@@ -329,3 +342,3 @@ comments.push({ | ||
if (cst.items[cst.items.length - 1].char !== '}') doc.errors.push(new _errors.YAMLSemanticError(cst, 'Expected flow map to end with }')); | ||
(0, _parseUtils.checkFlowCollectionEnd)(doc.errors, cst); | ||
if (key !== undefined) items.push(new _Pair.default(key)); | ||
@@ -332,0 +345,0 @@ return { |
@@ -18,2 +18,4 @@ "use strict"; | ||
var _Collection = _interopRequireDefault(require("./Collection")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -35,2 +37,8 @@ | ||
(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; | ||
@@ -97,3 +105,4 @@ return seq; | ||
const { | ||
char | ||
char, | ||
offset | ||
} = item; | ||
@@ -118,4 +127,6 @@ | ||
if (key instanceof _Pair.default) { | ||
const msg = 'Chaining flow sequence pairs is invalid (e.g. [ a : b : c ])'; | ||
doc.errors.push(new _errors.YAMLSemanticError(char, msg)); | ||
const msg = 'Chaining flow sequence pairs is invalid'; | ||
const err = new _errors.YAMLSemanticError(cst, msg); | ||
err.offset = offset; | ||
doc.errors.push(err); | ||
} | ||
@@ -134,3 +145,5 @@ | ||
const msg = `Flow sequence contains an unexpected ${char}`; | ||
doc.errors.push(new _errors.YAMLSyntaxError(cst, msg)); | ||
const err = new _errors.YAMLSyntaxError(cst, msg); | ||
err.offset = offset; | ||
doc.errors.push(err); | ||
} | ||
@@ -148,3 +161,3 @@ } else if (item.type === _constants.Type.BLANK_LINE) { | ||
if (next) { | ||
const msg = `Expected a ${next} here in flow sequence`; | ||
const msg = `Expected a ${next} in flow sequence`; | ||
doc.errors.push(new _errors.YAMLSemanticError(item, msg)); | ||
@@ -167,3 +180,3 @@ } | ||
if (cst.items[cst.items.length - 1].char !== ']') doc.errors.push(new _errors.YAMLSemanticError(cst, 'Expected flow sequence to end with ]')); | ||
(0, _parseUtils.checkFlowCollectionEnd)(doc.errors, cst); | ||
if (key !== undefined) items.push(new _Pair.default(key)); | ||
@@ -170,0 +183,0 @@ return { |
@@ -6,2 +6,3 @@ "use strict"; | ||
}); | ||
exports.checkFlowCollectionEnd = checkFlowCollectionEnd; | ||
exports.checkKeyLength = checkKeyLength; | ||
@@ -12,2 +13,50 @@ exports.resolveComments = resolveComments; | ||
var _constants = require("../constants"); | ||
function checkFlowCollectionEnd(errors, cst) { | ||
let char, name; | ||
switch (cst.type) { | ||
case _constants.Type.FLOW_MAP: | ||
char = '}'; | ||
name = 'flow map'; | ||
break; | ||
case _constants.Type.FLOW_SEQ: | ||
char = ']'; | ||
name = 'flow sequence'; | ||
break; | ||
default: | ||
errors.push(new _errors.YAMLSemanticError(cst, 'Not a flow collection!?')); | ||
return; | ||
} | ||
let lastItem; | ||
for (let i = cst.items.length - 1; i >= 0; --i) { | ||
const item = cst.items[i]; | ||
if (!item || item.type !== _constants.Type.COMMENT) { | ||
lastItem = item; | ||
break; | ||
} | ||
} | ||
if (lastItem && lastItem.char !== char) { | ||
const msg = `Expected ${name} to end with ${char}`; | ||
let err; | ||
if (typeof lastItem.offset === 'number') { | ||
err = new _errors.YAMLSemanticError(cst, msg); | ||
err.offset = lastItem.offset + 1; | ||
} else { | ||
err = new _errors.YAMLSemanticError(lastItem, msg); | ||
if (lastItem.range && lastItem.range.end) err.offset = lastItem.range.end - lastItem.range.start; | ||
} | ||
errors.push(err); | ||
} | ||
} | ||
function checkKeyLength(errors, node, itemIdx, key, keyStart) { | ||
@@ -14,0 +63,0 @@ if (!key || typeof keyStart !== 'number') return; |
@@ -275,6 +275,7 @@ "use strict"; | ||
const str = value.replace(/\n+/g, `$&\n${indent}`); // May need to verify that output will be parsed as a string, as plain numbers | ||
// and booleans get parsed with those types, e.g. '42', 'true' & '0.9e-3'. | ||
const str = value.replace(/\n+/g, `$&\n${indent}`); // Verify that output will be parsed as a string, as e.g. plain numbers and | ||
// booleans get parsed with those types in v1.2 (e.g. '42', 'true' & '0.9e-3'), | ||
// and others in v1.1. | ||
if (actualString && /^[\w.+-]+$/.test(str) && typeof tags.resolveScalar(str).value !== 'string') { | ||
if (actualString && typeof tags.resolveScalar(str).value !== 'string') { | ||
return doubleQuotedString(value, ctx); | ||
@@ -281,0 +282,0 @@ } |
module.exports = require('./dist/schema/Map').default | ||
require('./dist/deprecation').warnFileDeprecation(__filename) | ||
require('./dist/warnings').warnFileDeprecation(__filename) |
{ | ||
"name": "yaml", | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"license": "ISC", | ||
@@ -69,21 +69,21 @@ "author": "Eemeli Aro <eemeli@gmail.com>", | ||
"devDependencies": { | ||
"@babel/cli": "^7.4.4", | ||
"@babel/core": "^7.4.5", | ||
"@babel/plugin-proposal-class-properties": "^7.4.4", | ||
"@babel/plugin-transform-runtime": "^7.4.4", | ||
"@babel/preset-env": "^7.4.5", | ||
"babel-eslint": "^10.0.1", | ||
"babel-jest": "^24.8.0", | ||
"@babel/cli": "^7.5.5", | ||
"@babel/core": "^7.5.5", | ||
"@babel/plugin-proposal-class-properties": "^7.5.5", | ||
"@babel/plugin-transform-runtime": "^7.5.5", | ||
"@babel/preset-env": "^7.5.5", | ||
"babel-eslint": "^10.0.3", | ||
"babel-jest": "^24.9.0", | ||
"babel-plugin-trace": "^1.1.0", | ||
"common-tags": "^1.8.0", | ||
"cpy-cli": "^2.0.0", | ||
"eslint": "^5.16.0", | ||
"eslint-config-prettier": "^4.3.0", | ||
"eslint": "^6.2.2", | ||
"eslint-config-prettier": "^6.1.0", | ||
"eslint-plugin-prettier": "^3.1.0", | ||
"fast-check": "^1.15.1", | ||
"jest": "^24.8.0", | ||
"prettier": "^1.17.1" | ||
"fast-check": "^1.16.2", | ||
"jest": "^24.9.0", | ||
"prettier": "^1.18.2" | ||
}, | ||
"dependencies": { | ||
"@babel/runtime": "^7.4.5" | ||
"@babel/runtime": "^7.5.5" | ||
}, | ||
@@ -90,0 +90,0 @@ "engines": { |
module.exports = require('./dist/schema/Pair').default | ||
require('./dist/deprecation').warnFileDeprecation(__filename) | ||
require('./dist/warnings').warnFileDeprecation(__filename) |
module.exports = require('./dist/schema/Scalar').default | ||
require('./dist/deprecation').warnFileDeprecation(__filename) | ||
require('./dist/warnings').warnFileDeprecation(__filename) |
@@ -7,2 +7,2 @@ module.exports = require('./dist/schema').default | ||
require('./dist/deprecation').warnFileDeprecation(__filename) | ||
require('./dist/warnings').warnFileDeprecation(__filename) |
module.exports = require('./dist/schema/Seq').default | ||
require('./dist/deprecation').warnFileDeprecation(__filename) | ||
require('./dist/warnings').warnFileDeprecation(__filename) |
@@ -7,2 +7,2 @@ 'use strict' | ||
require('../dist/deprecation').warnFileDeprecation(__filename) | ||
require('../dist/warnings').warnFileDeprecation(__filename) |
module.exports = require('../dist/tags/yaml-1.1/omap').default | ||
require('../dist/deprecation').warnFileDeprecation(__filename) | ||
require('../dist/warnings').warnFileDeprecation(__filename) |
module.exports = require('../dist/tags/yaml-1.1/pairs').default | ||
require('../dist/deprecation').warnFileDeprecation(__filename) | ||
require('../dist/warnings').warnFileDeprecation(__filename) |
module.exports = require('../dist/tags/yaml-1.1/set').default | ||
require('../dist/deprecation').warnFileDeprecation(__filename) | ||
require('../dist/warnings').warnFileDeprecation(__filename) |
@@ -10,2 +10,2 @@ 'use strict' | ||
require('../dist/deprecation').warnFileDeprecation(__filename) | ||
require('../dist/warnings').warnFileDeprecation(__filename) |
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
482017
145
13282
Updated@babel/runtime@^7.5.5