@stoplight/yaml
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -8,29 +8,43 @@ "use strict"; | ||
} | ||
const startOffset = line === 0 ? 0 : lineMap[line - 1]; | ||
const node = findClosestScalar(ast, startOffset, lineMap[line]); | ||
const startOffset = line === 0 ? 0 : lineMap[line - 1] + 1; | ||
const node = findClosestScalar(ast, Math.min(lineMap[line] - 1, startOffset + character), line, lineMap); | ||
if (!node) | ||
return; | ||
return buildJsonPath(node); | ||
const path = buildJsonPath(node); | ||
if (path.length === 0) | ||
return; | ||
return path; | ||
}; | ||
const isValidNode = (node) => node !== null && node !== undefined; | ||
function* walk(node) { | ||
switch (node.kind) { | ||
case yaml_ast_parser_1.Kind.MAP: | ||
for (const mapping of node.mappings) { | ||
yield* walk(mapping); | ||
if (node.mappings.length !== 0) { | ||
for (const mapping of node.mappings) { | ||
if (isValidNode(mapping)) { | ||
yield mapping; | ||
yield* walk(mapping); | ||
} | ||
} | ||
} | ||
break; | ||
case yaml_ast_parser_1.Kind.MAPPING: | ||
yield node.key; | ||
if (node.value !== null) { | ||
if (isValidNode(node.key)) { | ||
yield node.key; | ||
} | ||
if (isValidNode(node.value)) { | ||
yield node.value; | ||
if (node.value.kind === yaml_ast_parser_1.Kind.MAP || node.value.kind === yaml_ast_parser_1.Kind.SEQ) { | ||
yield* walk(node.value); | ||
} | ||
else { | ||
yield node.value; | ||
} | ||
} | ||
break; | ||
case yaml_ast_parser_1.Kind.SEQ: | ||
for (const item of node.items) { | ||
yield* walk(item); | ||
if (node.items.length !== 0) { | ||
for (const item of node.items) { | ||
if (isValidNode(item)) { | ||
yield item; | ||
yield* walk(item); | ||
} | ||
} | ||
} | ||
@@ -41,15 +55,49 @@ break; | ||
break; | ||
case yaml_ast_parser_1.Kind.ANCHOR_REF: | ||
} | ||
} | ||
function getFirstScalarChild(node, line, lineMap) { | ||
const startOffset = lineMap[line - 1] + 1; | ||
const endOffset = lineMap[line]; | ||
switch (node.kind) { | ||
case yaml_ast_parser_1.Kind.MAPPING: | ||
return node.key; | ||
case yaml_ast_parser_1.Kind.MAP: | ||
if (node.mappings.length !== 0) { | ||
for (const mapping of node.mappings) { | ||
if (mapping.startPosition > startOffset && mapping.startPosition <= endOffset) { | ||
return getFirstScalarChild(mapping, line, lineMap); | ||
} | ||
} | ||
} | ||
break; | ||
case yaml_ast_parser_1.Kind.SEQ: | ||
if (node.items.length !== 0) { | ||
for (const item of node.items) { | ||
if (item.startPosition > startOffset && item.startPosition <= endOffset) { | ||
return getFirstScalarChild(item, line, lineMap); | ||
} | ||
} | ||
} | ||
break; | ||
} | ||
return node; | ||
} | ||
function findClosestScalar(ast, offset, endOffset) { | ||
for (const node of walk(ast)) { | ||
switch (node.kind) { | ||
case yaml_ast_parser_1.Kind.SCALAR: | ||
if (offset <= node.startPosition && endOffset >= node.endPosition) { | ||
return node; | ||
} | ||
function findClosestScalar(container, offset, line, lineMap) { | ||
for (const node of walk(container)) { | ||
if (node.startPosition <= offset && offset <= node.endPosition) { | ||
return node.kind === yaml_ast_parser_1.Kind.SCALAR ? node : findClosestScalar(node, offset, line, lineMap); | ||
} | ||
} | ||
if (lineMap[line - 1] === lineMap[line] - 1) { | ||
return container; | ||
} | ||
if (container.startPosition < lineMap[line - 1] && offset <= container.endPosition) { | ||
if (container.kind !== yaml_ast_parser_1.Kind.MAPPING) { | ||
return getFirstScalarChild(container, line, lineMap); | ||
} | ||
if (container.value && container.key.endPosition < offset) { | ||
return getFirstScalarChild(container.value, line, lineMap); | ||
} | ||
} | ||
return container; | ||
} | ||
@@ -66,3 +114,10 @@ function buildJsonPath(node) { | ||
if (prevNode !== node.key) { | ||
path.unshift(node.key.value); | ||
if (path.length > 0 && | ||
isValidNode(node.value) && | ||
node.value.value === path[0]) { | ||
path[0] = node.key.value; | ||
} | ||
else { | ||
path.unshift(node.key.value); | ||
} | ||
} | ||
@@ -76,3 +131,3 @@ break; | ||
} | ||
else { | ||
else if (index !== -1) { | ||
path.unshift(index); | ||
@@ -79,0 +134,0 @@ } |
{ | ||
"name": "@stoplight/yaml", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Useful functions when working with YAML.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -29,2 +29,4 @@ "use strict"; | ||
const node = nodes[index]; | ||
if (node === null) | ||
continue; | ||
const key = node.key ? node.key.value : index; | ||
@@ -31,0 +33,0 @@ const mappings = get(node, 'mappings', get(node, 'value.mappings')); |
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
38870
379
0