@@ -3,7 +3,7 @@ import type { Document } from '../doc/Document.ts'
33import type { FlowScalar } from '../parse/cst.ts'
44import type { StringifyContext } from '../stringify/stringify.ts'
55import { visit } from '../visit.ts'
6- import { ALIAS , isAlias , isCollection , isPair } from './identity.ts'
6+ import { ALIAS , hasAnchor , isAlias , isCollection , isPair } from './identity.ts'
77import type { Node , Range } from './Node.ts'
88import { NodeBase } from './Node.ts'
99import type { Scalar } from './Scalar.ts'
@@ -38,21 +38,36 @@ export class Alias extends NodeBase {
3838 * Resolve the value of this alias within `doc`, finding the last
3939 * instance of the `source` anchor before this node.
4040 */
41- resolve ( doc : Document ) : Scalar | YAMLMap | YAMLSeq | undefined {
41+ resolve (
42+ doc : Document ,
43+ ctx ?: ToJSContext
44+ ) : Scalar | YAMLMap | YAMLSeq | undefined {
45+ let nodes : Node [ ]
46+ if ( ctx ?. aliasResolveCache ) {
47+ nodes = ctx . aliasResolveCache
48+ } else {
49+ nodes = [ ]
50+ visit ( doc , {
51+ Node : ( _key : unknown , node : Node ) => {
52+ if ( isAlias ( node ) || hasAnchor ( node ) ) nodes . push ( node )
53+ }
54+ } )
55+ if ( ctx ) ctx . aliasResolveCache = nodes
56+ }
57+
4258 let found : Scalar | YAMLMap | YAMLSeq | undefined = undefined
43- visit ( doc , {
44- Node : ( _key : unknown , node : Node ) => {
45- if ( node === this ) return visit . BREAK
46- if ( node . anchor === this . source ) found = node
47- }
48- } )
59+ for ( const node of nodes ) {
60+ if ( node === this ) break
61+ if ( node . anchor === this . source ) found = node
62+ }
63+
4964 return found
5065 }
5166
5267 toJSON ( _arg ?: unknown , ctx ?: ToJSContext ) : unknown {
5368 if ( ! ctx ) return { source : this . source }
5469 const { anchors, doc, maxAliasCount } = ctx
55- const source = this . resolve ( doc )
70+ const source = this . resolve ( doc , ctx )
5671 if ( ! source ) {
5772 const msg = `Unresolved alias (the anchor must be set before the alias): ${ this . source } `
5873 throw new ReferenceError ( msg )
0 commit comments