Skip to content

Commit 332a512

Browse files
committed
Replace core with unist-util-visit-parents
1 parent 8a04bbb commit 332a512

File tree

3 files changed

+53
-132
lines changed

3 files changed

+53
-132
lines changed

index.js

+9-45
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
module.exports = visit
44

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

7-
var CONTINUE = true
8-
var SKIP = 'skip'
9-
var EXIT = false
7+
var CONTINUE = visitParents.CONTINUE
8+
var SKIP = visitParents.SKIP
9+
var EXIT = visitParents.EXIT
1010

1111
visit.CONTINUE = CONTINUE
1212
visit.SKIP = SKIP
@@ -19,47 +19,11 @@ function visit(tree, test, visitor, reverse) {
1919
test = null
2020
}
2121

22-
one(tree)
22+
visitParents(tree, test, overload, reverse)
2323

24-
/* Visit a single node. */
25-
function one(node, index, parent) {
26-
var result
27-
28-
index = index || (parent ? 0 : null)
29-
30-
if (!test || node.type === test || is(test, node, index, parent || null)) {
31-
result = visitor(node, index, parent || null)
32-
}
33-
34-
if (result === EXIT) {
35-
return result
36-
}
37-
38-
if (node.children && result !== SKIP) {
39-
return all(node.children, node) === EXIT ? EXIT : result
40-
}
41-
42-
return result
43-
}
44-
45-
/* Visit children in `parent`. */
46-
function all(children, parent) {
47-
var step = reverse ? -1 : 1
48-
var index = (reverse ? children.length : -1) + step
49-
var child
50-
var result
51-
52-
while (index > -1 && index < children.length) {
53-
child = children[index]
54-
result = child && one(child, index, parent)
55-
56-
if (result === EXIT) {
57-
return result
58-
}
59-
60-
index = typeof result === 'number' ? result : index + step
61-
}
62-
63-
return CONTINUE
24+
function overload(node, parents) {
25+
var parent = parents[parents.length - 1]
26+
var index = parent ? parent.children.indexOf(node) : null
27+
return visitor(node, index, parent)
6428
}
6529
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"index.js"
2929
],
3030
"dependencies": {
31-
"unist-util-is": "^2.1.1"
31+
"unist-util-visit-parents": "^2.0.0"
3232
},
3333
"devDependencies": {
3434
"browserify": "^16.0.0",

test.js

+43-86
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ var STOP = 5
1111
var SKIP = 7
1212
var SKIP_REVERSE = 6
1313

14-
var textNodes = 6
15-
var codeNodes = 1
14+
var texts = 6
15+
var codes = 1
1616

1717
var types = [
1818
'root',
@@ -63,92 +63,77 @@ test('unist-util-visit', function(t) {
6363
t.test('should iterate over all nodes', function(st) {
6464
var n = 0
6565

66-
st.doesNotThrow(function() {
67-
visit(tree, visitor)
68-
}, 'should visit all nodes (#1)')
66+
visit(tree, visitor)
6967

70-
st.equal(n, types.length, 'should visit all nodes (#2)')
68+
st.equal(n, types.length, 'should visit all nodes')
7169

7270
st.end()
7371

7472
function visitor(node) {
75-
assert.equal(node.type, types[n++], 'should be the expected type')
73+
assert.equal(node.type, types[n], 'should be the expected type')
74+
n++
7675
}
7776
})
7877

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

82-
st.doesNotThrow(function() {
83-
visit(tree, visitor, true)
84-
}, 'should visit all nodes in reverse (#1)')
81+
visit(tree, visitor, true)
8582

86-
st.equal(n, reverseTypes.length, 'should visit all nodes in reverse (#2)')
83+
st.equal(n, reverseTypes.length, 'should visit all nodes in reverse')
8784

8885
st.end()
8986

9087
function visitor(node) {
91-
assert.equal(node.type, reverseTypes[n++], 'should be the expected type')
88+
assert.equal(node.type, reverseTypes[n], 'should be the expected type')
89+
n++
9290
}
9391
})
9492

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

98-
st.doesNotThrow(function() {
99-
visit(tree, 'text', visitor)
100-
}, 'should visit all matching nodes (#1)')
96+
visit(tree, 'text', visitor)
10197

102-
st.equal(n, textNodes, 'should visit all matching nodes (#2)')
98+
st.equal(n, texts, 'should visit all matching nodes')
10399

104100
st.end()
105101

106102
function visitor(node) {
107-
n++
108103
assert.equal(node.type, 'text', 'should be the expected type')
104+
n++
109105
}
110106
})
111107

112108
t.test('should only visit given `type`s', function(st) {
113-
var n = 0
114109
var types = ['text', 'inlineCode']
110+
var n = 0
115111

116-
st.doesNotThrow(function() {
117-
visit(tree, types, visitor)
118-
}, 'should visit all matching nodes (#1)')
112+
visit(tree, types, visitor)
119113

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

122116
st.end()
123117

124118
function visitor(node) {
125119
n++
126-
assert.notEqual(
127-
types.indexOf(node.type),
128-
-1,
129-
'should be a requested type: ' + node.type
130-
)
120+
assert.notEqual(types.indexOf(node.type), -1, 'should match')
131121
}
132122
})
133123

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

