Skip to content

Commit

Permalink
fix: attach comment to CtLiteral nodes (#4836)
Browse files Browse the repository at this point in the history
  • Loading branch information
algomaster99 authored Aug 15, 2022
1 parent ae305c3 commit 6a95f50
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import spoon.reflect.code.CtConditional;
import spoon.reflect.code.CtIf;
import spoon.reflect.code.CtLambda;
import spoon.reflect.code.CtLiteral;
import spoon.reflect.code.CtNewArray;
import spoon.reflect.code.CtStatement;
import spoon.reflect.code.CtStatementList;
Expand Down Expand Up @@ -434,6 +435,11 @@ public void visitCtIf(CtIf e) {
}
}

@Override
public <T> void visitCtLiteral(CtLiteral<T> e) {
e.addComment(comment);
}

@Override
public void scanCtStatement(CtStatement s) {
if (!(s instanceof CtStatementList || s instanceof CtSwitch || s instanceof CtVariable)) {
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/spoon/reflect/ast/AstCheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import spoon.reflect.declaration.CtClass;
import spoon.reflect.reference.CtExecutableReference;
import spoon.support.modelobs.FineModelChangeListener;
import spoon.reflect.CtModel;
import spoon.reflect.code.CtBinaryOperator;
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtComment;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtFieldRead;
import spoon.reflect.code.CtIf;
Expand All @@ -42,6 +44,7 @@
import spoon.support.comparator.CtLineElementComparator;
import spoon.support.util.internal.ElementNameMap;
import spoon.support.util.ModelList;
import spoon.testing.utils.ModelTest;

import java.util.Arrays;
import java.util.Collection;
Expand All @@ -57,6 +60,16 @@

public class AstCheckerTest {

@ModelTest("src/test/resources/comment/CommentsOnCaseExpression.java")
void ctLiteralsInCtCaseExpressionShouldHaveCommentsAttached(CtModel model) {
// contract: literal nodes should have comments attached to them.
// act
List<CtComment> comments = model.getElements(new TypeFilter<>(CtComment.class));

// assert
assertThat(comments.size(), equalTo(4));
}

@Test
void leftOperandShouldBeGivenPriorityForStoringTheNestedOperator_stringLiteralConcatenation() {
// contract: string concatenation should be left associative.
Expand Down
20 changes: 20 additions & 0 deletions src/test/resources/comment/CommentsOnCaseExpression.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class CommentsOnCaseExpression {
public void commentsShouldBeAttachedToCtLiteral(int stage) {
String stageStr;
boolean fullStatus;
switch (stage) {
// Inline comments are also a part now
case (1/*org.apache.coyote.Constants.STAGE_PARSE*/):
stageStr = "P";
fullStatus = false;
break;
case (2/*org.apache.coyote.Constants.STAGE_PREPARE*/):
stageStr = "P";
fullStatus = false;
break;
case (3/*org.apache.coyote.Constants.STAGE_SERVICE*/):
stageStr = "S";
break;
}
}
}

0 comments on commit 6a95f50

Please sign in to comment.