UNPKG

@typespec/compiler

Version:

TypeSpec Compiler Preview

49 lines 1.6 kB
import { isCollection, isMap, isScalar } from "yaml"; import { findPair } from "yaml/util"; export function getLocationInYamlScript(file, path, kind = "value") { const node = findYamlNode(file, path, kind); return { file: file.file, pos: node?.range?.[0] ?? 0, end: node?.range?.[1] ?? 0, }; } function findYamlNode(file, path, kind = "value") { let current = file.doc.contents; for (let i = 0; i < path.length; i++) { const key = path[i]; const isLast = i === path.length - 1; if (isScalar(current)) { return current; } else if (isCollection(current)) { if (isLast) { if (kind === "value" || !isMap(current)) { if (Array.isArray(current.items) && current.items.every((item) => isScalar(item))) { return current.items.find((m) => m.source && m.source === key); } else { return current.get(key, true); } } else { const pair = findPair(current.items, key); if (kind === "key") { return pair?.key; } else { return pair; } } } else { current = current.get(key, true); } } else { continue; } } return current ?? undefined; } //# sourceMappingURL=diagnostics.js.map