Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions src/nodes/Alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Document } from '../doc/Document.ts'
import type { FlowScalar } from '../parse/cst.ts'
import type { StringifyContext } from '../stringify/stringify.ts'
import { visit } from '../visit.ts'
import { ALIAS, isAlias, isCollection, isPair } from './identity.ts'
import { ALIAS, hasAnchor, isAlias, isCollection, isPair } from './identity.ts'
import type { Node, Range } from './Node.ts'
import { NodeBase } from './Node.ts'
import type { Scalar } from './Scalar.ts'
Expand Down Expand Up @@ -38,21 +38,38 @@ export class Alias extends NodeBase {
* Resolve the value of this alias within `doc`, finding the last
* instance of the `source` anchor before this node.
*/
resolve(doc: Document): Scalar | YAMLMap | YAMLSeq | undefined {
resolve(
doc: Document,
ctx?: ToJSContext
): Scalar | YAMLMap | YAMLSeq | undefined {
let found: Scalar | YAMLMap | YAMLSeq | undefined = undefined
visit(doc, {
Node: (_key: unknown, node: Node) => {
if (node === this) return visit.BREAK
if (node.anchor === this.source) found = node
}
})

if (!ctx) {
visit(doc, {
Node: (_key: unknown, node: Node) => {
if (node === this) return visit.BREAK
if (node.anchor === this.source) found = node
}
})
return found
}

if (!ctx.aliasResolveCache) {
ctx.aliasResolveCache = buildCache(doc)
}

for (const node of ctx.aliasResolveCache) {
if (node === this) break
if (node.anchor === this.source) found = node
}

return found
}
Comment thread
eemeli marked this conversation as resolved.

toJSON(_arg?: unknown, ctx?: ToJSContext): unknown {
if (!ctx) return { source: this.source }
const { anchors, doc, maxAliasCount } = ctx
const source = this.resolve(doc)
const source = this.resolve(doc, ctx)
if (!source) {
const msg = `Unresolved alias (the anchor must be set before the alias): ${this.source}`
throw new ReferenceError(msg)
Expand Down Expand Up @@ -122,3 +139,15 @@ function getAliasCount(
}
return 1
}

function buildCache(
doc: Document<Node, boolean>
): (Scalar | YAMLMap | YAMLSeq | Alias)[] {
const found: (Scalar | YAMLMap | YAMLSeq | Alias)[] = []
visit(doc, {
Node: (_key: unknown, node: Node) => {
if (isAlias(node) || hasAnchor(node)) found.push(node)
}
})
return found
}
6 changes: 6 additions & 0 deletions src/nodes/toJS.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import type { Document } from '../doc/Document.ts'
import { hasAnchor } from './identity.ts'
import type { Node } from './Node.ts'
import type { Alias } from './Alias.ts'
import type { Scalar } from './Scalar.ts'
import type { YAMLMap } from './YAMLMap.ts'
import type { YAMLSeq } from './YAMLSeq.ts'

export interface AnchorData {
aliasCount: number
Expand All @@ -10,6 +14,8 @@ export interface AnchorData {

export interface ToJSContext {
anchors: Map<Node, AnchorData>
/** Cached anchor and allias nodes in the order they occurred in the document */
aliasResolveCache?: (Alias | Scalar | YAMLMap | YAMLSeq)[]
Comment thread
eemeli marked this conversation as resolved.
Outdated
doc: Document<Node, boolean>
keep: boolean
mapAsMap: boolean
Expand Down