]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-20958 Improve error message for startColumn on empty line (#10628)
authorDejan Milisavljevic <130993898+dejan-milisavljevic-sonarsource@users.noreply.github.com>
Thu, 15 Feb 2024 15:46:48 +0000 (16:46 +0100)
committersonartech <sonartech@sonarsource.com>
Thu, 15 Feb 2024 20:02:34 +0000 (20:02 +0000)
sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/ExternalIssueImporter.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/ExternalIssueImporterTest.java

index e76122628b4687780ca7c019f55926d352aa6241..848a54c12781ed2738f4c229c3087bc638159713 100644 (file)
@@ -44,6 +44,8 @@ import org.sonar.scanner.externalissue.ExternalIssueReport.Issue;
 import org.sonar.scanner.externalissue.ExternalIssueReport.Location;
 import org.sonar.scanner.externalissue.ExternalIssueReport.Rule;
 
+import static java.lang.String.format;
+
 public class ExternalIssueImporter {
   private static final Logger LOG = LoggerFactory.getLogger(ExternalIssueImporter.class);
   private static final int MAX_UNKNOWN_FILE_PATHS_TO_PRINT = 5;
@@ -197,6 +199,7 @@ public class ExternalIssueImporter {
     if (location.textRange != null) {
       if (location.textRange.startColumn != null) {
         TextPointer start = file.newPointer(location.textRange.startLine, location.textRange.startColumn);
+        checkStartColumnOnEmptyLine(file, start);
         int endLine = (location.textRange.endLine != null) ? location.textRange.endLine : location.textRange.startLine;
         int endColumn;
 
@@ -211,6 +214,12 @@ public class ExternalIssueImporter {
     return newLocation;
   }
 
+  private static void checkStartColumnOnEmptyLine(InputFile file, TextPointer startPointer) {
+    if (file.selectLine(startPointer.line()).end().lineOffset() == 0) {
+      throw new IllegalArgumentException(format("A 'startColumn' %s cannot be provided when the line is empty", startPointer));
+    }
+  }
+
   @CheckForNull
   private static InputFile findFile(SensorContext context, String filePath) {
     return context.fileSystem().inputFile(context.fileSystem().predicates().hasPath(filePath));
index be0d139b4cda8e2aaa120626c92b11ad4546b366..549a8517a164fa70b7f7989727d2c9db08c161ad 100644 (file)
@@ -71,7 +71,7 @@ public class ExternalIssueImporterTest {
     context = SensorContextTester.create(baseDir);
     sourceFile = new TestInputFileBuilder("foo", "src/Foo.java")
       .setModuleBaseDir(baseDir.toPath())
-      .initMetadata("the first line\nthe second line")
+      .initMetadata("the first line\nthe second line\n\n")
       .setCharset(UTF_8)
       .setLanguage("java")
       .build();
@@ -173,6 +173,18 @@ public class ExternalIssueImporterTest {
     assertThat(got.end().lineOffset()).isEqualTo(sourceFile.selectLine(input.endLine).end().lineOffset());
   }
 
+  @Test
+  public void execute_whenNewFormatWithStartColumnOnEmptyLine_shouldThrowException() {
+    ExternalIssueReport.TextRange input = new ExternalIssueReport.TextRange();
+    input.startLine = 3;
+    input.startColumn = 0;
+
+    ExternalIssueReport.Issue issue = newIssue(input);
+    assertThatThrownBy(() -> runOn(issue))
+      .isInstanceOf(IllegalArgumentException.class)
+      .hasMessage("A 'startColumn' [line=3, lineOffset=0] cannot be provided when the line is empty");
+  }
+
   @Test
   public void execute_whenNewFormatContainsNonExistentCleanCodeAttribute_shouldThrowException() {
     ExternalIssueReport report = new ExternalIssueReport();
@@ -304,6 +316,19 @@ public class ExternalIssueImporterTest {
     assertThat(got.end().lineOffset()).isEqualTo(sourceFile.selectLine(input.endLine).end().lineOffset());
   }
 
+  @Test
+  public void execute_whenDeprecatedFormatWithStartColumnOnEmptyLine_shouldThrowException() {
+    ExternalIssueReport.TextRange input = new ExternalIssueReport.TextRange();
+    input.startLine = 3;
+    input.startColumn = 0;
+
+    ExternalIssueReport.Issue issue = newIssue(input);
+    assertThatThrownBy(() -> runOnDeprecatedFormat(issue))
+      .isInstanceOf(IllegalArgumentException.class)
+      .hasMessage("A 'startColumn' [line=3, lineOffset=0] cannot be provided when the line is empty");
+  }
+
+
   private static ExternalIssueReport.Rule createRule() {
     return createRule(RULE_ATTRIBUTE.name(), SECURITY.name(), HIGH.name());
   }