/
compose-node.ts
140 lines (134 loc) · 3.98 KB
/
compose-node.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import type { Directives } from '../doc/directives.js'
import { Alias } from '../nodes/Alias.js'
import type { ParsedNode } from '../nodes/Node.js'
import type { ParseOptions } from '../options.js'
import type { FlowScalar, SourceToken, Token } from '../parse/cst.js'
import type { Schema } from '../schema/Schema.js'
import { composeCollection } from './compose-collection.js'
import { composeScalar } from './compose-scalar.js'
import type { ComposeErrorHandler } from './composer.js'
import { resolveEnd } from './resolve-end.js'
import { emptyScalarPosition } from './util-empty-scalar-position.js'
export interface ComposeContext {
atRoot: boolean
directives: Directives
options: Readonly<Required<Omit<ParseOptions, 'lineCounter'>>>
schema: Readonly<Schema>
}
interface Props {
spaceBefore: boolean
comment: string
anchor: SourceToken | null
tag: SourceToken | null
end: number
}
const CN = { composeNode, composeEmptyNode }
export type ComposeNode = typeof CN
export function composeNode(
ctx: ComposeContext,
token: Token,
props: Props,
onError: ComposeErrorHandler
) {
const { spaceBefore, comment, anchor, tag } = props
let node: ParsedNode
let isSrcToken = true
switch (token.type) {
case 'alias':
node = composeAlias(ctx, token, onError)
if (anchor || tag)
onError(
token,
'ALIAS_PROPS',
'An alias node must not specify any properties'
)
break
case 'scalar':
case 'single-quoted-scalar':
case 'double-quoted-scalar':
case 'block-scalar':
node = composeScalar(ctx, token, tag, onError)
if (anchor) node.anchor = anchor.source.substring(1)
break
case 'block-map':
case 'block-seq':
case 'flow-collection':
node = composeCollection(CN, ctx, token, tag, onError)
if (anchor) node.anchor = anchor.source.substring(1)
break
default: {
const message =
token.type === 'error'
? token.message
: `Unsupported token (type: ${token.type})`
onError(token, 'UNEXPECTED_TOKEN', message)
node = composeEmptyNode(
ctx,
token.offset,
undefined,
null,
props,
onError
)
isSrcToken = false
}
}
if (anchor && node.anchor === '')
onError(anchor, 'BAD_ALIAS', 'Anchor cannot be an empty string')
if (spaceBefore) node.spaceBefore = true
if (comment) {
if (token.type === 'scalar' && token.source === '') node.comment = comment
else node.commentBefore = comment
}
// @ts-expect-error Type checking misses meaning of isSrcToken
if (ctx.options.keepSourceTokens && isSrcToken) node.srcToken = token
return node
}
export function composeEmptyNode(
ctx: ComposeContext,
offset: number,
before: Token[] | undefined,
pos: number | null,
{ spaceBefore, comment, anchor, tag, end }: Props,
onError: ComposeErrorHandler
) {
const token: FlowScalar = {
type: 'scalar',
offset: emptyScalarPosition(offset, before, pos),
indent: -1,
source: ''
}
const node = composeScalar(ctx, token, tag, onError)
if (anchor) {
node.anchor = anchor.source.substring(1)
if (node.anchor === '')
onError(anchor, 'BAD_ALIAS', 'Anchor cannot be an empty string')
}
if (spaceBefore) node.spaceBefore = true
if (comment) {
node.comment = comment
node.range[2] = end
}
return node
}
function composeAlias(
{ options }: ComposeContext,
{ offset, source, end }: FlowScalar,
onError: ComposeErrorHandler
) {
const alias = new Alias(source.substring(1))
if (alias.source === '')
onError(offset, 'BAD_ALIAS', 'Alias cannot be an empty string')
if (alias.source.endsWith(':'))
onError(
offset + source.length - 1,
'BAD_ALIAS',
'Alias ending in : is ambiguous',
true
)
const valueEnd = offset + source.length
const re = resolveEnd(end, valueEnd, options.strict, onError)
alias.range = [offset, valueEnd, re.offset]
if (re.comment) alias.comment = re.comment
return alias as Alias.Parsed
}