Skip to content

Commit

Permalink
Reset depth when formatting differences
Browse files Browse the repository at this point in the history
Fixes #41.

Co-authored-by: Mark Wubben <mark@novemberborn.net>
  • Loading branch information
bunysae and novemberborn committed May 11, 2020
1 parent f6a020f commit 7ee06d8
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/diff.js
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}
Expand Down
40 changes: 40 additions & 0 deletions test/diff.js
Expand Up @@ -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 }))
})
24 changes: 24 additions & 0 deletions test/snapshots/diff.js.md
Expand Up @@ -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#}%`
Binary file modified test/snapshots/diff.js.snap
Binary file not shown.

0 comments on commit 7ee06d8

Please sign in to comment.