diff --git a/src/doc/Document.ts b/src/doc/Document.ts index de666740..2fb90a6f 100644 --- a/src/doc/Document.ts +++ b/src/doc/Document.ts @@ -205,7 +205,7 @@ export class Document< const keyToStr = (v: unknown) => typeof v === 'number' || v instanceof String || v instanceof Number const asStr = replacer.filter(keyToStr).map(String) - if (asStr.length > 0) replacer = replacer.concat(asStr) + if (asStr.length > 0) replacer = [...replacer, ...asStr] nc = new NodeCreator(this, options, replacer) } else { options ??= replacer ?? undefined diff --git a/src/nodes/Alias.ts b/src/nodes/Alias.ts index 25922ab1..eee15eac 100644 --- a/src/nodes/Alias.ts +++ b/src/nodes/Alias.ts @@ -110,8 +110,7 @@ export class Alias implements NodeBase { } if (maxAliasCount >= 0) { data.count += 1 - if (data.aliasCount === 0) - data.aliasCount = getAliasCount(doc, source, anchors) + data.aliasCount ||= getAliasCount(doc, ctx, source, anchors) if (data.count * data.aliasCount > maxAliasCount) { const msg = 'Excessive alias count indicates a resource exhaustion attack' @@ -142,21 +141,22 @@ export class Alias implements NodeBase { function getAliasCount( doc: Document, + ctx: ToJSContext, node: Node | Pair | null, anchors: ToJSContext['anchors'] ): number { if (node instanceof Alias) { - const source = node.resolve(doc) + const source = node.resolve(doc, ctx) const anchor = anchors && source && anchors.get(source) return anchor ? anchor.count * anchor.aliasCount : 0 } else if (node instanceof Pair) { - const kc = getAliasCount(doc, node.key, anchors) - const vc = getAliasCount(doc, node.value, anchors) + const kc = getAliasCount(doc, ctx, node.key, anchors) + const vc = getAliasCount(doc, ctx, node.value, anchors) return Math.max(kc, vc) } else if (node && 'items' in node) { let count = 0 for (const item of node.items) { - const c = getAliasCount(doc, item, anchors) + const c = getAliasCount(doc, ctx, item, anchors) if (c > count) count = c } return count diff --git a/src/parse/cst-visit.ts b/src/parse/cst-visit.ts index 214d549a..ab7b6914 100644 --- a/src/parse/cst-visit.ts +++ b/src/parse/cst-visit.ts @@ -75,7 +75,7 @@ export const visit: { } = function visit(cst, visitor) { if ('type' in cst && cst.type === 'document') cst = { start: cst.start, value: cst.value } - _visit(Object.freeze([]), cst, visitor) + _visit([], cst, visitor) } visit.BREAK = BREAK @@ -118,11 +118,7 @@ function _visit( const token = item[field] if (token && 'items' in token) { for (let i = 0; i < token.items.length; ++i) { - const ci = _visit( - Object.freeze(path.concat([[field, i]])), - token.items[i], - visitor - ) + const ci = _visit([...path, [field, i]], token.items[i], visitor) if (typeof ci === 'number') i = ci - 1 else if (ci === BREAK) return BREAK else if (ci === REMOVE) { diff --git a/src/parse/parser.ts b/src/parse/parser.ts index 422bbffe..1443cc11 100644 --- a/src/parse/parser.ts +++ b/src/parse/parser.ts @@ -641,7 +641,7 @@ export class Parser { }) } else if (start.length > 0) { // Not actually at next item - it.sep = it.sep.concat(start, this.sourceToken) + it.sep = [...it.sep, ...start, this.sourceToken] } else { it.sep.push(this.sourceToken) } diff --git a/src/schema/tags.ts b/src/schema/tags.ts index 3b1a8403..c25c1694 100644 --- a/src/schema/tags.ts +++ b/src/schema/tags.ts @@ -98,7 +98,7 @@ export function getTags( const schemaTags = schemas.get(schemaName) if (schemaTags && !customTags) { return addMergeTag && !schemaTags.includes(merge) - ? schemaTags.concat(merge) + ? [...schemaTags, merge] : schemaTags.slice() } @@ -121,7 +121,7 @@ export function getTags( } else if (typeof customTags === 'function') { tags = customTags(tags.slice()) } - if (addMergeTag) tags = tags.concat(merge) + if (addMergeTag) tags = [...tags, merge] return tags.reduce<(CollectionTag | ScalarTag)[]>((tags, tag) => { const tagObj = typeof tag === 'string' ? tagsByName[tag] : tag diff --git a/src/schema/yaml-1.1/merge.ts b/src/schema/yaml-1.1/merge.ts index bc424cf3..f6bca109 100644 --- a/src/schema/yaml-1.1/merge.ts +++ b/src/schema/yaml-1.1/merge.ts @@ -49,7 +49,7 @@ export function addMergeToJSMap( map: MapLike, value: unknown ): void { - value = ctx && value instanceof Alias ? value.resolve(doc) : value + value = ctx && value instanceof Alias ? value.resolve(doc, ctx) : value if (value instanceof YAMLSeq) for (const it of value.items) mergeValue(doc, ctx, map, it) else if (Array.isArray(value)) @@ -63,7 +63,7 @@ function mergeValue( map: MapLike, value: unknown ) { - const source = value instanceof Alias ? value.resolve(doc) : value + const source = value instanceof Alias ? value.resolve(doc, ctx) : value const srcMap = (source as YAMLMap).toJS(doc, ctx, Map) if (!(srcMap instanceof Map)) throw new Error('Merge sources must be maps or map aliases') diff --git a/src/visit.ts b/src/visit.ts index 01db124d..67e1d70f 100644 --- a/src/visit.ts +++ b/src/visit.ts @@ -99,9 +99,9 @@ export const visit: { } = function visit(node, visitor) { const visitor_ = initVisitor(visitor) if (node instanceof Document) { - const cd = visit_(null, node.value, visitor_, Object.freeze([node])) + const cd = visit_(null, node.value, visitor_, [node]) if (cd === REMOVE) node.value = new Scalar(null) - } else visit_(null, node, visitor_, Object.freeze([])) + } else visit_(null, node, visitor_, []) } visit.BREAK = BREAK @@ -123,7 +123,7 @@ function visit_( if (typeof ctrl !== 'symbol') { if (node instanceof YAMLMap || node instanceof YAMLSeq) { - path = Object.freeze([...path, node]) + path = [...path, node] for (let i = 0; i < node.items.length; ++i) { const ci = visit_(i, node.items[i], visitor, path) if (typeof ci === 'number') i = ci - 1 @@ -134,7 +134,7 @@ function visit_( } } } else if (node instanceof Pair) { - path = Object.freeze([...path, node]) + path = [...path, node] const ck = visit_('key', node.key, visitor, path) if (ck === BREAK) return BREAK else if (ck === REMOVE) node.key = new Scalar(null) @@ -193,14 +193,9 @@ export const visitAsync: { } = async function visitAsync(node, visitor) { const visitor_ = initVisitor(visitor) if (node instanceof Document) { - const cd = await visitAsync_( - null, - node.value, - visitor_, - Object.freeze([node]) - ) + const cd = await visitAsync_(null, node.value, visitor_, [node]) if (cd === REMOVE) node.value = new Scalar(null) - } else await visitAsync_(null, node, visitor_, Object.freeze([])) + } else await visitAsync_(null, node, visitor_, []) } visitAsync.BREAK = BREAK @@ -222,7 +217,7 @@ async function visitAsync_( if (typeof ctrl !== 'symbol') { if (node instanceof YAMLMap || node instanceof YAMLSeq) { - path = Object.freeze([...path, node]) + path = [...path, node] for (let i = 0; i < node.items.length; ++i) { const ci = await visitAsync_(i, node.items[i], visitor, path) if (typeof ci === 'number') i = ci - 1 @@ -233,7 +228,7 @@ async function visitAsync_( } } } else if (node instanceof Pair) { - path = Object.freeze([...path, node]) + path = [...path, node] const ck = await visitAsync_('key', node.key, visitor, path) if (ck === BREAK) return BREAK else if (ck === REMOVE) node.key = new Scalar(null)