diff --git a/dist/index.js b/dist/index.js index a9c4d31..b778f82 100644 --- a/dist/index.js +++ b/dist/index.js @@ -473,8 +473,7 @@ function createNodeAnchors(doc, prefix) { return { onAnchor: (source) => { aliasObjects.push(source); - if (!prevAnchors) - prevAnchors = anchorNames(doc); + prevAnchors ?? (prevAnchors = anchorNames(doc)); const anchor = findNewAnchor(prefix, prevAnchors); prevAnchors.add(anchor); return anchor; @@ -638,23 +637,36 @@ 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) { + resolve(doc, ctx) { + let nodes; + if (ctx?.aliasResolveCache) { + nodes = ctx.aliasResolveCache; + } + else { + nodes = []; + visit$1(doc, { + Node: (_key, node) => { + if (isAlias(node) || hasAnchor(node)) + nodes.push(node); + } + }); + if (ctx) + ctx.aliasResolveCache = nodes; + } let found = undefined; - visit$1(doc, { - Node: (_key, node) => { - if (node === this) - return visit$1.BREAK; - if (node.anchor === this.source) - found = node; - } - }); + for (const node of nodes) { + if (node === this) + break; + if (node.anchor === this.source) + found = node; + } return found; } toJSON(_arg, ctx) { 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); @@ -773,8 +785,7 @@ function createNode(value, tagName, ctx) { if (aliasDuplicateObjects && value && typeof value === 'object') { ref = sourceObjects.get(value); if (ref) { - if (!ref.anchor) - ref.anchor = onAnchor(value); + ref.anchor ?? (ref.anchor = onAnchor(value)); return new Alias(ref.anchor); } else { @@ -1379,10 +1390,9 @@ function plainString(item, ctx, onComment, onChompKeep) { (inFlow && /[[\]{},]/.test(value))) { return quotedString(value, ctx); } - if (!value || - /^[\n\t ,[\]{}#&*!|>'"%@`]|^[?-]$|^[?-][ \t]|[\n:][ \t]|[ \t]\n|[\n\t ]#|[\n\t :]$/.test(value)) { + if (/^[\n\t ,[\]{}#&*!|>'"%@`]|^[?-]$|^[?-][ \t]|[\n:][ \t]|[ \t]\n|[\n\t ]#|[\n\t :]$/.test(value)) { // not allowed: - // - empty string, '-' or '?' + // - '-' or '?' // - start with an indicator character (except [?:-]) or /[?-] / // - '\n ', ': ' or ' \n' anywhere // - '#' not preceded by a non-space char @@ -1525,7 +1535,7 @@ function getTagObject(tags, item) { tagObj = tags.find(t => t.nodeClass && obj instanceof t.nodeClass); } if (!tagObj) { - const name = obj?.constructor?.name ?? typeof obj; + const name = obj?.constructor?.name ?? (obj === null ? 'null' : typeof obj); throw new Error(`Tag not resolved for ${name} value`); } return tagObj; @@ -1540,7 +1550,7 @@ function stringifyProps(node, tagObj, { anchors, doc }) { anchors.add(anchor); props.push(`&${anchor}`); } - const tag = node.tag ? node.tag : tagObj.default ? null : tagObj.tag; + const tag = node.tag ?? (tagObj.default ? null : tagObj.tag); if (tag) props.push(doc.directives.tagString(tag)); return props.join(' '); @@ -1566,8 +1576,7 @@ function stringify$2(item, ctx, onComment, onChompKeep) { const node = isNode(item) ? item : ctx.doc.createNode(item, { onTagObj: o => (tagObj = o) }); - if (!tagObj) - tagObj = getTagObject(ctx.doc.schema.tags, node); + tagObj ?? (tagObj = getTagObject(ctx.doc.schema.tags, node)); const props = stringifyProps(node, tagObj, ctx); if (props.length > 0) ctx.indentAtStart = (ctx.indentAtStart ?? 0) + props.length + 1; @@ -1826,6 +1835,7 @@ function addPairToJSMap(ctx, map, { key, value }) { function stringifyKey(key, jsKey, ctx) { if (jsKey === null) return ''; + // eslint-disable-next-line @typescript-eslint/no-base-to-string if (typeof jsKey !== 'object') return String(jsKey); if (isNode(key) && ctx?.doc) { @@ -2530,8 +2540,7 @@ const binary = { else { throw new Error('This environment does not support writing binary tags; either Buffer or btoa is required'); } - if (!type) - type = Scalar.BLOCK_LITERAL; + type ?? (type = Scalar.BLOCK_LITERAL); if (type !== Scalar.QUOTE_DOUBLE) { const lineWidth = Math.max(ctx.options.lineWidth - ctx.indent.length, ctx.options.minContentWidth); const n = Math.ceil(str.length / lineWidth); @@ -3667,8 +3676,7 @@ function resolveProps(tokens, { flow, indicator, next, offset, onError, parentIn if (token.source.endsWith(':')) onError(token.offset + token.source.length - 1, 'BAD_ALIAS', 'Anchor ending in : is ambiguous', true); anchor = token; - if (start === null) - start = token.offset; + start ?? (start = token.offset); atNewline = false; hasSpace = false; reqSpace = true; @@ -3677,8 +3685,7 @@ function resolveProps(tokens, { flow, indicator, next, offset, onError, parentIn if (tag) onError(token, 'MULTIPLE_TAGS', 'A node can have at most one tag'); tag = token; - if (start === null) - start = token.offset; + start ?? (start = token.offset); atNewline = false; hasSpace = false; reqSpace = true; @@ -4754,8 +4761,7 @@ function findScalarTagByTest({ atKey, directives, schema }, value, token, onErro function emptyScalarPosition(offset, before, pos) { if (before) { - if (pos === null) - pos = before.length; + pos ?? (pos = before.length); for (let i = pos - 1; i >= 0; --i) { let st = before[i]; switch (st.type) { diff --git a/package-lock.json b/package-lock.json index 844bc4d..d21d1f9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10145,14 +10145,14 @@ "dev": true }, "node_modules/yaml": { - "version": "2.7.1", - "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.7.1.tgz", - "integrity": "sha512-10ULxpnOCQXxJvBgxsn9ptjq6uviG/htZKk9veJGhlqn3w/DxQ631zFF+nlQXLwmImeS5amR2dl2U8sg6U9jsQ==", + "version": "2.8.0", + "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.8.0.tgz", + "integrity": "sha512-4lLa/EcQCB0cJkyts+FpIRx5G/llPxfP6VQU5KByHEhLxY3IJCH0f0Hy1MHI8sClTvsIb8qwRJ6R/ZdlDJ/leQ==", "bin": { "yaml": "bin.mjs" }, "engines": { - "node": ">= 14" + "node": ">= 14.6" } }, "node_modules/yargs": {