@stoplight/yaml
Advanced tools
Comparing version 1.2.1 to 2.0.0
@@ -0,2 +1,4 @@ | ||
export * from './getJsonPathForPosition'; | ||
export * from './getLocationForJsonPath'; | ||
export * from './parseWithPointers'; | ||
export * from './safeStringify'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
tslib_1.__exportStar(require("./getJsonPathForPosition"), exports); | ||
tslib_1.__exportStar(require("./getLocationForJsonPath"), exports); | ||
tslib_1.__exportStar(require("./parseWithPointers"), exports); | ||
tslib_1.__exportStar(require("./safeStringify"), exports); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@stoplight/yaml", | ||
"version": "1.2.1", | ||
"version": "2.0.0", | ||
"description": "Useful functions when working with YAML.", | ||
@@ -27,3 +27,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@stoplight/types": "3.x.x", | ||
"@stoplight/types": "4.x.x", | ||
"lodash": "4.x.x", | ||
@@ -30,0 +30,0 @@ "yaml-ast-parser": "0.0.43" |
import { IParserResult } from '@stoplight/types'; | ||
export interface IYamlParserOpts { | ||
maxPointerDepth?: number; | ||
} | ||
export declare const parseWithPointers: <T>(value: string, opts?: IYamlParserOpts) => IParserResult<T>; | ||
export declare const lineForPosition: (pos: number, lines: number[], start?: number, end?: number | undefined) => number; | ||
import { YAMLNode } from 'yaml-ast-parser'; | ||
export declare const parseWithPointers: <T>(value: string) => IParserResult<T, YAMLNode, number[]>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const types_1 = require("@stoplight/types"); | ||
const yaml_ast_parser_1 = require("yaml-ast-parser"); | ||
const get = require("lodash/get"); | ||
exports.parseWithPointers = (value, opts = {}) => { | ||
exports.parseWithPointers = (value) => { | ||
const lineMap = computeLineMap(value); | ||
const ast = yaml_ast_parser_1.load(value); | ||
const parsed = { | ||
ast, | ||
lineMap, | ||
data: {}, | ||
pointers: {}, | ||
validations: [], | ||
diagnostics: [], | ||
}; | ||
if (!value || !value.trim().length) | ||
return parsed; | ||
const ast = yaml_ast_parser_1.load(value); | ||
if (!ast) | ||
return parsed; | ||
const lineMap = computeLineMap(value); | ||
parsed.pointers = { | ||
'': getLoc(lineMap, { | ||
start: 0, | ||
end: ast.endPosition, | ||
}), | ||
}; | ||
parsed.data = walk([], {}, ast.mappings, parsed.pointers, lineMap, 1, opts); | ||
walk(parsed.data, ast.mappings, lineMap); | ||
if (ast.errors) { | ||
parsed.validations = transformErrors(ast.errors); | ||
parsed.diagnostics = transformErrors(ast.errors, lineMap); | ||
} | ||
return parsed; | ||
}; | ||
exports.lineForPosition = (pos, lines, start = 0, end) => { | ||
if (pos === 0) { | ||
return 1; | ||
} | ||
if (typeof end === 'undefined') { | ||
end = lines.length; | ||
} | ||
const target = Math.floor((end - start) / 2) + start; | ||
if (pos >= lines[target] && !lines[target + 1]) { | ||
return target + 1; | ||
} | ||
const nextLinePos = lines[Math.min(target + 1, lines.length)]; | ||
if (pos >= lines[target] && pos <= nextLinePos) { | ||
if (pos === nextLinePos) { | ||
return target + 2; | ||
} | ||
return target + 1; | ||
} | ||
if (pos > lines[target]) { | ||
return exports.lineForPosition(pos, lines, target + 1, end); | ||
} | ||
else { | ||
return exports.lineForPosition(pos, lines, start, target - 1); | ||
} | ||
}; | ||
const walk = (path, container, nodes, pointers, lineMap, depth, opts) => { | ||
if (opts.maxPointerDepth && opts.maxPointerDepth < depth) | ||
return container; | ||
const walk = (container, nodes, lineMap) => { | ||
for (const i in nodes) { | ||
@@ -63,10 +30,5 @@ if (!nodes.hasOwnProperty(i)) | ||
const key = node.key ? node.key.value : index; | ||
const nodePath = path.concat(key); | ||
pointers[`/${nodePath.join('/')}`] = getLoc(lineMap, { | ||
start: node.startPosition, | ||
end: node.endPosition, | ||
}); | ||
const mappings = get(node, 'mappings', get(node, 'value.mappings')); | ||
if (mappings) { | ||
container[key] = walk(nodePath, {}, mappings, pointers, lineMap, depth + 1, opts); | ||
container[key] = walk({}, mappings, lineMap); | ||
continue; | ||
@@ -76,3 +38,3 @@ } | ||
if (items) { | ||
container[key] = walk(nodePath, [], items, pointers, lineMap, depth + 1, opts); | ||
container[key] = walk([], items, lineMap); | ||
continue; | ||
@@ -107,28 +69,25 @@ } | ||
for (const line of lines) { | ||
sum += line.length + 1; | ||
lineMap.push(sum); | ||
sum += line.length + 1; | ||
} | ||
return lineMap; | ||
}; | ||
const getLoc = (lineMap, { start = 0, end = 0 }) => { | ||
return { | ||
start: { line: exports.lineForPosition(start, lineMap) }, | ||
end: { line: exports.lineForPosition(end, lineMap) }, | ||
}; | ||
}; | ||
const transformErrors = (errors) => { | ||
const transformErrors = (errors, lineMap) => { | ||
const validations = []; | ||
for (const error of errors) { | ||
const validation = { | ||
ruleId: error.name, | ||
msg: error.reason, | ||
level: error.isWarning ? 40 : 60, | ||
}; | ||
if (error.mark && error.mark.line) { | ||
validation.location = { | ||
code: error.name, | ||
message: error.reason, | ||
severity: error.isWarning ? types_1.DiagnosticSeverity.Warning : types_1.DiagnosticSeverity.Error, | ||
range: { | ||
start: { | ||
line: error.mark.line, | ||
line: error.mark.line - 1, | ||
character: error.mark.column, | ||
}, | ||
}; | ||
} | ||
end: { | ||
line: error.mark.line - 1, | ||
character: error.mark.toLineEnd ? lineMap[error.mark.line - 1] : error.mark.column, | ||
}, | ||
}, | ||
}; | ||
validations.push(validation); | ||
@@ -135,0 +94,0 @@ } |
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
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
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
34329
21
322
1
+ Added@stoplight/types@4.2.0(transitive)
- Removed@stoplight/types@3.1.0(transitive)
Updated@stoplight/types@4.x.x