yaml-unist-parser
Version:
A YAML parser that produces output compatible with unist
44 lines (43 loc) • 1.11 kB
JavaScript
const FINISH = true;
export function addOrigRange(cst) {
if (!cst.setOrigRanges()) {
const fn = (obj) => {
if (isRange(obj)) {
obj.origStart = obj.start;
obj.origEnd = obj.end;
return FINISH;
}
if (isFlowChar(obj)) {
obj.origOffset = obj.offset;
return FINISH;
}
};
cst.forEach(document => visit(document, fn));
}
}
function visit(obj, fn) {
if (!obj || typeof obj !== "object") {
return;
}
if (fn(obj) === FINISH) {
return;
}
for (const key of Object.keys(obj)) {
if (key === "context" || key === "error") {
continue;
}
const value = obj[key];
if (Array.isArray(value)) {
value.forEach(x => visit(x, fn));
}
else {
visit(value, fn);
}
}
}
function isRange(obj) {
return typeof obj.start === "number";
}
function isFlowChar(obj) {
return typeof obj.offset === "number";
}