@stoplight/yaml
Advanced tools
Comparing version 2.1.2 to 2.2.0
{ | ||
"name": "@stoplight/yaml", | ||
"version": "2.1.2", | ||
"version": "2.2.0", | ||
"description": "Useful functions when working with YAML.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -5,3 +5,2 @@ "use strict"; | ||
const yaml_ast_parser_1 = require("yaml-ast-parser"); | ||
const get = require("lodash/get"); | ||
exports.parseWithPointers = (value) => { | ||
@@ -18,3 +17,3 @@ const lineMap = computeLineMap(value); | ||
return parsed; | ||
walk(parsed.data, ast.mappings, lineMap); | ||
parsed.data = walk(ast); | ||
if (ast.errors) { | ||
@@ -25,43 +24,60 @@ parsed.diagnostics = transformErrors(ast.errors, lineMap); | ||
}; | ||
const walk = (container, nodes, lineMap) => { | ||
for (const i in nodes) { | ||
if (!nodes.hasOwnProperty(i)) | ||
continue; | ||
const index = parseInt(i); | ||
const node = nodes[index]; | ||
if (node === null) | ||
continue; | ||
const key = node.key ? node.key.value : index; | ||
const mappings = get(node, 'mappings', get(node, 'value.mappings')); | ||
if (mappings) { | ||
container[key] = walk({}, mappings, lineMap); | ||
continue; | ||
} | ||
const items = get(node, 'items', get(node, 'value.items')); | ||
if (items) { | ||
container[key] = walk([], items, lineMap); | ||
continue; | ||
} | ||
if (node) { | ||
if (node.hasOwnProperty('valueObject')) { | ||
container[key] = node.valueObject; | ||
const walk = (node) => { | ||
if (node) { | ||
switch (node.kind) { | ||
case yaml_ast_parser_1.Kind.MAP: { | ||
const container = {}; | ||
for (const mapping of node.mappings) { | ||
container[mapping.key.value] = walk(mapping.value); | ||
} | ||
return container; | ||
} | ||
else if (node.hasOwnProperty('value')) { | ||
if (node.value && node.value.hasOwnProperty('valueObject')) { | ||
container[key] = node.value.valueObject; | ||
case yaml_ast_parser_1.Kind.SEQ: | ||
return node.items.map(item => walk(item)); | ||
case yaml_ast_parser_1.Kind.SCALAR: | ||
return 'valueObject' in node ? node.valueObject : node.value; | ||
case yaml_ast_parser_1.Kind.ANCHOR_REF: | ||
if (node.value !== undefined && isCircularAnchorRef(node)) { | ||
node.value = dereferenceAnchor(node.value, node.referencesAnchor); | ||
} | ||
else if (node.value && node.value.hasOwnProperty('value')) { | ||
container[key] = node.value.value; | ||
} | ||
else { | ||
container[key] = node.value; | ||
} | ||
} | ||
return walk(node.value); | ||
default: | ||
return null; | ||
} | ||
else { | ||
container[key] = node; | ||
} | ||
return node; | ||
}; | ||
const isCircularAnchorRef = (anchorRef) => { | ||
const { referencesAnchor } = anchorRef; | ||
let node = anchorRef; | ||
while ((node = node.parent)) { | ||
if ('anchorId' in node && node.anchorId === referencesAnchor) { | ||
return true; | ||
} | ||
} | ||
return container; | ||
return false; | ||
}; | ||
const dereferenceAnchor = (node, anchorId) => { | ||
if (!node) | ||
return node; | ||
if ('referencesAnchor' in node && node.referencesAnchor === anchorId) | ||
return; | ||
switch (node.kind) { | ||
case yaml_ast_parser_1.Kind.MAP: | ||
return Object.assign({}, node, { mappings: node.mappings.map(mapping => dereferenceAnchor(mapping, anchorId)) }); | ||
case yaml_ast_parser_1.Kind.SEQ: | ||
return Object.assign({}, node, { items: node.items.map(item => dereferenceAnchor(item, anchorId)) }); | ||
case yaml_ast_parser_1.Kind.MAPPING: | ||
return Object.assign({}, node, { value: dereferenceAnchor(node.value, anchorId) }); | ||
case yaml_ast_parser_1.Kind.SCALAR: | ||
return node; | ||
case yaml_ast_parser_1.Kind.ANCHOR_REF: | ||
if (node.value !== undefined && isCircularAnchorRef(node)) { | ||
return; | ||
} | ||
return node; | ||
default: | ||
return node; | ||
} | ||
}; | ||
const computeLineMap = (input) => { | ||
@@ -68,0 +84,0 @@ const lines = input.split(/\n/); |
Sorry, the diff of this file is not supported yet
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
40883
396