@stoplight/yaml
Advanced tools
Comparing version 3.3.0 to 3.3.1
@@ -1,2 +0,2 @@ | ||
import { YAMLNode } from '@stoplight/yaml-ast-parser'; | ||
import { YAMLNode } from './types'; | ||
export declare function buildJsonPath(node: YAMLNode): import("@stoplight/types").Segment[]; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const yaml_ast_parser_1 = require("@stoplight/yaml-ast-parser"); | ||
const types_1 = require("./types"); | ||
const utils_1 = require("./utils"); | ||
@@ -10,6 +10,6 @@ function buildJsonPath(node) { | ||
switch (node.kind) { | ||
case yaml_ast_parser_1.Kind.SCALAR: | ||
case types_1.Kind.SCALAR: | ||
path.unshift(node.value); | ||
break; | ||
case yaml_ast_parser_1.Kind.MAPPING: | ||
case types_1.Kind.MAPPING: | ||
if (prevNode !== node.key) { | ||
@@ -24,6 +24,6 @@ if (path.length > 0 && utils_1.isObject(node.value) && node.value.value === path[0]) { | ||
break; | ||
case yaml_ast_parser_1.Kind.SEQ: | ||
case types_1.Kind.SEQ: | ||
if (prevNode) { | ||
const index = node.items.indexOf(prevNode); | ||
if (prevNode.kind === yaml_ast_parser_1.Kind.SCALAR) { | ||
if (prevNode.kind === types_1.Kind.SCALAR) { | ||
path[0] = index; | ||
@@ -30,0 +30,0 @@ } |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const yaml_ast_parser_1 = require("@stoplight/yaml-ast-parser"); | ||
const buildJsonPath_1 = require("./buildJsonPath"); | ||
const types_1 = require("./types"); | ||
const utils_1 = require("./utils"); | ||
@@ -12,3 +12,3 @@ exports.getJsonPathForPosition = ({ ast, lineMap }, { line, character }) => { | ||
const node = findClosestScalar(ast, Math.min(lineMap[line] - 1, startOffset + character), line, lineMap); | ||
if (!node) | ||
if (!utils_1.isObject(node)) | ||
return; | ||
@@ -22,3 +22,3 @@ const path = buildJsonPath_1.buildJsonPath(node); | ||
switch (node.kind) { | ||
case yaml_ast_parser_1.Kind.MAP: | ||
case types_1.Kind.MAP: | ||
if (node.mappings.length !== 0) { | ||
@@ -32,3 +32,3 @@ for (const mapping of node.mappings) { | ||
break; | ||
case yaml_ast_parser_1.Kind.MAPPING: | ||
case types_1.Kind.MAPPING: | ||
if (utils_1.isObject(node.key)) { | ||
@@ -41,3 +41,3 @@ yield node.key; | ||
break; | ||
case yaml_ast_parser_1.Kind.SEQ: | ||
case types_1.Kind.SEQ: | ||
if (node.items.length !== 0) { | ||
@@ -51,3 +51,3 @@ for (const item of node.items) { | ||
break; | ||
case yaml_ast_parser_1.Kind.SCALAR: | ||
case types_1.Kind.SCALAR: | ||
yield node; | ||
@@ -61,5 +61,5 @@ break; | ||
switch (node.kind) { | ||
case yaml_ast_parser_1.Kind.MAPPING: | ||
case types_1.Kind.MAPPING: | ||
return node.key; | ||
case yaml_ast_parser_1.Kind.MAP: | ||
case types_1.Kind.MAP: | ||
if (node.mappings.length !== 0) { | ||
@@ -73,3 +73,3 @@ for (const mapping of node.mappings) { | ||
break; | ||
case yaml_ast_parser_1.Kind.SEQ: | ||
case types_1.Kind.SEQ: | ||
if (node.items.length !== 0) { | ||
@@ -89,3 +89,3 @@ for (const item of node.items) { | ||
if (node.startPosition <= offset && offset <= node.endPosition) { | ||
return node.kind === yaml_ast_parser_1.Kind.SCALAR ? node : findClosestScalar(node, offset, line, lineMap); | ||
return node.kind === types_1.Kind.SCALAR ? node : findClosestScalar(node, offset, line, lineMap); | ||
} | ||
@@ -97,3 +97,3 @@ } | ||
if (container.startPosition < lineMap[line - 1] && offset <= container.endPosition) { | ||
if (container.kind !== yaml_ast_parser_1.Kind.MAPPING) { | ||
if (container.kind !== types_1.Kind.MAPPING) { | ||
return getFirstScalarChild(container, line, lineMap); | ||
@@ -100,0 +100,0 @@ } |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const yaml_ast_parser_1 = require("@stoplight/yaml-ast-parser"); | ||
const lineForPosition_1 = require("./lineForPosition"); | ||
const types_1 = require("./types"); | ||
const utils_1 = require("./utils"); | ||
@@ -16,7 +16,7 @@ exports.getLocationForJsonPath = ({ ast, lineMap, metadata }, path, closest = false) => { | ||
function getStartPosition(node, offset) { | ||
if (node.parent && node.parent.kind === yaml_ast_parser_1.Kind.MAPPING) { | ||
if (node.parent && node.parent.kind === types_1.Kind.MAPPING) { | ||
if (node.parent.value === null) { | ||
return node.parent.endPosition; | ||
} | ||
if (node.kind !== yaml_ast_parser_1.Kind.SCALAR) { | ||
if (node.kind !== types_1.Kind.SCALAR) { | ||
return node.parent.key.endPosition + 1; | ||
@@ -32,3 +32,3 @@ } | ||
switch (node.kind) { | ||
case yaml_ast_parser_1.Kind.SEQ: | ||
case types_1.Kind.SEQ: | ||
const { items } = node; | ||
@@ -39,3 +39,3 @@ if (items.length !== 0 && items[items.length - 1] !== null) { | ||
break; | ||
case yaml_ast_parser_1.Kind.MAPPING: | ||
case types_1.Kind.MAPPING: | ||
if (node.value !== null) { | ||
@@ -45,3 +45,3 @@ return getEndPosition(node.value); | ||
break; | ||
case yaml_ast_parser_1.Kind.MAP: | ||
case types_1.Kind.MAP: | ||
if (node.value !== null && node.mappings.length !== 0) { | ||
@@ -51,4 +51,4 @@ return getEndPosition(node.mappings[node.mappings.length - 1]); | ||
break; | ||
case yaml_ast_parser_1.Kind.SCALAR: | ||
if (node.parent !== null && node.parent.kind === yaml_ast_parser_1.Kind.MAPPING && node.parent.value === null) { | ||
case types_1.Kind.SCALAR: | ||
if (node.parent !== null && node.parent.kind === types_1.Kind.MAPPING && node.parent.value === null) { | ||
return node.parent.endPosition; | ||
@@ -62,4 +62,7 @@ } | ||
pathLoop: for (const segment of path) { | ||
switch (node && node.kind) { | ||
case yaml_ast_parser_1.Kind.MAP: | ||
if (!utils_1.isObject(node)) { | ||
return closest ? node : void 0; | ||
} | ||
switch (node.kind) { | ||
case types_1.Kind.MAP: | ||
const mappings = getMappings(node.mappings, mergeKeys); | ||
@@ -79,3 +82,3 @@ for (let i = mappings.length - 1; i >= 0; i--) { | ||
return closest ? node : void 0; | ||
case yaml_ast_parser_1.Kind.SEQ: | ||
case types_1.Kind.SEQ: | ||
for (let i = 0; i < node.items.length; i++) { | ||
@@ -110,4 +113,6 @@ if (i === Number(segment)) { | ||
function reduceMergeKeys(node) { | ||
if (!utils_1.isObject(node)) | ||
return []; | ||
switch (node.kind) { | ||
case yaml_ast_parser_1.Kind.SEQ: | ||
case types_1.Kind.SEQ: | ||
return node.items.reduceRight((items, item) => { | ||
@@ -117,5 +122,5 @@ items.push(...reduceMergeKeys(item)); | ||
}, []); | ||
case yaml_ast_parser_1.Kind.MAP: | ||
case types_1.Kind.MAP: | ||
return node.mappings; | ||
case yaml_ast_parser_1.Kind.ANCHOR_REF: | ||
case types_1.Kind.ANCHOR_REF: | ||
return reduceMergeKeys(node.value); | ||
@@ -122,0 +127,0 @@ default: |
@@ -11,2 +11,3 @@ "use strict"; | ||
tslib_1.__exportStar(require("./safeStringify"), exports); | ||
tslib_1.__exportStar(require("./types"), exports); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@stoplight/yaml", | ||
"version": "3.3.0", | ||
"version": "3.3.1", | ||
"description": "Useful functions when working with YAML.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -1,4 +0,3 @@ | ||
import { YAMLNode } from '@stoplight/yaml-ast-parser'; | ||
import { IParseOptions } from './types'; | ||
import { IParseOptions, Kind, YAMLAnchorReference, YAMLMapping, YAMLNode, YAMLScalar } 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; | ||
export declare const walkAST: (node: YAMLAnchorReference | import("./types").YAMLBaseNode<Kind.INCLUDE_REF> | YAMLScalar | import("./types").YAMLMap | YAMLMapping | import("./types").YAMLSequence | null, options?: IParseOptions | undefined, duplicatedMappingKeys?: YAMLNode[] | undefined) => unknown; |
@@ -7,2 +7,4 @@ "use strict"; | ||
const lineForPosition_1 = require("./lineForPosition"); | ||
const types_2 = require("./types"); | ||
const utils_1 = require("./utils"); | ||
exports.parseWithPointers = (value, options) => { | ||
@@ -39,3 +41,3 @@ const lineMap = computeLineMap(value); | ||
switch (node.kind) { | ||
case yaml_ast_parser_1.Kind.MAP: { | ||
case types_2.Kind.MAP: { | ||
const container = {}; | ||
@@ -69,11 +71,12 @@ const seenKeys = []; | ||
} | ||
case yaml_ast_parser_1.Kind.SEQ: | ||
case types_2.Kind.SEQ: | ||
return node.items.map(item => exports.walkAST(item, options, duplicatedMappingKeys)); | ||
case yaml_ast_parser_1.Kind.SCALAR: | ||
case types_2.Kind.SCALAR: | ||
return getScalarValue(node); | ||
case yaml_ast_parser_1.Kind.ANCHOR_REF: | ||
if (node.value !== void 0 && isCircularAnchorRef(node)) { | ||
case types_2.Kind.ANCHOR_REF: { | ||
if (utils_1.isObject(node.value) && isCircularAnchorRef(node)) { | ||
node.value = dereferenceAnchor(node.value, node.referencesAnchor); | ||
} | ||
return exports.walkAST(node.value, options, duplicatedMappingKeys); | ||
return node.value && exports.walkAST(node.value, options, duplicatedMappingKeys); | ||
} | ||
default: | ||
@@ -96,18 +99,18 @@ return null; | ||
const dereferenceAnchor = (node, anchorId) => { | ||
if (!node) | ||
if (!utils_1.isObject(node)) | ||
return node; | ||
if ('referencesAnchor' in node && node.referencesAnchor === anchorId) | ||
return; | ||
if (node.kind === types_2.Kind.ANCHOR_REF && node.referencesAnchor === anchorId) | ||
return null; | ||
switch (node.kind) { | ||
case yaml_ast_parser_1.Kind.MAP: | ||
case types_2.Kind.MAP: | ||
return Object.assign({}, node, { mappings: node.mappings.map(mapping => dereferenceAnchor(mapping, anchorId)) }); | ||
case yaml_ast_parser_1.Kind.SEQ: | ||
case types_2.Kind.SEQ: | ||
return Object.assign({}, node, { items: node.items.map(item => dereferenceAnchor(item, anchorId)) }); | ||
case yaml_ast_parser_1.Kind.MAPPING: | ||
case types_2.Kind.MAPPING: | ||
return Object.assign({}, node, { value: dereferenceAnchor(node.value, anchorId) }); | ||
case yaml_ast_parser_1.Kind.SCALAR: | ||
case types_2.Kind.SCALAR: | ||
return node; | ||
case yaml_ast_parser_1.Kind.ANCHOR_REF: | ||
if (node.value !== undefined && isCircularAnchorRef(node)) { | ||
return; | ||
case types_2.Kind.ANCHOR_REF: | ||
if (utils_1.isObject(node.value) && isCircularAnchorRef(node)) { | ||
return null; | ||
} | ||
@@ -121,11 +124,11 @@ return node; | ||
switch (yaml_ast_parser_1.determineScalarType(node)) { | ||
case yaml_ast_parser_1.ScalarType.null: | ||
case types_2.ScalarType.null: | ||
return null; | ||
case yaml_ast_parser_1.ScalarType.string: | ||
case types_2.ScalarType.string: | ||
return String(node.value); | ||
case yaml_ast_parser_1.ScalarType.bool: | ||
case types_2.ScalarType.bool: | ||
return yaml_ast_parser_1.parseYamlBoolean(node.value); | ||
case yaml_ast_parser_1.ScalarType.int: | ||
case types_2.ScalarType.int: | ||
return yaml_ast_parser_1.parseYamlInteger(node.value); | ||
case yaml_ast_parser_1.ScalarType.float: | ||
case types_2.ScalarType.float: | ||
return yaml_ast_parser_1.parseYamlFloat(node.value); | ||
@@ -132,0 +135,0 @@ } |
@@ -1,7 +0,40 @@ | ||
import { IParserResult } from '@stoplight/types'; | ||
import { LoadOptions, YAMLNode } from '@stoplight/yaml-ast-parser'; | ||
export interface IParseOptions extends LoadOptions { | ||
import { IParserResult, Optional } from '@stoplight/types'; | ||
import * as YAMLAstParser from '@stoplight/yaml-ast-parser'; | ||
import { Kind, ScalarType } from '@stoplight/yaml-ast-parser'; | ||
export interface IParseOptions extends YAMLAstParser.LoadOptions { | ||
json?: boolean; | ||
mergeKeys?: boolean; | ||
} | ||
export declare type YAMLBaseNode<K extends Kind> = Omit<YAMLAstParser.YAMLNode, 'kind' | 'parent'> & { | ||
kind: K; | ||
parent: YAMLNode; | ||
}; | ||
export declare type YAMLAnchorReference = Omit<YAMLAstParser.YAMLAnchorReference, 'kind' | 'value' | 'parent'> & { | ||
kind: Kind.ANCHOR_REF; | ||
value: Optional<YAMLNode>; | ||
parent: YAMLNode; | ||
}; | ||
export declare type YAMLIncludeReference = YAMLBaseNode<Kind.INCLUDE_REF>; | ||
export declare type YAMLScalar = Omit<YAMLAstParser.YAMLScalar, 'kind' | 'parent'> & { | ||
kind: Kind.SCALAR; | ||
parent: YAMLNode; | ||
}; | ||
export declare type YAMLMap = Omit<YAMLAstParser.YamlMap, 'kind' | 'mappings' | 'parent'> & { | ||
kind: Kind.MAP; | ||
mappings: YAMLMapping[]; | ||
parent: YAMLNode; | ||
}; | ||
export declare type YAMLMapping = Omit<YAMLAstParser.YAMLMapping, 'kind' | 'key' | 'value' | 'parent'> & { | ||
kind: Kind.MAPPING; | ||
key: YAMLScalar; | ||
value: YAMLNode | null; | ||
parent: YAMLNode; | ||
}; | ||
export declare type YAMLSequence = Omit<YAMLAstParser.YAMLSequence, 'kind' | 'items' | 'parent'> & { | ||
kind: Kind.SEQ; | ||
items: YAMLNode[]; | ||
parent: YAMLNode; | ||
}; | ||
export declare type YAMLNode = YAMLAnchorReference | YAMLIncludeReference | YAMLScalar | YAMLMap | YAMLMapping | YAMLSequence; | ||
export declare type YamlParserResult<T> = IParserResult<T, YAMLNode, number[], IParseOptions>; | ||
export { Kind, ScalarType }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const yaml_ast_parser_1 = require("@stoplight/yaml-ast-parser"); | ||
exports.Kind = yaml_ast_parser_1.Kind; | ||
exports.ScalarType = yaml_ast_parser_1.ScalarType; | ||
//# sourceMappingURL=types.js.map |
@@ -1,1 +0,1 @@ | ||
export declare const isObject: (sth: unknown) => boolean; | ||
export declare const isObject: (sth: unknown) => sth is object; |
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
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
56754
607