yaml-unist-parser
Version:
A YAML parser that produces output compatible with unist
71 lines (69 loc) • 3.99 kB
JavaScript
import { createEmptyPosition, createPosition } from "../factories/position.mjs";
import { maybeContentPropertyToken, tokens } from "../cst.mjs";
import { extractComments } from "../utils/extract-comments.mjs";
import { createMappingKey } from "../factories/mapping-key.mjs";
import { createMappingValue } from "../factories/mapping-value.mjs";
import { isEmptyNode } from "./transform.mjs";
//#region src/transforms/pair.ts
function transformPair(pair, srcItem, context, createNode) {
var _srcItem$key;
const keyPropTokens = [];
let explicitKeyIndToken = null;
for (const token of tokens(srcItem.start)) {
if (maybeContentPropertyToken(token)) {
keyPropTokens.push(token);
continue;
}
if (token.type === "explicit-key-ind") {
explicitKeyIndToken = token;
continue;
}
// istanbul ignore else -- @preserve
if (token.type === "comma") continue;
// istanbul ignore next -- @preserve
throw new Error(`Unexpected token type in collection item start: ${token.type}`);
}
const valuePropTokens = [];
let mapValueIndToken = null;
for (const token of tokens(srcItem.sep)) {
if (maybeContentPropertyToken(token)) {
valuePropTokens.push(token);
continue;
}
// istanbul ignore else -- @preserve
if (token.type === "map-value-ind") {
mapValueIndToken = token;
continue;
}
// istanbul ignore next -- @preserve
throw new Error(`Unexpected token type in collection item sep: ${token.type}`);
}
const keyStartOffset = (explicitKeyIndToken === null || explicitKeyIndToken === void 0 ? void 0 : explicitKeyIndToken.offset) ?? ((_srcItem$key = srcItem.key) === null || _srcItem$key === void 0 ? void 0 : _srcItem$key.offset) ?? (mapValueIndToken === null || mapValueIndToken === void 0 ? void 0 : mapValueIndToken.offset) ?? srcItem.value.offset;
const keyRange = [keyStartOffset, srcItem.key ? pair.key.range[1] : explicitKeyIndToken ? explicitKeyIndToken.offset + explicitKeyIndToken.source.length : keyStartOffset];
let valueRange = null;
if (pair.value) {
var _srcItem$value;
const valueStartOffset = (mapValueIndToken === null || mapValueIndToken === void 0 ? void 0 : mapValueIndToken.offset) ?? ((_srcItem$value = srcItem.value) === null || _srcItem$value === void 0 ? void 0 : _srcItem$value.offset) ?? pair.value.range[0];
valueRange = [valueStartOffset, srcItem.value ? pair.value.range[1] : mapValueIndToken ? mapValueIndToken.offset + mapValueIndToken.source.length : valueStartOffset];
}
return transformAstPair(pair, context, createNode, {
range: keyRange,
props: { tokens: keyPropTokens }
}, {
range: valueRange,
props: { tokens: valuePropTokens }
});
}
function transformAstPair(pair, context, createNode, additionalKeyData, additionalValueData) {
let keyContent = null;
if (!isEmptyNode(pair.key, additionalKeyData.props)) keyContent = context.transformNode(pair.key, additionalKeyData.props);
else extractComments(additionalKeyData.props.tokens, context);
let valueContent = null;
if (!isEmptyNode(pair.value, additionalValueData.props)) valueContent = context.transformNode(pair.value, additionalValueData.props);
else extractComments(additionalValueData.props.tokens, context);
const mappingKey = createMappingKey(context.transformRange([additionalKeyData.range ? additionalKeyData.range[0] : keyContent.position.start.offset, keyContent ? keyContent.position.end.offset : additionalKeyData.range[1]]), keyContent);
const mappingValue = valueContent || additionalValueData.range ? createMappingValue(context.transformRange([additionalValueData.range ? additionalValueData.range[0] : valueContent.position.start.offset, valueContent ? valueContent.position.end.offset : additionalValueData.range[0] + 1]), valueContent) : null;
return createNode(createPosition(mappingKey.position.start, mappingValue ? mappingValue.position.end : mappingKey.position.end), mappingKey, mappingValue || createMappingValue(createEmptyPosition(mappingKey.position.end), null));
}
//#endregion
export { transformPair };