UNPKG

@microsoft.azure/openapi-validator-core

Version:
55 lines 1.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.JsonParser = void 0; const jsonc_parser_1 = require("jsonc-parser"); class JsonParser { parse(text) { const errors = []; const rootNode = (0, jsonc_parser_1.parseTree)(text, errors, { disallowComments: true }); if (errors.length || rootNode == undefined) { throw new Error("Parser failed with errors:" + JSON.stringify(errors)); } return { getLocation: (path) => { const targetPath = [...path]; if (targetPath.length === 0) { return getRange(text, rootNode); } while (targetPath.length > 0) { const root = (0, jsonc_parser_1.findNodeAtLocation)(rootNode, targetPath); if (root) { return getRange(text, root); } targetPath.pop(); } throw new Error("Invalid JSONPath:" + path.join(".")); }, getValue: () => { return Object.assign({}, (0, jsonc_parser_1.getNodeValue)(rootNode)); } }; } } exports.JsonParser = JsonParser; function getLocation(text, offset) { let line = 1; let column = 0; for (let i = 0; i < offset; i++) { if (text[i] === "\n") { line++; column = 0; } column++; } return { line, column }; } function getRange(text, node) { return { start: getLocation(text, node.offset), end: getLocation(text, node.offset + node.length) }; } //# sourceMappingURL=jsonParser.js.map