Is there a way to map returned values to their row/col position? #279
-
I'm working on yaml-enforce, an api/cli to check structure and typing of YAML files, and I'd love to be able to give line+column numbers for validation errors. Is there a way to store that info somewhere while the data is being parsed? Something like: import { parse } from "yaml";
const yamlText = "...";
const lineNumbers = {};
const data = parse(yamlText, {
lineTracker: (path, line, column) => lineNumbers[path] = { line, col },
}); Where 'path' is preferably the dot-notation path to the value (like Since LineCounter already exists I would also be fine with it just returning an offset into the input string as I can then use a LineCounter to calculate the line/col as needed. import { parse } from "yaml";
const yamlText = "...";
const offsets = {};
const data = parse(yamlText, {
offsetTracker: (path, offset) => offsets[path] = offset,
}); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The intended way to achieve this would be something like this: import { LineCounter, parseDocument, visit } from 'yaml'
const lc = new LineCounter()
const doc = parseDocument('...', { lineCounter: lc })
visit(doc, (_key, node, path) => console.log({ path, pos: lc.linePos(node.range[0]) }))
const data = doc.toJS() |
Beta Was this translation helpful? Give feedback.
The intended way to achieve this would be something like this: