Skip to content
This repository was archived by the owner on Aug 24, 2019. It is now read-only.

Commit 0ff5472

Browse files
committed
fix: fix remove and replace
1 parent 6be2b9f commit 0ff5472

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

packages/h2x-traverse/src/NodePath.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class NodePath {
5353
this.shouldStop = true
5454
this.node = node
5555
this.container[this.key] = node
56-
this.context.visit(this.container, this.key)
56+
this.requeue()
5757
}
5858

5959
remove() {
@@ -68,6 +68,11 @@ class NodePath {
6868
this.container[this.key] = null
6969
}
7070
this.node = null
71+
this.requeue()
72+
}
73+
74+
requeue() {
75+
this.context.visit(this.parent, this.listKey)
7176
}
7277

7378
_call(fns) {

packages/h2x-traverse/src/TraversalContext.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,9 @@ class TraversalContext {
2020
if (container.length === 0) return false
2121
let shouldStop = false
2222

23-
const paths = []
24-
2523
Array.from(container).forEach((value, key) => {
24+
if (shouldStop) return
2625
const nodePath = this.create(parent, container, key, listKey)
27-
paths.push(nodePath)
28-
})
29-
30-
paths.forEach(nodePath => {
3126
if (nodePath && nodePath.visit()) shouldStop = true
3227
})
3328

packages/h2x-traverse/src/index.test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ describe('traverse', () => {
112112
if (path.node.tagName === 'DIV') {
113113
path.replace(path.node.ownerDocument.createElement('header'))
114114
}
115+
116+
// Bug with visiting container instead of parent
117+
expect(path.container !== path.parent).toBe(true)
115118
})
116119
const ast = parse(`<div foo bar></div>`)
117120
traverse(ast, {
@@ -130,13 +133,13 @@ describe('traverse', () => {
130133
}
131134
})
132135

133-
const ast = parse(`<header><div></div><span></span></header>`)
136+
const ast = parse(`<header><div></div><span></span><span></span></header>`)
134137

135138
traverse(ast, { enter })
136-
expect(enter).toHaveBeenCalledTimes(3)
139+
expect(enter).toHaveBeenCalledTimes(4)
137140

138141
traverse(ast, { enter })
139-
expect(enter).toHaveBeenCalledTimes(5)
142+
expect(enter).toHaveBeenCalledTimes(7)
140143
})
141144

142145
it('should be possible to remove attribute', () => {

0 commit comments

Comments
 (0)