@@ -59,8 +59,6 @@ class HitMap {
59
59
required Map <String , List <List <int >>?> ignoredLinesInFilesCache,
60
60
required Resolver resolver,
61
61
}) {
62
- final loader = Loader ();
63
-
64
62
// Map of source file to map of line to hit count for that line.
65
63
final globalHitMap = < String , HitMap > {};
66
64
@@ -71,66 +69,6 @@ class HitMap {
71
69
continue ;
72
70
}
73
71
74
- var ignoredLinesList = < List <int >> [];
75
-
76
- if (checkIgnoredLines) {
77
- if (ignoredLinesInFilesCache.containsKey (source)) {
78
- final cacheHit = ignoredLinesInFilesCache[source];
79
- if (cacheHit == null ) {
80
- // Null-entry indicates that the whole file was ignored.
81
- continue ;
82
- }
83
- ignoredLinesList = cacheHit;
84
- } else {
85
- final path = resolver.resolve (source);
86
- if (path != null ) {
87
- final lines = loader.loadSync (path) ?? [];
88
- ignoredLinesList = getIgnoredLines (path, lines);
89
-
90
- // Ignore the whole file.
91
- if (ignoredLinesList.length == 1 &&
92
- ignoredLinesList[0 ][0 ] == 0 &&
93
- ignoredLinesList[0 ][1 ] == lines.length) {
94
- // Null-entry indicates that the whole file was ignored.
95
- ignoredLinesInFilesCache[source] = null ;
96
- continue ;
97
- }
98
- ignoredLinesInFilesCache[source] = ignoredLinesList;
99
- } else {
100
- // Couldn't resolve source. Allow cache to answer next time
101
- // anyway.
102
- ignoredLinesInFilesCache[source] = ignoredLinesList;
103
- }
104
- }
105
- }
106
-
107
- // Move to the first ignore range.
108
- final ignoredLines = ignoredLinesList.iterator;
109
- var hasCurrent = ignoredLines.moveNext ();
110
-
111
- bool shouldIgnoreLine (Iterator <List <int >> ignoredRanges, int line) {
112
- if (! hasCurrent || ignoredRanges.current.isEmpty) {
113
- return false ;
114
- }
115
-
116
- if (line < ignoredRanges.current[0 ]) return false ;
117
-
118
- while (hasCurrent &&
119
- ignoredRanges.current.isNotEmpty &&
120
- ignoredRanges.current[1 ] < line) {
121
- hasCurrent = ignoredRanges.moveNext ();
122
- }
123
-
124
- if (hasCurrent &&
125
- ignoredRanges.current.isNotEmpty &&
126
- ignoredRanges.current[0 ] <= line &&
127
- line <= ignoredRanges.current[1 ]) {
128
- return true ;
129
- }
130
-
131
- return false ;
132
- }
133
-
134
72
void addToMap (Map <int , int > map, int line, int count) {
135
73
final oldCount = map.putIfAbsent (line, () => 0 );
136
74
map[line] = count + oldCount;
@@ -147,17 +85,13 @@ class HitMap {
147
85
final k = hits[i];
148
86
if (k is int ) {
149
87
// Single line.
150
- if (shouldIgnoreLine (ignoredLines, k)) continue ;
151
-
152
88
addToMap (hitMap, k, hits[i + 1 ] as int );
153
89
} else if (k is String ) {
154
90
// Linerange. We expand line ranges to actual lines at this point.
155
91
final splitPos = k.indexOf ('-' );
156
92
final start = int .parse (k.substring (0 , splitPos));
157
93
final end = int .parse (k.substring (splitPos + 1 ));
158
94
for (var j = start; j <= end; j++ ) {
159
- if (shouldIgnoreLine (ignoredLines, j)) continue ;
160
-
161
95
addToMap (hitMap, j, hits[i + 1 ] as int );
162
96
}
163
97
} else {
@@ -185,7 +119,12 @@ class HitMap {
185
119
fillHitMap (e['branchHits' ] as List , sourceHitMap.branchHits! );
186
120
}
187
121
}
188
- return globalHitMap;
122
+ return checkIgnoredLines
123
+ ? globalHitMap.filterIgnored (
124
+ ignoredLinesInFilesCache: ignoredLinesInFilesCache,
125
+ resolver: resolver,
126
+ )
127
+ : globalHitMap;
189
128
}
190
129
191
130
/// Creates a single hitmap from a raw json object.
@@ -268,6 +207,39 @@ extension FileHitMaps on Map<String, HitMap> {
268
207
}
269
208
});
270
209
}
210
+
211
+ /// Filters out lines that are ignored by ignore comments.
212
+ Map <String , HitMap > filterIgnored ({
213
+ required Map <String , List <List <int >>?> ignoredLinesInFilesCache,
214
+ required Resolver resolver,
215
+ }) {
216
+ final loader = Loader ();
217
+ final newHitMaps = < String , HitMap > {};
218
+ for (final MapEntry (key: source, value: hitMap) in entries) {
219
+ final ignoredLinesList = ignoredLinesInFilesCache.putIfAbsent (source, () {
220
+ final path = resolver.resolve (source);
221
+ if (path == null ) return < List <int >> [];
222
+ return getIgnoredLines (path, loader.loadSync (path));
223
+ });
224
+ // Null here means that the whole file is ignored.
225
+ if (ignoredLinesList == null ) continue ;
226
+
227
+ Map <int , int >? filterHits (Map <int , int >? hits) => hits == null
228
+ ? null
229
+ : {
230
+ for (final MapEntry (key: line, value: count) in hits.entries)
231
+ if (! ignoredLinesList.ignoredContains (line)) line: count,
232
+ };
233
+
234
+ newHitMaps[source] = HitMap (
235
+ filterHits (hitMap.lineHits),
236
+ filterHits (hitMap.funcHits),
237
+ hitMap.funcNames,
238
+ filterHits (hitMap.branchHits),
239
+ );
240
+ }
241
+ return newHitMaps;
242
+ }
271
243
}
272
244
273
245
/// Class containing information about a coverage hit.
0 commit comments