@Test public void descendants_include_children_of_children() throws Exception { VariableStatementTree variable = parse("var a;", Kind.VARIABLE_STATEMENT); Set<JavaScriptTree> descendants = ((JavaScriptTree) variable).descendants().collect(Collectors.toSet()); IdentifierTree identifier = (IdentifierTree) variable.declaration().variables().get(0); JavaScriptTree declarationToken = (JavaScriptTree) variable.declaration().token(); JavaScriptTree identifierToken = (JavaScriptTree) identifier.identifierToken(); assertThat(descendants).contains(declarationToken, (JavaScriptTree) identifier, identifierToken); }
@Override public Stream<JavaScriptTree> descendants() { if (this.isLeaf()) { return Stream.empty(); } Stream<JavaScriptTree> kins = childrenStream().filter(Objects::nonNull) .filter(tree -> tree instanceof JavaScriptTree) .map(tree -> (JavaScriptTree) tree); for (Iterator<Tree> iterator = this.childrenIterator(); iterator.hasNext();) { Tree tree = iterator.next(); if (tree != null) { kins = Stream.concat(kins, tree.descendants()); } } return kins; }
JavaScriptTree implements Tree { @Override public Stream<JavaScriptTree> descendants() { if (this.isLeaf()) { return Stream.empty(); } Stream<JavaScriptTree> kins = childrenStream().filter(Objects::nonNull) .filter(tree -> tree instanceof JavaScriptTree) .map(tree -> (JavaScriptTree) tree); for (Iterator<Tree> iterator = this.childrenIterator(); iterator.hasNext();) { Tree tree = iterator.next(); if (tree != null) { kins = Stream.concat(kins, tree.descendants()); } } return kins; } }
JavaScriptTree implements Tree { @Override public Stream<JavaScriptTree> descendants() { if (this.isLeaf()) { return Stream.empty(); } Stream<JavaScriptTree> kins = childrenStream().filter(Objects::nonNull) .filter(tree -> tree instanceof JavaScriptTree) .map(tree -> (JavaScriptTree) tree); for (Iterator<Tree> iterator = this.childrenIterator(); iterator.hasNext();) { Tree tree = iterator.next(); if (tree != null) { kins = Stream.concat(kins, tree.descendants()); } } return kins; }  }
JavaScriptTree implements Tree { @Override public Stream<JavaScriptTree> descendants() { if (this.isLeaf()) { return Stream.empty(); } Stream<JavaScriptTree> kins = childrenStream().filter(Objects::nonNull) .filter(tree -> tree instanceof JavaScriptTree) .map(tree -> (JavaScriptTree) tree); for (Iterator<Tree> iterator = this.childrenIterator(); iterator.hasNext();) { Tree tree = iterator.next(); if (tree != null) { kins = Stream.concat(kins, tree.descendants()); } } return kins; }  @Override final boolean is(Kinds... kind); abstract Kind getKind(); abstract Iterator<Tree> childrenIterator(); boolean isLeaf(); @Override SyntaxToken lastToken(); @Override SyntaxToken firstToken(); @Override boolean isAncestorOf(Tree tree); @Override Stream<JavaScriptTree> descendants(); @Override Stream<Tree> childrenStream(); void setParent(Tree parent); @Override Tree parent(); @Override String toString(); }
JavaScriptTree implements Tree { @Override public Stream<JavaScriptTree> descendants() { if (this.isLeaf()) { return Stream.empty(); } Stream<JavaScriptTree> kins = childrenStream().filter(Objects::nonNull) .filter(tree -> tree instanceof JavaScriptTree) .map(tree -> (JavaScriptTree) tree); for (Iterator<Tree> iterator = this.childrenIterator(); iterator.hasNext();) { Tree tree = iterator.next(); if (tree != null) { kins = Stream.concat(kins, tree.descendants()); } } return kins; }  @Override final boolean is(Kinds... kind); abstract Kind getKind(); abstract Iterator<Tree> childrenIterator(); boolean isLeaf(); @Override SyntaxToken lastToken(); @Override SyntaxToken firstToken(); @Override boolean isAncestorOf(Tree tree); @Override Stream<JavaScriptTree> descendants(); @Override Stream<Tree> childrenStream(); void setParent(Tree parent); @Override Tree parent(); @Override String toString();  }