137-
st.doesNotThrow(function() {
138-
visit(tree, test, visitor)
139-
}, 'should visit all passing nodes (#1)')
127+
visit(tree, test, visitor)
140128

141-
st.equal(n, 3, 'should visit all passing nodes (#2)')
129+
st.equal(n, 3, 'should visit all passing nodes')
142130

143131
st.end()
144132

145133
function visitor(node, index, parent) {
146-
var parentType = parent && parent.type
134+
var info = '(' + (parent && parent.type) + ':' + index + ')'
135+
assert.ok(test(node, index), 'should be a requested node ' + info)
147136
n++
148-
assert.ok(
149-
index > 3,
150-
'should be a requested node (' + parentType + ':' + index + ')'
151-
)
152137
}
153138

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

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

163-
st.doesNotThrow(function() {
164-
visit(
165-
tree,
166-
[test, 'paragraph', {value: '.'}, ['emphasis', 'strong']],
167-
visitor
168-
)
169-
}, 'should visit all passing nodes (#1)')
149+
visit(tree, tests, visitor)
170150

171-
st.equal(n, 5, 'should visit all passing nodes (#2)')
151+
st.equal(n, 5, 'should visit all passing nodes')
172152

173153
st.end()
174154

175155
function visitor(node) {
156+
var ok = expected.indexOf(node.type) !== -1 || node.value === '.'
157+
assert.ok(ok, 'should be a requested type: ' + node.type)
176158
n++
177-
178-
assert.ok(
179-
expected.indexOf(node.type) !== -1 || node.value === '.',
180-
'should be a requested type: ' + node.type
181-
)
182159
}
183160

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

192-
st.doesNotThrow(function() {
193-
visit(tree, visitor)
194-
}, 'should visit nodes until `visit.EXIT` is given (#1)')
169+
visit(tree, visitor)
195170

196-
st.equal(n, STOP, 'should visit nodes until `visit.EXIT` is given (#2)')
171+
st.equal(n, STOP, 'should visit nodes until `visit.EXIT` is given')
197172

198173
st.end()
199174

200175
function visitor(node) {
201176
assert.equal(node.type, types[n++], 'should be the expected type')
202-
203-
if (n === STOP) {
204-
return visit.EXIT
205-
}
177+
return n === STOP ? visit.EXIT : visit.CONTINUE
206178
}
207179
})
208180

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

212-
st.doesNotThrow(function() {
213-
visit(tree, visitor, true)
214-
}, 'should visit nodes until `visit.EXIT` is given (#1)')
184+
visit(tree, visitor, true)
215185

216-
st.equal(n, STOP, 'should visit nodes until `visit.EXIT` is given (#2)')
186+
st.equal(n, STOP, 'should visit nodes until `visit.EXIT` is given')
217187

218188
st.end()
219189

220190
function visitor(node) {
221191
assert.equal(node.type, reverseTypes[n++], 'should be the expected type')
222-
223-
if (n === STOP) {
224-
return visit.EXIT
225-
}
192+
return n === STOP ? visit.EXIT : visit.CONTINUE
226193
}
227194
})
228195

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

233-
st.doesNotThrow(function() {
234-
visit(tree, visitor)
235-
}, 'should visit nodes except when `visit.SKIP` is given (#1)')
200+
visit(tree, visitor)
236201

237202
st.equal(
238203
count,
239204
types.length - 1,
240-
'should visit nodes except when `visit.SKIP` is given (#2)'
205+
'should visit nodes except when `visit.SKIP` is given'
241206
)
242207

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

260-
st.doesNotThrow(function() {
261-
visit(tree, visitor, true)
262-
}, 'should visit nodes except when `visit.SKIP` is given (#1)')
225+
visit(tree, visitor, true)
263226

264227
st.equal(
265228
count,
266229
reverseTypes.length - 1,
267-
'should visit nodes except when `visit.SKIP` is given (#2)'
230+
'should visit nodes except when `visit.SKIP` is given'
268231
)
269232

270233
st.end()
@@ -305,11 +268,9 @@ test('unist-util-visit', function(t) {
305268
'text'
306269
]
307270

308-
st.doesNotThrow(function() {
309-
visit(tree, visitor)
310-
}, 'should visit nodes again (#1)')
271+
visit(tree, visitor)
311272

312-
st.equal(n, expected.length, 'should visit nodes again (#2)')
273+
st.equal(n, expected.length, 'should visit nodes again')
313274

314275
st.end()
315276

@@ -340,11 +301,9 @@ test('unist-util-visit', function(t) {
340301
'text'
341302
]
342303

343-
st.doesNotThrow(function() {
344-
visit(tree, visitor)
345-
}, 'should skip nodes (#1)')
304+
visit(tree, visitor)
346305

347-
st.equal(n, expected.length, 'should skip nodes (#2)')
306+
st.equal(n, expected.length, 'should skip nodes')
348307

349308
st.end()
350309

@@ -377,11 +336,9 @@ test('unist-util-visit', function(t) {
377336
'text'
378337
]
379338

380-
st.doesNotThrow(function() {
381-
visit(tree, visitor)
382-
}, 'should skip nodes (#1)')
339+
visit(tree, visitor)
383340

384-
st.equal(n, expected.length, 'should skip nodes (#2)')
341+
st.equal(n, expected.length, 'should skip nodes')
385342

386343
st.end()
387344

0 commit comments

Comments
 (0)