Skip to content

Commit

Permalink
Assert when inserting an individual Node using addChildrenToFront fun…
Browse files Browse the repository at this point in the history
…ction, which leads to inconsistent AST tree.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=185437399
  • Loading branch information
SergejSalnikov authored and brad4d committed Feb 13, 2018
1 parent 8f8b4b0 commit cbd51f1
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/com/google/javascript/rhino/Node.java
Expand Up @@ -745,10 +745,21 @@ public final void addChildToBack(Node child) {
child.parent = this;
}

public final void addChildrenToFront(Node children) {
/**
* Add all children to the front of this node.
*
* @param children first of a list of sibling nodes who have no parent.
* NOTE: Usually you would get this argument from a removeChildren() call.
* A single detached node will not work because its sibling pointers will not be
* correctly initialized.
*/
public final void addChildrenToFront(@Nullable Node children) {
if (children == null) {
return; // removeChildren() returns null when there are none
}
// NOTE: If there is only one sibling, its previous pointer must point to itself.
// Null indicates a fully detached node.
checkNotNull(children.previous, children);
for (Node child = children; child != null; child = child.next) {
checkArgument(child.parent == null);
child.parent = this;
Expand Down Expand Up @@ -803,13 +814,20 @@ public final void addChildAfter(Node newChild, @Nullable Node node) {

/**
* Add all children after 'node'. If 'node' is null, add them to the front of this node.
*
* @param children first of a list of sibling nodes who have no parent.
* NOTE: Usually you would get this argument from a removeChildren() call.
* A single detached node will not work because its sibling pointers will not be
* correctly initialized.
*/
public final void addChildrenAfter(@Nullable Node children, @Nullable Node node) {
if (children == null) {
return; // removeChildren() returns null when there are none
}
checkArgument(node == null || node.parent == this);
checkNotNull(children.previous);
// NOTE: If there is only one sibling, its previous pointer must point to itself.
// Null indicates a fully detached node.
checkNotNull(children.previous, children);
if (node == null) {
addChildrenToFront(children);
return;
Expand Down

0 comments on commit cbd51f1

Please sign in to comment.