Skip to content

Commit

Permalink
Merge pull request #2916 from jlerbsc/master
Browse files Browse the repository at this point in the history
Fix issue 2578 Orphaned Comments exist but not printed on unrelated change to AST
  • Loading branch information
jlerbsc committed Nov 13, 2020
2 parents 2e55995 + 00fcc40 commit 89ec332
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,31 @@

package com.github.javaparser.printer;

import static com.github.javaparser.ParseStart.COMPILATION_UNIT;
import static com.github.javaparser.ParserConfiguration.LanguageLevel.JAVA_9;
import static com.github.javaparser.Providers.provider;
import static com.github.javaparser.StaticJavaParser.parse;
import static com.github.javaparser.StaticJavaParser.parseBodyDeclaration;
import static com.github.javaparser.StaticJavaParser.parseStatement;
import static com.github.javaparser.printer.PrettyPrinterConfiguration.IndentType.TABS;
import static com.github.javaparser.printer.PrettyPrinterConfiguration.IndentType.TABS_WITH_SPACE_ALIGN;
import static com.github.javaparser.utils.TestUtils.assertEqualsStringIgnoringEol;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseProblemException;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.TypeDeclaration;
import com.github.javaparser.ast.expr.VariableDeclarationExpr;
import com.github.javaparser.ast.stmt.Statement;
import com.github.javaparser.ast.type.PrimitiveType;
import org.junit.jupiter.api.Test;

import static com.github.javaparser.ParseStart.COMPILATION_UNIT;
import static com.github.javaparser.ParserConfiguration.LanguageLevel.JAVA_9;
import static com.github.javaparser.Providers.provider;
import static com.github.javaparser.StaticJavaParser.*;
import static com.github.javaparser.printer.PrettyPrinterConfiguration.IndentType.TABS;
import static com.github.javaparser.printer.PrettyPrinterConfiguration.IndentType.TABS_WITH_SPACE_ALIGN;
import static com.github.javaparser.utils.TestUtils.assertEqualsStringIgnoringEol;
import static org.junit.jupiter.api.Assertions.assertEquals;

class PrettyPrinterTest {

Expand Down Expand Up @@ -471,4 +476,19 @@ void printAnnotationsAtPrettyPlaces() {
"module foo.bar {\n" +
"}\n", printed);
}

@Test
public void testIssue2578() {
String code =
"class C{\n" +
" //orphan\n" +
" /*orphan*/\n" +
"}";
CompilationUnit cu = StaticJavaParser.parse(code);
TypeDeclaration td = cu.findFirst(TypeDeclaration.class).get();
assertEquals(2, td.getAllContainedComments().size());
td.setPublic(true); // --- simple AST change -----
System.out.println(cu.toString()); // orphan and /*orphan*/ must be printed
assertEquals(2, td.getAllContainedComments().size()); // the orphaned comments exist
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import com.github.javaparser.ast.ArrayCreationLevel;
import com.github.javaparser.ast.CompilationUnit;
Expand Down Expand Up @@ -1904,7 +1905,8 @@ private void printOrphanCommentsBeforeThisChildNode(final Node node) {
private void printOrphanCommentsEnding(final Node node) {
if (configuration.isIgnoreComments()) return;

List<Node> everything = new ArrayList<>(node.getChildNodes());
// extract all nodes for which the position/range is indicated to avoid to skip orphan comments
List<Node> everything = node.getChildNodes().stream().filter(n->n.getRange().isPresent()).collect(Collectors.toList());
sortByBeginPosition(everything);
if (everything.isEmpty()) {
return;
Expand Down

0 comments on commit 89ec332

Please sign in to comment.