-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathcompose-node.ts
More file actions
160 lines (154 loc) · 4.55 KB
/
Copy pathcompose-node.ts
File metadata and controls
160 lines (154 loc) · 4.55 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import type { Directives } from '../doc/directives.ts'
import { Alias } from '../nodes/Alias.ts'
import { isScalar } from '../nodes/identity.ts'
import type { ParsedNode } from '../nodes/Node.ts'
import type { ParseOptions } from '../options.ts'
import type { FlowScalar, SourceToken, Token } from '../parse/cst.ts'
import type { Schema } from '../schema/Schema.ts'
import { composeCollection } from './compose-collection.ts'
import { composeScalar } from './compose-scalar.ts'
import type { ComposeErrorHandler } from './composer.ts'
import { resolveEnd } from './resolve-end.ts'
import { emptyScalarPosition } from './util-empty-scalar-position.ts'
export interface ComposeContext {
atKey: boolean
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
newlineAfterProp: 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 atKey = ctx.atKey
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, props, 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 (
atKey &&
ctx.options.stringKeys &&
(!isScalar(node) ||
typeof node.value !== 'string' ||
(node.tag && node.tag !== 'tag:yaml.org,2002:str'))
) {
const msg = 'With stringKeys, all keys must be strings'
onError(tag ?? token, 'NON_STRING_KEY', msg)
}
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
// Extend the range to include any trailing content (like comments) if it's beyond the current range end
if (end > node.range[2]) {
node.range[2] = end
}
if (comment) {
node.comment = comment
}
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
}