Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: attach comment to CtLiteral nodes #4836

Merged
merged 6 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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*/):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you improve the test case and a comment before a case, e.g.,/*org.apache.coyote.Constants.STAGE_PARSE*/1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added inline comment to add variety.

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;
}
}
}