@stoplight/yaml
Advanced tools
Comparing version 2.8.0 to 3.0.0
@@ -15,5 +15,3 @@ "use strict"; | ||
if (prevNode !== node.key) { | ||
if (path.length > 0 && | ||
utils_1.isValidNode(node.value) && | ||
node.value.value === path[0]) { | ||
if (path.length > 0 && utils_1.isObject(node.value) && node.value.value === path[0]) { | ||
path[0] = node.key.value; | ||
@@ -20,0 +18,0 @@ } |
import { GetJsonPathForPosition } from '@stoplight/types'; | ||
import { YAMLNode } from 'yaml-ast-parser'; | ||
export declare const getJsonPathForPosition: GetJsonPathForPosition<YAMLNode, number[]>; | ||
import { YamlParserResult } from './types'; | ||
export declare const getJsonPathForPosition: GetJsonPathForPosition<YamlParserResult<unknown>>; |
@@ -24,3 +24,3 @@ "use strict"; | ||
for (const mapping of node.mappings) { | ||
if (utils_1.isValidNode(mapping)) { | ||
if (utils_1.isObject(mapping)) { | ||
yield mapping; | ||
@@ -32,6 +32,6 @@ } | ||
case yaml_ast_parser_1.Kind.MAPPING: | ||
if (utils_1.isValidNode(node.key)) { | ||
if (utils_1.isObject(node.key)) { | ||
yield node.key; | ||
} | ||
if (utils_1.isValidNode(node.value)) { | ||
if (utils_1.isObject(node.value)) { | ||
yield node.value; | ||
@@ -43,3 +43,3 @@ } | ||
for (const item of node.items) { | ||
if (utils_1.isValidNode(item)) { | ||
if (utils_1.isObject(item)) { | ||
yield item; | ||
@@ -46,0 +46,0 @@ } |
import { GetLocationForJsonPath } from '@stoplight/types'; | ||
import { YAMLNode } from 'yaml-ast-parser'; | ||
export declare const getLocationForJsonPath: GetLocationForJsonPath<YAMLNode, number[]>; | ||
import { YamlParserResult } from './types'; | ||
export declare const getLocationForJsonPath: GetLocationForJsonPath<YamlParserResult<unknown>>; |
@@ -5,4 +5,5 @@ "use strict"; | ||
const lineForPosition_1 = require("./lineForPosition"); | ||
exports.getLocationForJsonPath = ({ ast, lineMap }, path, closest = false) => { | ||
const node = findNodeAtPath(ast, path, closest); | ||
const utils_1 = require("./utils"); | ||
exports.getLocationForJsonPath = ({ ast, lineMap, metadata }, path, closest = false) => { | ||
const node = findNodeAtPath(ast, path, { closest, mergeKeys: metadata !== undefined && metadata.mergeKeys === true }); | ||
if (node === void 0) | ||
@@ -55,7 +56,9 @@ return; | ||
} | ||
function findNodeAtPath(node, path, closest) { | ||
function findNodeAtPath(node, path, { closest, mergeKeys }) { | ||
pathLoop: for (const segment of path) { | ||
switch (node && node.kind) { | ||
case yaml_ast_parser_1.Kind.MAP: | ||
for (const item of node.mappings) { | ||
const mappings = getMappings(node.mappings, mergeKeys); | ||
for (let i = mappings.length - 1; i >= 0; i--) { | ||
const item = mappings[i]; | ||
if (item.key.value === segment) { | ||
@@ -86,2 +89,32 @@ if (item.value === null) { | ||
} | ||
function getMappings(mappings, mergeKeys) { | ||
if (!mergeKeys) | ||
return mappings; | ||
return mappings.reduce((mergedMappings, mapping) => { | ||
if (utils_1.isObject(mapping)) { | ||
if (mapping.key.value === "<<") { | ||
mergedMappings.push(...reduceMergeKeys(mapping.value)); | ||
} | ||
else { | ||
mergedMappings.push(mapping); | ||
} | ||
} | ||
return mergedMappings; | ||
}, []); | ||
} | ||
function reduceMergeKeys(node) { | ||
switch (node.kind) { | ||
case yaml_ast_parser_1.Kind.SEQ: | ||
return node.items.reduceRight((items, item) => { | ||
items.push(...reduceMergeKeys(item)); | ||
return items; | ||
}, []); | ||
case yaml_ast_parser_1.Kind.MAP: | ||
return node.mappings; | ||
case yaml_ast_parser_1.Kind.ANCHOR_REF: | ||
return reduceMergeKeys(node.value); | ||
default: | ||
return []; | ||
} | ||
} | ||
const getLoc = (lineMap, { start = 0, end = 0 }) => { | ||
@@ -88,0 +121,0 @@ const startLine = lineForPosition_1.lineForPosition(start, lineMap); |
@@ -0,3 +1,5 @@ | ||
export * from './buildJsonPath'; | ||
export * from './getJsonPathForPosition'; | ||
export * from './getLocationForJsonPath'; | ||
export * from './lineForPosition'; | ||
export * from './parse'; | ||
@@ -4,0 +6,0 @@ export * from './parseWithPointers'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
tslib_1.__exportStar(require("./buildJsonPath"), exports); | ||
tslib_1.__exportStar(require("./getJsonPathForPosition"), exports); | ||
tslib_1.__exportStar(require("./getLocationForJsonPath"), exports); | ||
tslib_1.__exportStar(require("./lineForPosition"), exports); | ||
tslib_1.__exportStar(require("./parse"), exports); | ||
@@ -7,0 +9,0 @@ tslib_1.__exportStar(require("./parseWithPointers"), exports); |
{ | ||
"name": "@stoplight/yaml", | ||
"version": "2.8.0", | ||
"version": "3.0.0", | ||
"description": "Useful functions when working with YAML.", | ||
@@ -27,5 +27,5 @@ "keywords": [ | ||
"dependencies": { | ||
"@stoplight/types": "^9.1.2", | ||
"@stoplight/types": "^10.0.1", | ||
"@types/js-yaml": "3.12.1", | ||
"lodash": "^4.17.5", | ||
"lodash": "^4.17.15", | ||
"yaml-ast-parser": "~0.0.43" | ||
@@ -32,0 +32,0 @@ }, |
@@ -1,3 +0,4 @@ | ||
import { LoadOptions, YAMLNode } from 'yaml-ast-parser'; | ||
export declare const parseWithPointers: <T>(value: string, options?: LoadOptions | undefined) => import("@stoplight/types").IParserResult<T, YAMLNode, number[]>; | ||
export declare const walkAST: (node: YAMLNode | null, duplicatedMappingKeys?: YAMLNode[] | undefined) => unknown; | ||
import { YAMLNode } from 'yaml-ast-parser'; | ||
import { IParseOptions } from './types'; | ||
export declare const parseWithPointers: <T>(value: string, options?: IParseOptions | undefined) => import("@stoplight/types").IParserResult<T | undefined, YAMLNode, number[], IParseOptions>; | ||
export declare const walkAST: (node: YAMLNode | null, options?: IParseOptions | undefined, duplicatedMappingKeys?: YAMLNode[] | undefined) => unknown; |
@@ -13,4 +13,5 @@ "use strict"; | ||
lineMap, | ||
data: {}, | ||
data: undefined, | ||
diagnostics: [], | ||
metadata: options, | ||
}; | ||
@@ -20,3 +21,3 @@ if (!ast) | ||
const duplicatedMappingKeys = []; | ||
parsed.data = exports.walkAST(ast, options !== undefined && options.ignoreDuplicateKeys === false ? duplicatedMappingKeys : undefined); | ||
parsed.data = exports.walkAST(ast, options, options !== undefined && options.ignoreDuplicateKeys === false ? duplicatedMappingKeys : undefined); | ||
if (duplicatedMappingKeys.length > 0) { | ||
@@ -33,3 +34,3 @@ parsed.diagnostics.push(...transformDuplicatedMappingKeys(duplicatedMappingKeys, lineMap)); | ||
}; | ||
exports.walkAST = (node, duplicatedMappingKeys) => { | ||
exports.walkAST = (node, options, duplicatedMappingKeys) => { | ||
if (node) { | ||
@@ -40,6 +41,16 @@ switch (node.kind) { | ||
for (const mapping of node.mappings) { | ||
if (duplicatedMappingKeys !== undefined && mapping.key.value in container) { | ||
duplicatedMappingKeys.push(mapping.key); | ||
if (mapping.key.value in container) { | ||
if (options !== void 0 && options.json === false) { | ||
throw new Error('Duplicate YAML mapping key encountered'); | ||
} | ||
if (duplicatedMappingKeys !== void 0) { | ||
duplicatedMappingKeys.push(mapping.key); | ||
} | ||
} | ||
container[mapping.key.value] = exports.walkAST(mapping.value, duplicatedMappingKeys); | ||
if (options !== void 0 && options.mergeKeys === true && mapping.key.value === "<<") { | ||
Object.assign(container, reduceMergeKeys(exports.walkAST(mapping.value, options, duplicatedMappingKeys))); | ||
} | ||
else { | ||
container[mapping.key.value] = exports.walkAST(mapping.value, options, duplicatedMappingKeys); | ||
} | ||
} | ||
@@ -49,3 +60,3 @@ return container; | ||
case yaml_ast_parser_1.Kind.SEQ: | ||
return node.items.map(item => exports.walkAST(item, duplicatedMappingKeys)); | ||
return node.items.map(item => exports.walkAST(item, options, duplicatedMappingKeys)); | ||
case yaml_ast_parser_1.Kind.SCALAR: | ||
@@ -57,3 +68,3 @@ return 'valueObject' in node ? node.valueObject : node.value; | ||
} | ||
return exports.walkAST(node.value, duplicatedMappingKeys); | ||
return exports.walkAST(node.value, options, duplicatedMappingKeys); | ||
default: | ||
@@ -160,2 +171,8 @@ return null; | ||
}; | ||
const reduceMergeKeys = (items) => { | ||
if (Array.isArray(items)) { | ||
return items.reduceRight((merged, item) => Object.assign(merged, item), {}); | ||
} | ||
return typeof items !== 'object' || items === null ? null : Object(items); | ||
}; | ||
//# sourceMappingURL=parseWithPointers.js.map |
@@ -21,5 +21,5 @@ # @stoplight/yaml | ||
- **[getJsonPathForPosition](https://stoplightio.github.io/yaml/globals.html#getjsonpathforposition)**: Computes JSON path for given position. | ||
- **[getLocationForJsonPath](https://stoplightio.github.io/yaml/globals.html#getlocationforjsonpath)**: Retrieves location of node matching given JSON path. | ||
- **[parseWithPointers](https://stoplightio.github.io/yaml/globals.html#parsewithpointers)**: Parses YAML into JSON and also returns diagnostics as well as full ast with line information. | ||
- **[getJsonPathForPosition](./src/getJsonPathForPosition.ts)**: Computes JSON path for given position. | ||
- **[getLocationForJsonPath](./src/getLocationForJsonPath.ts)**: Retrieves location of node matching given JSON path. | ||
- **[parseWithPointers](./src/parseWithPointers.ts)**: Parses YAML into JSON and also returns diagnostics as well as full ast with line information. | ||
@@ -26,0 +26,0 @@ ```ts |
import { IParserResult } from '@stoplight/types'; | ||
import { YAMLNode } from 'yaml-ast-parser'; | ||
export declare type YamlParserResult<T> = IParserResult<T, YAMLNode, number[]>; | ||
import { LoadOptions, YAMLNode } from 'yaml-ast-parser'; | ||
export interface IParseOptions extends LoadOptions { | ||
json?: boolean; | ||
mergeKeys?: boolean; | ||
} | ||
export declare type YamlParserResult<T> = IParserResult<T, YAMLNode, number[], IParseOptions>; |
@@ -1,2 +0,1 @@ | ||
import { YAMLNode } from 'yaml-ast-parser'; | ||
export declare const isValidNode: (node: YAMLNode) => boolean; | ||
export declare const isObject: (sth: unknown) => boolean; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isValidNode = (node) => node !== null && node !== undefined; | ||
exports.isObject = (sth) => sth !== null && typeof sth === 'object'; | ||
//# sourceMappingURL=utils.js.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
52812
36
535
+ Added@stoplight/types@10.0.1(transitive)
- Removed@stoplight/types@9.1.2(transitive)
Updated@stoplight/types@^10.0.1
Updatedlodash@^4.17.15