Skip to content

Commit

Permalink
feat: Include empty comment lines in the total count of lines of comm…
Browse files Browse the repository at this point in the history
…ents
  • Loading branch information
felipebz committed May 16, 2024
1 parent b26962f commit 09b8593
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class PlSqlSquidSensorTest {

//assertThat(context.measure(key, CoreMetrics.FILES).value()).isEqualTo(1);
assertThat(context.measure(key, CoreMetrics.NCLOC).value()).isEqualTo(18)
assertThat(context.measure(key, CoreMetrics.COMMENT_LINES).value()).isEqualTo(2)
assertThat(context.measure(key, CoreMetrics.COMMENT_LINES).value()).isEqualTo(4)
assertThat(context.measure(key, CoreMetrics.COMPLEXITY).value()).isEqualTo(6)
assertThat(context.measure(key, CoreMetrics.FUNCTIONS).value()).isEqualTo(2)
assertThat(context.measure(key, CoreMetrics.STATEMENTS).value()).isEqualTo(7)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import com.felipebz.flr.api.Token
import com.felipebz.flr.api.Trivia
import org.sonar.plugins.plsqlopen.api.PlSqlGrammar
import org.sonar.plugins.plsqlopen.api.checks.PlSqlCheck
import org.sonar.plugins.plsqlopen.api.squid.PlSqlCommentAnalyzer

class MetricsVisitor : PlSqlCheck() {

Expand Down Expand Up @@ -57,16 +56,24 @@ class MetricsVisitor : PlSqlCheck() {
}

override fun visitComment(trivia: Trivia, content: String) {
var line = trivia.token.line

for (commentLine in content.lineSequence()) {
if (commentLine.contains("NOSONAR")) {
linesOfComments.remove(line)
noSonar.add(line)
} else if (!PlSqlCommentAnalyzer.isBlank(commentLine)) {
linesOfComments.add(line)
val comment = trivia.token.value
val line = trivia.token.line
val endLine = trivia.token.endLine
val firstLineContainsNoSonar = comment.indexOfAny(newlineChars).let {
if (it == -1) {
comment.contains("NOSONAR")
} else {
comment.regionMatches(0, "NOSONAR", 0, it)
}
line++
}

for (i in line .. endLine) {
linesOfComments.add(i)
}

if (firstLineContainsNoSonar) {
linesOfComments.remove(line)
noSonar.add(line)
}
}

Expand All @@ -76,4 +83,8 @@ class MetricsVisitor : PlSqlCheck() {

fun getExecutableLines(): Set<Int> = executableLines

companion object {
private val newlineChars = charArrayOf('\n', '\r')
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ package org.sonar.plugins.plsqlopen.api.squid

object PlSqlCommentAnalyzer {

fun isBlank(line: String) = line.isBlank()

fun getContents(comment: String) =
when (comment[0]) {
'-' -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class MetricsVisitorTest {
@Test
fun comments() {
TestPlSqlVisitorRunner.scanFile(File("src/test/resources/metrics/comments.sql"), null, visitor)
assertThat(visitor.getLinesOfComments()).containsOnly(2, 4)
assertThat(visitor.getLinesOfComments()).containsOnly(2, 3, 4, 5)
}

@Test
Expand Down

0 comments on commit 09b8593

Please sign in to comment.