@stoplight/yaml
Advanced tools
Comparing version 2.6.1 to 2.7.0
@@ -15,3 +15,3 @@ "use strict"; | ||
const nextLinePos = lines[Math.min(target + 1, lines.length)]; | ||
if (pos === lines[target]) { | ||
if (pos === lines[target] - 1) { | ||
return target; | ||
@@ -18,0 +18,0 @@ } |
{ | ||
"name": "@stoplight/yaml", | ||
"version": "2.6.1", | ||
"version": "2.7.0", | ||
"description": "Useful functions when working with YAML.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -1,3 +0,3 @@ | ||
import { YAMLNode } from 'yaml-ast-parser'; | ||
export declare const parseWithPointers: <T>(value: string) => import("@stoplight/types").IParserResult<T, YAMLNode, number[]>; | ||
export declare const walkAST: (node: YAMLNode | null) => unknown; | ||
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; |
@@ -5,5 +5,6 @@ "use strict"; | ||
const yaml_ast_parser_1 = require("yaml-ast-parser"); | ||
exports.parseWithPointers = (value) => { | ||
const lineForPosition_1 = require("./lineForPosition"); | ||
exports.parseWithPointers = (value, options) => { | ||
const lineMap = computeLineMap(value); | ||
const ast = yaml_ast_parser_1.load(value); | ||
const ast = yaml_ast_parser_1.load(value, Object.assign({}, options, { ignoreDuplicateKeys: true })); | ||
const parsed = { | ||
@@ -17,9 +18,16 @@ ast, | ||
return parsed; | ||
parsed.data = exports.walkAST(ast); | ||
const duplicatedMappingKeys = []; | ||
parsed.data = exports.walkAST(ast, options !== undefined && options.ignoreDuplicateKeys === false ? duplicatedMappingKeys : undefined); | ||
if (duplicatedMappingKeys.length > 0) { | ||
parsed.diagnostics.push(...transformDuplicatedMappingKeys(duplicatedMappingKeys, lineMap)); | ||
} | ||
if (ast.errors) { | ||
parsed.diagnostics = transformErrors(ast.errors, lineMap); | ||
parsed.diagnostics.push(...transformErrors(ast.errors, lineMap)); | ||
} | ||
if (parsed.diagnostics.length > 0) { | ||
parsed.diagnostics.sort((itemA, itemB) => itemA.range.start.line - itemB.range.start.line); | ||
} | ||
return parsed; | ||
}; | ||
exports.walkAST = (node) => { | ||
exports.walkAST = (node, duplicatedMappingKeys) => { | ||
if (node) { | ||
@@ -30,3 +38,6 @@ switch (node.kind) { | ||
for (const mapping of node.mappings) { | ||
container[mapping.key.value] = exports.walkAST(mapping.value); | ||
if (duplicatedMappingKeys !== undefined && mapping.key.value in container) { | ||
duplicatedMappingKeys.push(mapping.key); | ||
} | ||
container[mapping.key.value] = exports.walkAST(mapping.value, duplicatedMappingKeys); | ||
} | ||
@@ -36,3 +47,3 @@ return container; | ||
case yaml_ast_parser_1.Kind.SEQ: | ||
return node.items.map(item => exports.walkAST(item)); | ||
return node.items.map(item => exports.walkAST(item, duplicatedMappingKeys)); | ||
case yaml_ast_parser_1.Kind.SCALAR: | ||
@@ -44,3 +55,3 @@ return 'valueObject' in node ? node.valueObject : node.value; | ||
} | ||
return exports.walkAST(node.value); | ||
return exports.walkAST(node.value, duplicatedMappingKeys); | ||
default: | ||
@@ -123,2 +134,25 @@ return null; | ||
}; | ||
const transformDuplicatedMappingKeys = (nodes, lineMap) => { | ||
const validations = []; | ||
for (const node of nodes) { | ||
const startLine = lineForPosition_1.lineForPosition(node.startPosition, lineMap); | ||
const endLine = lineForPosition_1.lineForPosition(node.endPosition, lineMap); | ||
validations.push({ | ||
code: 'YAMLException', | ||
message: 'duplicate key', | ||
range: { | ||
start: { | ||
line: startLine, | ||
character: startLine === 0 ? node.startPosition : node.startPosition - lineMap[startLine - 1], | ||
}, | ||
end: { | ||
line: endLine, | ||
character: endLine === 0 ? node.endPosition : node.endPosition - lineMap[endLine - 1], | ||
}, | ||
}, | ||
severity: types_1.DiagnosticSeverity.Error, | ||
}); | ||
} | ||
return validations; | ||
}; | ||
//# sourceMappingURL=parseWithPointers.js.map |
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
47091
458