/
resolve-block-seq.ts
52 lines (50 loc) · 1.64 KB
/
resolve-block-seq.ts
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
import { YAMLSeq } from '../nodes/YAMLSeq.js'
import type { BlockSequence } from '../parse/cst.js'
import type { ComposeContext, ComposeNode } from './compose-node.js'
import type { ComposeErrorHandler } from './composer.js'
import { resolveProps } from './resolve-props.js'
import { flowIndentCheck } from './util-flow-indent-check.js'
export function resolveBlockSeq(
{ composeNode, composeEmptyNode }: ComposeNode,
ctx: ComposeContext,
bs: BlockSequence,
onError: ComposeErrorHandler
) {
const seq = new YAMLSeq(ctx.schema)
if (ctx.atRoot) ctx.atRoot = false
let offset = bs.offset
let commentEnd: number | null = null
for (const { start, value } of bs.items) {
const props = resolveProps(start, {
indicator: 'seq-item-ind',
next: value,
offset,
onError,
startOnNewline: true
})
if (!props.found) {
if (props.anchor || props.tag || value) {
if (value && value.type === 'block-seq')
onError(
props.end,
'BAD_INDENT',
'All sequence items must start at the same column'
)
else
onError(offset, 'MISSING_CHAR', 'Sequence item without - indicator')
} else {
commentEnd = props.end
if (props.comment) seq.comment = props.comment
continue
}
}
const node = value
? composeNode(ctx, value, props, onError)
: composeEmptyNode(ctx, props.end, start, null, props, onError)
if (ctx.schema.compat) flowIndentCheck(bs.indent, value, onError)
offset = node.range[2]
seq.items.push(node)
}
seq.range = [bs.offset, offset, commentEnd ?? offset]
return seq as YAMLSeq.Parsed
}