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

Commit

Permalink
fix: fix remove and replace
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Sep 1, 2017
1 parent 6be2b9f commit 0ff5472
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
7 changes: 6 additions & 1 deletion packages/h2x-traverse/src/NodePath.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class NodePath {
this.shouldStop = true
this.node = node
this.container[this.key] = node
this.context.visit(this.container, this.key)
this.requeue()
}

remove() {
Expand All @@ -68,6 +68,11 @@ class NodePath {
this.container[this.key] = null
}
this.node = null
this.requeue()
}

requeue() {
this.context.visit(this.parent, this.listKey)
}

_call(fns) {
Expand Down
7 changes: 1 addition & 6 deletions packages/h2x-traverse/src/TraversalContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,9 @@ class TraversalContext {
if (container.length === 0) return false
let shouldStop = false

const paths = []

Array.from(container).forEach((value, key) => {
if (shouldStop) return
const nodePath = this.create(parent, container, key, listKey)
paths.push(nodePath)
})

paths.forEach(nodePath => {
if (nodePath && nodePath.visit()) shouldStop = true
})

Expand Down
9 changes: 6 additions & 3 deletions packages/h2x-traverse/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ describe('traverse', () => {
if (path.node.tagName === 'DIV') {
path.replace(path.node.ownerDocument.createElement('header'))
}

// Bug with visiting container instead of parent
expect(path.container !== path.parent).toBe(true)
})
const ast = parse(`<div foo bar></div>`)
traverse(ast, {
Expand All @@ -130,13 +133,13 @@ describe('traverse', () => {
}
})

const ast = parse(`<header><div></div><span></span></header>`)
const ast = parse(`<header><div></div><span></span><span></span></header>`)

traverse(ast, { enter })
expect(enter).toHaveBeenCalledTimes(3)
expect(enter).toHaveBeenCalledTimes(4)

traverse(ast, { enter })
expect(enter).toHaveBeenCalledTimes(5)
expect(enter).toHaveBeenCalledTimes(7)
})

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

0 comments on commit 0ff5472

Please sign in to comment.