Skip to content

Commit

Permalink
feat(classes): Add counting of Scala classes
Browse files Browse the repository at this point in the history
There was no support for collecting Scala class number so I
added it. Tested with the unit tests and with SonarQube
6.3.1. Everything seemed OK.
  • Loading branch information
Boyan Bonev committed Aug 29, 2017
1 parent 4743716 commit 8fa49b6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
15 changes: 9 additions & 6 deletions src/main/scala/com/sagacify/sonar/scala/Measures.scala
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package com.sagacify.sonar.scala

import scala.annotation.tailrec

import scalariform.lexer.ScalaLexer
import scalariform.lexer.Token
import scalariform.lexer.Tokens.LINE_COMMENT
import scalariform.lexer.Tokens.MULTILINE_COMMENT
import scalariform.lexer.Tokens.XML_COMMENT
import scalariform.lexer.Tokens.WS
import scalariform.lexer.Tokens.EOF
import scalariform.lexer.Tokens._

object Measures {

final def count_classes(tokens: List[Token], i: Int = 0): Int = {
var count = 0
tokens.foreach(token => if (token.tokenType == CLASS || token.tokenType == OBJECT) count += 1)

count
}


/* applied on raw source code */

/* applied on lines of code */
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/com/sagacify/sonar/scala/ScalaSensor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ScalaSensor(scala: Scala, fs: FileSystem) extends Sensor {
CM.NCLOC,
Measures.count_ncloc(tokens))

// context.saveMeasure(input, CM.CLASSES, classes)
context.saveMeasure(inputFile, CM.CLASSES, Measures.count_classes(tokens))
// context.saveMeasure(input, CM.FUNCTIONS, methods)
// context.saveMeasure(input, CM.ACCESSORS, accessors)
// context.saveMeasure(input, CM.COMPLEXITY_IN_FUNCTIONS, complexityInMethods)
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/ScalaFile2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ class ScalaFile2 {
println("function called.")
}
}

object ScalaFile2 {
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class ScalaSensorSpec extends FlatSpec with Matchers {
.saveMeasure(file, CM.FILES, 1)
verify(sensorContext, times(1))
.saveMeasure(file, CM.COMMENT_LINES, 0)
verify(sensorContext, times(1))
.saveMeasure(file, CM.CLASSES, 1)

}
}
Expand All @@ -87,7 +89,8 @@ class ScalaSensorSpec extends FlatSpec with Matchers {
.saveMeasure(file, CM.FILES, 1)
verify(sensorContext, times(1))
.saveMeasure(file, CM.COMMENT_LINES, 1)

verify(sensorContext, times(1))
.saveMeasure(file, CM.CLASSES, 2)
}
}
}

0 comments on commit 8fa49b6

Please sign in to comment.