From 61999c500e5bac37b73f868a954d87710c7f8574 Mon Sep 17 00:00:00 2001 From: Wojtek Wajerowicz <115081248+wojciech-wajerowicz-sonarsource@users.noreply.github.com> Date: Tue, 10 Jan 2023 10:25:09 +0100 Subject: [PATCH] SONAR-18180 Fix import of SARIF issues with empty range. --- .../externalissue/sarif/RegionMapper.java | 18 +++++++++++------- .../externalissue/sarif/RegionMapperTest.java | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/RegionMapper.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/RegionMapper.java index c84fca8e3c4..8c55a873a9d 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/RegionMapper.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/RegionMapper.java @@ -35,18 +35,22 @@ public class RegionMapper { return Optional.empty(); } int startLine = Objects.requireNonNull(region.getStartLine(), "No start line defined for the region."); - Integer endLine = region.getEndLine(); - if (endLine != null) { - int startColumn = Optional.ofNullable(region.getStartColumn()).map(RegionMapper::adjustSarifColumnIndexToSqIndex).orElse(0); - int endColumn = Optional.ofNullable(region.getEndColumn()).map(RegionMapper::adjustSarifColumnIndexToSqIndex) - .orElseGet(() -> file.selectLine(endLine).end().lineOffset()); - return Optional.of(file.newRange(startLine, startColumn, endLine, endColumn)); - } else { + int endLine = Optional.ofNullable(region.getEndLine()).orElse(startLine); + int startColumn = Optional.ofNullable(region.getStartColumn()).map(RegionMapper::adjustSarifColumnIndexToSqIndex).orElse(0); + int endColumn = Optional.ofNullable(region.getEndColumn()).map(RegionMapper::adjustSarifColumnIndexToSqIndex) + .orElseGet(() -> file.selectLine(endLine).end().lineOffset()); + if (rangeIsEmpty(startLine, endLine, startColumn, endColumn)) { return Optional.of(file.selectLine(startLine)); + } else { + return Optional.of(file.newRange(startLine, startColumn, endLine, endColumn)); } } private static int adjustSarifColumnIndexToSqIndex(int index) { return index - 1; } + + private static boolean rangeIsEmpty(int startLine, int endLine, int startColumn, int endColumn) { + return startLine == endLine && startColumn == endColumn; + } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/RegionMapperTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/RegionMapperTest.java index e480a971143..2b305833b52 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/RegionMapperTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/RegionMapperTest.java @@ -159,6 +159,20 @@ public class RegionMapperTest { assertThat(textRange.end().lineOffset()).isEqualTo(7); } + @Test + public void mapRegion_whenRangeIsEmpty_shouldSelectWholeLine() { + Region fullRegion = mockRegion(8, 8, 3, 3); + + Optional optTextRange = regionMapper.mapRegion(fullRegion, INPUT_FILE); + + assertThat(optTextRange).isPresent(); + TextRange textRange = optTextRange.get(); + assertThat(textRange.start().line()).isEqualTo(fullRegion.getStartLine()); + assertThat(textRange.start().lineOffset()).isZero(); + assertThat(textRange.end().line()).isEqualTo(fullRegion.getEndLine()); + assertThat(textRange.end().lineOffset()).isEqualTo(LINE_END_OFFSET); + } + private static Region mockRegion(@Nullable Integer startColumn, @Nullable Integer endColumn, @Nullable Integer startLine, @Nullable Integer endLine) { Region region = mock(Region.class); when(region.getStartColumn()).thenReturn(startColumn); -- 2.39.5