diff --git a/lib/diff.js b/lib/diff.js index cefa14c..efc6b25 100644 --- a/lib/diff.js +++ b/lib/diff.js @@ -75,7 +75,7 @@ function diffDescriptors (lhs, rhs, options) { const isCircular = descriptor => lhsCircular.has(descriptor) || rhsCircular.has(descriptor) - const format = (builder, subject, circular) => { + const format = (builder, subject, circular, depthOffset = 0) => { if (diffIndex >= 0 && !diffStack[diffIndex].shouldFormat(subject)) return if (circular.has(subject)) { @@ -113,7 +113,7 @@ function diffDescriptors (lhs, rhs, options) { const formatter = subject.formatShallow(themeUtils.applyModifiers(subject, theme), indent) const recursor = subject.createRecursor() - if (formatter.increaseIndent && maxDepth > 0 && indent.level === maxDepth) { + if (formatter.increaseIndent && maxDepth > 0 && indent.level === (maxDepth + depthOffset)) { const isEmpty = recursor() === null let formatted = !isEmpty && typeof formatter.maxDepth === 'function' ? formatter.maxDepth() @@ -374,7 +374,7 @@ function diffDescriptors (lhs, rhs, options) { } do { - format(builder, subject, circular) + format(builder, subject, circular, indent.level) subject = stack[topIndex].recursor() } while (subject !== null) } diff --git a/test/diff.js b/test/diff.js index a99151f..8a3e7bb 100644 --- a/test/diff.js +++ b/test/diff.js @@ -485,3 +485,43 @@ test('inverted diffs', t => { baz: 'qux\ncorge\nquux', }, { invert: true })) }) + +test('lists: effectively resets depth when formatting differences', t => { + const l1 = [ + { + b: 'b', + }, + { + d: 'bar', + e: { + f: 'f', + }, + }, + ] + const l2 = [ + { + b: 'b', + }, + ] + t.snapshot(_diff(l1, l2, { maxDepth: 1, theme })) +}) + +test('objects: effectively resets depth when formatting differences', t => { + const o1 = { + a: { + b: 'b', + }, + c: { + d: 'bar', + e: { + f: 'f', + }, + }, + } + const o2 = { + a: { + b: 'b', + }, + } + t.snapshot(_diff(o1, o2, { maxDepth: 1, theme })) +}) diff --git a/test/snapshots/diff.js.md b/test/snapshots/diff.js.md index 597ca63..e54fdc7 100644 --- a/test/snapshots/diff.js.md +++ b/test/snapshots/diff.js.md @@ -1146,3 +1146,27 @@ Generated by [AVA](https://avajs.dev). %diffGutters.actual#- % foo%property.separator#: %%string.line.open#'%%string.diff.insertLine.open%BAR%string.diff.insertLine.close%%string.line.close#'%%property.after#,%␊ %diffGutters.expected#+ % foo%property.separator#: %%string.line.open#'%%string.diff.deleteLine.open%bar%string.diff.deleteLine.close%%string.line.close#'%%property.after#,%␊ %diffGutters.padding# %%object.closeBracket#}%` + +## lists: effectively resets depth when formatting differences + +> Snapshot 1 + + `%diffGutters.padding# %%list.openBracket#[%␊ + %diffGutters.padding# % %object.ctor.open%Object%object.ctor.close% %object.openBracket#{% %maxDepth#…% %object.closeBracket#}%%item.after#,%␊ + %diffGutters.actual#- % %object.openBracket#{%␊ + %diffGutters.actual#- % d%property.separator#: %%string.line.open#'%%string.open%bar%string.close%%string.line.close#'%%property.after#,%␊ + %diffGutters.actual#- % e%property.separator#: %%object.ctor.open%Object%object.ctor.close% %object.openBracket#{% %maxDepth#…% %object.closeBracket#}%%property.after#,%␊ + %diffGutters.actual#- % %object.closeBracket#}%%item.after#,%␊ + %diffGutters.padding# %%list.closeBracket#]%` + +## objects: effectively resets depth when formatting differences + +> Snapshot 1 + + `%diffGutters.padding# %%object.openBracket#{%␊ + %diffGutters.padding# % a%property.separator#: %%object.ctor.open%Object%object.ctor.close% %object.openBracket#{% %maxDepth#…% %object.closeBracket#}%%property.after#,%␊ + %diffGutters.actual#- % c%property.separator#: %%object.openBracket#{%␊ + %diffGutters.actual#- % d%property.separator#: %%string.line.open#'%%string.open%bar%string.close%%string.line.close#'%%property.after#,%␊ + %diffGutters.actual#- % e%property.separator#: %%object.ctor.open%Object%object.ctor.close% %object.openBracket#{% %maxDepth#…% %object.closeBracket#}%%property.after#,%␊ + %diffGutters.actual#- % %object.closeBracket#}%%property.after#,%␊ + %diffGutters.padding# %%object.closeBracket#}%` diff --git a/test/snapshots/diff.js.snap b/test/snapshots/diff.js.snap index c0f50db..7edc6c4 100644 Binary files a/test/snapshots/diff.js.snap and b/test/snapshots/diff.js.snap differ