1
1
package scoverage
2
2
3
3
import scala .collection .mutable
4
- import scala .reflect .internal .util .{Position , SourceFile }
4
+ import scala .reflect .internal .util .Position
5
+ import scala .reflect .internal .util .SourceFile
5
6
import scala .util .matching .Regex
6
7
7
- /**
8
- * Methods related to filtering the instrumentation and coverage.
9
- *
10
- * @author Stephen Samuel
11
- */
8
+ /** Methods related to filtering the instrumentation and coverage.
9
+ *
10
+ * @author Stephen Samuel
11
+ */
12
12
trait CoverageFilter {
13
13
def isClassIncluded (className : String ): Boolean
14
14
def isFileIncluded (file : SourceFile ): Boolean
@@ -25,40 +25,44 @@ object AllCoverageFilter extends CoverageFilter {
25
25
override def isSymbolIncluded (symbolName : String ): Boolean = true
26
26
}
27
27
28
- class RegexCoverageFilter (excludedPackages : Seq [String ],
29
- excludedFiles : Seq [String ],
30
- excludedSymbols : Seq [String ]) extends CoverageFilter {
28
+ class RegexCoverageFilter (
29
+ excludedPackages : Seq [String ],
30
+ excludedFiles : Seq [String ],
31
+ excludedSymbols : Seq [String ]
32
+ ) extends CoverageFilter {
31
33
32
34
val excludedClassNamePatterns = excludedPackages.map(_.r.pattern)
33
35
val excludedFilePatterns = excludedFiles.map(_.r.pattern)
34
36
val excludedSymbolPatterns = excludedSymbols.map(_.r.pattern)
35
37
36
- /**
37
- * We cache the excluded ranges to avoid scanning the source code files
38
- * repeatedly. For a large project there might be a lot of source code
39
- * data, so we only hold a weak reference.
40
- */
41
- val linesExcludedByScoverageCommentsCache : mutable.Map [SourceFile , List [Range ]] = mutable.WeakHashMap .empty
38
+ /** We cache the excluded ranges to avoid scanning the source code files
39
+ * repeatedly. For a large project there might be a lot of source code
40
+ * data, so we only hold a weak reference.
41
+ */
42
+ val linesExcludedByScoverageCommentsCache
43
+ : mutable.Map [SourceFile , List [Range ]] = mutable.WeakHashMap .empty
42
44
43
45
final val scoverageExclusionCommentsRegex =
44
46
""" (?ms)^\s*//\s*(\$COVERAGE-OFF\$).*?(^\s*//\s*\$COVERAGE-ON\$|\Z)""" .r
45
47
46
- /**
47
- * True if the given className has not been excluded by the
48
- * `excludedPackages` option.
49
- */
48
+ /** True if the given className has not been excluded by the
49
+ * `excludedPackages` option.
50
+ */
50
51
override def isClassIncluded (className : String ): Boolean = {
51
- excludedClassNamePatterns.isEmpty || ! excludedClassNamePatterns.exists(_.matcher(className).matches)
52
+ excludedClassNamePatterns.isEmpty || ! excludedClassNamePatterns.exists(
53
+ _.matcher(className).matches
54
+ )
52
55
}
53
56
54
57
override def isFileIncluded (file : SourceFile ): Boolean = {
55
- def isFileMatch (file : SourceFile ) = excludedFilePatterns.exists(_.matcher(file.path.replace(" .scala" , " " )).matches)
58
+ def isFileMatch (file : SourceFile ) = excludedFilePatterns.exists(
59
+ _.matcher(file.path.replace(" .scala" , " " )).matches
60
+ )
56
61
excludedFilePatterns.isEmpty || ! isFileMatch(file)
57
62
}
58
63
59
- /**
60
- * True if the line containing `position` has not been excluded by a magic comment.
61
- */
64
+ /** True if the line containing `position` has not been excluded by a magic comment.
65
+ */
62
66
def isLineIncluded (position : Position ): Boolean = {
63
67
if (position.isDefined) {
64
68
val excludedLineNumbers = getExcludedLineNumbers(position.source)
@@ -70,27 +74,34 @@ class RegexCoverageFilter(excludedPackages: Seq[String],
70
74
}
71
75
72
76
override def isSymbolIncluded (symbolName : String ): Boolean = {
73
- excludedSymbolPatterns.isEmpty || ! excludedSymbolPatterns.exists(_.matcher(symbolName).matches)
77
+ excludedSymbolPatterns.isEmpty || ! excludedSymbolPatterns.exists(
78
+ _.matcher(symbolName).matches
79
+ )
74
80
}
75
81
76
- /**
77
- * Provides overloads to paper over 2.12.13+ SourceFile incompatibility
78
- */
79
- def compatFindAllIn (regexp : Regex , pattern : Array [Char ]): Regex .MatchIterator = regexp.findAllIn(new String (pattern))
80
- def compatFindAllIn (regexp : Regex , pattern : String ): Regex .MatchIterator = regexp.findAllIn(pattern)
82
+ /** Provides overloads to paper over 2.12.13+ SourceFile incompatibility
83
+ */
84
+ def compatFindAllIn (
85
+ regexp : Regex ,
86
+ pattern : Array [Char ]
87
+ ): Regex .MatchIterator = regexp.findAllIn(new String (pattern))
88
+ def compatFindAllIn (regexp : Regex , pattern : String ): Regex .MatchIterator =
89
+ regexp.findAllIn(pattern)
81
90
82
- /**
83
- * Checks the given sourceFile for any magic comments which exclude lines
84
- * from coverage. Returns a list of Ranges of lines that should be excluded.
85
- *
86
- * The line numbers returned are conventional 1-based line numbers (i.e. the
87
- * first line is line number 1)
88
- */
91
+ /** Checks the given sourceFile for any magic comments which exclude lines
92
+ * from coverage. Returns a list of Ranges of lines that should be excluded.
93
+ *
94
+ * The line numbers returned are conventional 1-based line numbers (i.e. the
95
+ * first line is line number 1)
96
+ */
89
97
def getExcludedLineNumbers (sourceFile : SourceFile ): List [Range ] = {
90
98
linesExcludedByScoverageCommentsCache.get(sourceFile) match {
91
99
case Some (lineNumbers) => lineNumbers
92
100
case None =>
93
- val lineNumbers = compatFindAllIn(scoverageExclusionCommentsRegex, sourceFile.content).matchData.map { m =>
101
+ val lineNumbers = compatFindAllIn(
102
+ scoverageExclusionCommentsRegex,
103
+ sourceFile.content
104
+ ).matchData.map { m =>
94
105
// Asking a SourceFile for the line number of the char after
95
106
// the end of the file gives an exception
96
107
val endChar = math.min(m.end(2 ), sourceFile.content.length - 1 )
@@ -100,7 +111,8 @@ class RegexCoverageFilter(excludedPackages: Seq[String],
100
111
// line numbers
101
112
Range (
102
113
1 + sourceFile.offsetToLine(m.start(1 )),
103
- 1 + sourceFile.offsetToLine(endChar))
114
+ 1 + sourceFile.offsetToLine(endChar)
115
+ )
104
116
}.toList
105
117
linesExcludedByScoverageCommentsCache.put(sourceFile, lineNumbers)
106
118
lineNumbers
0 commit comments