Skip to content

Commit

Permalink
Replace core with unist-util-visit-parents
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 23, 2018
1 parent 8a04bbb commit 332a512
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 132 deletions.
54 changes: 9 additions & 45 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

module.exports = visit

var is = require('unist-util-is')
var visitParents = require('unist-util-visit-parents')

var CONTINUE = true
var SKIP = 'skip'
var EXIT = false
var CONTINUE = visitParents.CONTINUE
var SKIP = visitParents.SKIP
var EXIT = visitParents.EXIT

visit.CONTINUE = CONTINUE
visit.SKIP = SKIP
Expand All @@ -19,47 +19,11 @@ function visit(tree, test, visitor, reverse) {
test = null
}

one(tree)
visitParents(tree, test, overload, reverse)

/* Visit a single node. */
function one(node, index, parent) {
var result

index = index || (parent ? 0 : null)

if (!test || node.type === test || is(test, node, index, parent || null)) {
result = visitor(node, index, parent || null)
}

if (result === EXIT) {
return result
}

if (node.children && result !== SKIP) {
return all(node.children, node) === EXIT ? EXIT : result
}

return result
}

/* Visit children in `parent`. */
function all(children, parent) {
var step = reverse ? -1 : 1
var index = (reverse ? children.length : -1) + step
var child
var result

while (index > -1 && index < children.length) {
child = children[index]
result = child && one(child, index, parent)

if (result === EXIT) {
return result
}

index = typeof result === 'number' ? result : index + step
}

return CONTINUE
function overload(node, parents) {
var parent = parents[parents.length - 1]
var index = parent ? parent.children.indexOf(node) : null
return visitor(node, index, parent)
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"index.js"
],
"dependencies": {
"unist-util-is": "^2.1.1"
"unist-util-visit-parents": "^2.0.0"
},
"devDependencies": {
"browserify": "^16.0.0",
Expand Down
129 changes: 43 additions & 86 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ var STOP = 5
var SKIP = 7
var SKIP_REVERSE = 6

var textNodes = 6
var codeNodes = 1
var texts = 6
var codes = 1

var types = [
'root',
Expand Down Expand Up @@ -63,92 +63,77 @@ test('unist-util-visit', function(t) {
t.test('should iterate over all nodes', function(st) {
var n = 0

st.doesNotThrow(function() {
visit(tree, visitor)
}, 'should visit all nodes (#1)')
visit(tree, visitor)

st.equal(n, types.length, 'should visit all nodes (#2)')
st.equal(n, types.length, 'should visit all nodes')

st.end()

function visitor(node) {
assert.equal(node.type, types[n++], 'should be the expected type')
assert.equal(node.type, types[n], 'should be the expected type')
n++
}
})

t.test('should iterate over all nodes, backwards', function(st) {
var n = 0

st.doesNotThrow(function() {
visit(tree, visitor, true)
}, 'should visit all nodes in reverse (#1)')
visit(tree, visitor, true)

st.equal(n, reverseTypes.length, 'should visit all nodes in reverse (#2)')
st.equal(n, reverseTypes.length, 'should visit all nodes in reverse')

st.end()

function visitor(node) {
assert.equal(node.type, reverseTypes[n++], 'should be the expected type')
assert.equal(node.type, reverseTypes[n], 'should be the expected type')
n++
}
})

t.test('should only visit a given `type`', function(st) {
var n = 0

st.doesNotThrow(function() {
visit(tree, 'text', visitor)
}, 'should visit all matching nodes (#1)')
visit(tree, 'text', visitor)

st.equal(n, textNodes, 'should visit all matching nodes (#2)')
st.equal(n, texts, 'should visit all matching nodes')

st.end()

function visitor(node) {
n++
assert.equal(node.type, 'text', 'should be the expected type')
n++
}
})

t.test('should only visit given `type`s', function(st) {
var n = 0
var types = ['text', 'inlineCode']
var n = 0

st.doesNotThrow(function() {
visit(tree, types, visitor)
}, 'should visit all matching nodes (#1)')
visit(tree, types, visitor)

st.equal(n, textNodes + codeNodes, 'should visit all matching nodes (#2)')
st.equal(n, texts + codes, 'should visit all matching nodes')

st.end()

function visitor(node) {
n++
assert.notEqual(
types.indexOf(node.type),
-1,
'should be a requested type: ' + node.type
)
assert.notEqual(types.indexOf(node.type), -1, 'should match')
}
})

t.test('should accept any `is`-compatible test function', function(st) {
var n = 0

st.doesNotThrow(function() {
visit(tree, test, visitor)
}, 'should visit all passing nodes (#1)')
visit(tree, test, visitor)

st.equal(n, 3, 'should visit all passing nodes (#2)')
st.equal(n, 3, 'should visit all passing nodes')

st.end()

function visitor(node, index, parent) {
var parentType = parent && parent.type
var info = '(' + (parent && parent.type) + ':' + index + ')'
assert.ok(test(node, index), 'should be a requested node ' + info)
n++
assert.ok(
index > 3,
'should be a requested node (' + parentType + ':' + index + ')'
)
}

function test(node, index) {
Expand All @@ -157,28 +142,20 @@ test('unist-util-visit', function(t) {
})

t.test('should accept an array of `is`-compatible tests', function(st) {
var n = 0
var expected = ['root', 'paragraph', 'emphasis', 'strong']
var tests = [test, 'paragraph', {value: '.'}, ['emphasis', 'strong']]
var n = 0

st.doesNotThrow(function() {
visit(
tree,
[test, 'paragraph', {value: '.'}, ['emphasis', 'strong']],
visitor
)
}, 'should visit all passing nodes (#1)')
visit(tree, tests, visitor)

st.equal(n, 5, 'should visit all passing nodes (#2)')
st.equal(n, 5, 'should visit all passing nodes')

st.end()

function visitor(node) {
var ok = expected.indexOf(node.type) !== -1 || node.value === '.'
assert.ok(ok, 'should be a requested type: ' + node.type)
n++

assert.ok(
expected.indexOf(node.type) !== -1 || node.value === '.',
'should be a requested type: ' + node.type
)
}

function test(node) {
Expand All @@ -189,55 +166,43 @@ test('unist-util-visit', function(t) {
t.test('should stop if `visitor` stops', function(st) {
var n = 0

st.doesNotThrow(function() {
visit(tree, visitor)
}, 'should visit nodes until `visit.EXIT` is given (#1)')
visit(tree, visitor)

st.equal(n, STOP, 'should visit nodes until `visit.EXIT` is given (#2)')
st.equal(n, STOP, 'should visit nodes until `visit.EXIT` is given')

st.end()

function visitor(node) {
assert.equal(node.type, types[n++], 'should be the expected type')

if (n === STOP) {
return visit.EXIT
}
return n === STOP ? visit.EXIT : visit.CONTINUE
}
})

t.test('should stop if `visitor` stops, backwards', function(st) {
var n = 0

st.doesNotThrow(function() {
visit(tree, visitor, true)
}, 'should visit nodes until `visit.EXIT` is given (#1)')
visit(tree, visitor, true)

st.equal(n, STOP, 'should visit nodes until `visit.EXIT` is given (#2)')
st.equal(n, STOP, 'should visit nodes until `visit.EXIT` is given')

st.end()

function visitor(node) {
assert.equal(node.type, reverseTypes[n++], 'should be the expected type')

if (n === STOP) {
return visit.EXIT
}
return n === STOP ? visit.EXIT : visit.CONTINUE
}
})

t.test('should skip if `visitor` skips', function(st) {
var n = 0
var count = 0

st.doesNotThrow(function() {
visit(tree, visitor)
}, 'should visit nodes except when `visit.SKIP` is given (#1)')
visit(tree, visitor)

st.equal(
count,
types.length - 1,
'should visit nodes except when `visit.SKIP` is given (#2)'
'should visit nodes except when `visit.SKIP` is given'
)

st.end()
Expand All @@ -257,14 +222,12 @@ test('unist-util-visit', function(t) {
var n = 0
var count = 0

st.doesNotThrow(function() {
visit(tree, visitor, true)
}, 'should visit nodes except when `visit.SKIP` is given (#1)')
visit(tree, visitor, true)

st.equal(
count,
reverseTypes.length - 1,
'should visit nodes except when `visit.SKIP` is given (#2)'
'should visit nodes except when `visit.SKIP` is given'
)

st.end()
Expand Down Expand Up @@ -305,11 +268,9 @@ test('unist-util-visit', function(t) {
'text'
]

st.doesNotThrow(function() {
visit(tree, visitor)
}, 'should visit nodes again (#1)')
visit(tree, visitor)

st.equal(n, expected.length, 'should visit nodes again (#2)')
st.equal(n, expected.length, 'should visit nodes again')

st.end()

Expand Down Expand Up @@ -340,11 +301,9 @@ test('unist-util-visit', function(t) {
'text'
]

st.doesNotThrow(function() {
visit(tree, visitor)
}, 'should skip nodes (#1)')
visit(tree, visitor)

st.equal(n, expected.length, 'should skip nodes (#2)')
st.equal(n, expected.length, 'should skip nodes')

st.end()

Expand Down Expand Up @@ -377,11 +336,9 @@ test('unist-util-visit', function(t) {
'text'
]

st.doesNotThrow(function() {
visit(tree, visitor)
}, 'should skip nodes (#1)')
visit(tree, visitor)

st.equal(n, expected.length, 'should skip nodes (#2)')
st.equal(n, expected.length, 'should skip nodes')

st.end()

Expand Down

0 comments on commit 332a512

Please sign in to comment.