Skip to content

Commit e17b9ad

Browse files
SONARJAVA-4682 Fix quickfix edits parsing
Fix a problem where the expectations parser would pick the first pair of closing accolades "}}" in the edit as the end of the quick fix edit preventing the use of these characters as part of the edits.
1 parent eca46bd commit e17b9ad

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

java-checks-testkit/src/main/java/org/sonar/java/checks/verifier/internal/Expectations.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,15 @@ private static Map.Entry<IssueAttribute, Object> parseAttribute(String attribute
645645
}
646646

647647
private static String parseMessage(String cleanedComment, int horizon) {
648-
return StringUtils.substringBetween(cleanedComment.substring(0, horizon), "{{", "}}");
648+
String delimitedComment = cleanedComment.substring(0, horizon);
649+
int firstIndex = delimitedComment.indexOf("{{");
650+
if (firstIndex != -1) {
651+
int lastIndex = delimitedComment.lastIndexOf("}}");
652+
if (lastIndex != -1) {
653+
return delimitedComment.substring(firstIndex + 2, lastIndex);
654+
}
655+
}
656+
return null;
649657
}
650658

651659
static class ParsedComment {

java-checks-testkit/src/test/java/org/sonar/java/checks/verifier/internal/ExpectationsTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,15 @@ private void assertJavaQuickFix(JavaQuickFix quickFix, String editTextSpan) {
374374
assertThat(textSpan).hasToString(editTextSpan);
375375
}
376376

377+
@Test
378+
void quick_fix_edit_fails_without_end_delimiter() {
379+
parser.parseIssue("// Noncompliant@ [[sc=3;ec=10;quickfixes=qf1]]", TEST_LINE);
380+
parser.parseQuickFix("// fix@qf1 {{It goes like this:}}", TEST_LINE + 1);
381+
parser.parseQuickFix("// edit@qf1 [[sc=5;ec=10]] {{boom}", TEST_LINE + 2);
382+
assertThatThrownBy(() -> parser.consolidateQuickFixes())
383+
.isInstanceOf(AssertionError.class);
384+
}
385+
377386
@Test
378387
void quick_fix_without_message() {
379388
parser.parseIssue("// Noncompliant@+1 [[sc=5;ec=10;quickfixes=qf1]]", TEST_LINE);

0 commit comments

Comments
 (0)