-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathflow-map.ts
More file actions
73 lines (63 loc) · 2.29 KB
/
flow-map.ts
File metadata and controls
73 lines (63 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import type * as YAML from "yaml";
import type * as YAML_CST from "../cst.js";
import { createFlowMapping } from "../factories/flow-mapping.js";
import { createFlowMappingItem } from "../factories/flow-mapping-item.js";
import type { FlowMapping } from "../types.js";
import { extractComments } from "../utils/extract-comments.js";
import type Context from "./context.js";
import { transformPair } from "./pair.js";
import type { TransformNodeProperties } from "./transform.js";
export function transformFlowMap(
flowMap: YAML.YAMLMap.Parsed,
context: Context,
props: TransformNodeProperties,
): FlowMapping {
const srcToken = flowMap.srcToken;
// istanbul ignore if -- @preserve
if (!srcToken || srcToken.type !== "flow-collection") {
throw new Error("Expected flow-collection CST node for flow map");
}
const flowMappingItems = flowMap.items.map((pair, index) => {
const srcItem = srcToken.items[index];
return transformPair(pair, srcItem, context, createFlowMappingItem);
});
if (flowMap.items.length < srcToken.items.length) {
// Handle extra comments
for (let i = flowMap.items.length; i < srcToken.items.length; i++) {
const srcItem = srcToken.items[i];
for (const token of extractComments(srcItem.start, context)) {
// istanbul ignore else -- @preserve
if (token.type === "comma") {
// skip
continue;
}
// istanbul ignore next -- @preserve
throw new Error(
`Unexpected token type in collection item start: ${token.type}`,
);
}
}
}
let flowMapEndToken: YAML_CST.FlowMapEndSourceToken | null = null;
for (const token of extractComments(srcToken.end, context)) {
// istanbul ignore else -- @preserve
if (token.type === "flow-map-end") {
flowMapEndToken = token;
continue;
}
// istanbul ignore next -- @preserve
throw new Error(`Unexpected token type in flow map end: ${token.type}`);
}
// istanbul ignore if -- @preserve
if (!flowMapEndToken) {
throw new Error("Expected flow-map-end token");
}
return createFlowMapping(
context.transformRange([
srcToken.start.offset,
flowMapEndToken.offset + flowMapEndToken.source.length,
]),
context.transformContentProperties(flowMap, props.tokens),
flowMappingItems,
);
}