forked from eemeli/yaml
/
Node.ts
119 lines (95 loc) · 3.47 KB
/
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
import type { Document } from '../doc/Document.js'
import { Token } from '../parse/cst.js'
import type { StringifyContext } from '../stringify/stringify.js'
import type { Alias } from './Alias.js'
import type { Pair } from './Pair.js'
import type { Scalar } from './Scalar.js'
import type { YAMLMap } from './YAMLMap.js'
import type { YAMLSeq } from './YAMLSeq.js'
export type Node = Alias | Scalar | YAMLMap | YAMLSeq
export type ParsedNode =
| Alias.Parsed
| Scalar.Parsed
| YAMLMap.Parsed
| YAMLSeq.Parsed
export type Range = [start: number, valueEnd: number, nodeEnd: number]
export const ALIAS = Symbol.for('yaml.alias')
export const DOC = Symbol.for('yaml.document')
export const MAP = Symbol.for('yaml.map')
export const PAIR = Symbol.for('yaml.pair')
export const SCALAR = Symbol.for('yaml.scalar')
export const SEQ = Symbol.for('yaml.seq')
export const NODE_TYPE = Symbol.for('yaml.node.type')
export const isAlias = (node: any): node is Alias =>
!!node && typeof node === 'object' && node[NODE_TYPE] === ALIAS
export const isDocument = (node: any): node is Document =>
!!node && typeof node === 'object' && node[NODE_TYPE] === DOC
export const isMap = (node: any): node is YAMLMap =>
!!node && typeof node === 'object' && node[NODE_TYPE] === MAP
export const isPair = (node: any): node is Pair =>
!!node && typeof node === 'object' && node[NODE_TYPE] === PAIR
export const isScalar = (node: any): node is Scalar =>
!!node && typeof node === 'object' && node[NODE_TYPE] === SCALAR
export const isSeq = (node: any): node is YAMLSeq =>
!!node && typeof node === 'object' && node[NODE_TYPE] === SEQ
export function isCollection(node: any): node is YAMLMap | YAMLSeq {
if (node && typeof node === 'object')
switch (node[NODE_TYPE]) {
case MAP:
case SEQ:
return true
}
return false
}
export function isNode(node: any): node is Node {
if (node && typeof node === 'object')
switch (node[NODE_TYPE]) {
case ALIAS:
case MAP:
case SCALAR:
case SEQ:
return true
}
return false
}
export const hasAnchor = (node: unknown): node is Scalar | YAMLMap | YAMLSeq =>
(isScalar(node) || isCollection(node)) && !!node.anchor
export abstract class NodeBase {
readonly [NODE_TYPE]: symbol
/** A comment on or immediately after this */
declare comment?: string | null
/** A comment before this */
declare commentBefore?: string | null
/**
* The `[start, value-end, node-end]` character offsets for the part of the
* source parsed into this node (undefined if not parsed). The `value-end`
* and `node-end` positions are themselves not included in their respective
* ranges.
*/
declare range?: Range | null
/** A blank line before this node and its commentBefore */
declare spaceBefore?: boolean
/** The CST token that was composed into this node. */
declare srcToken?: Token
/** A fully qualified tag, if required */
declare tag?: string
/** A plain JS representation of this node */
abstract toJSON(): any
abstract toString(
ctx?: StringifyContext,
onComment?: () => void,
onChompKeep?: () => void
): string
constructor(type: symbol) {
Object.defineProperty(this, NODE_TYPE, { value: type })
}
/** Create a copy of this node. */
clone(): NodeBase {
const copy: NodeBase = Object.create(
Object.getPrototypeOf(this),
Object.getOwnPropertyDescriptors(this)
)
if (this.range) copy.range = this.range.slice() as NodeBase['range']
return copy
}
}