Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- Fixed small ambiguity in the order of license match evaluation.
- Resolve `analysis_options.yaml` dev dependency to expose transitive formatter options.
- Fixed license coverage calculation.
- Fixed license match post-filtering.

## 0.22.23

Expand Down
40 changes: 24 additions & 16 deletions lib/src/license_detection/license_detector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -268,25 +268,33 @@ double calculateUnclaimedTokenPercentage(
/// Returns the number of tokens in the longest unclaimed token
/// sequence.
int findLongestUnclaimedTokenRange(List<LicenseMatch> matches, int end) {
var ranges = <Range>[];
if (matches.isEmpty) return end;

for (var match in matches) {
ranges.add(Range(match.tokenRange.start, match.tokenRange.end));
}

ranges.sort(sortRangeOnStartValue);
var maxTokenSequence = ranges.first.start - 0;
var maxUnclaimed = 0;

// create an end-of-range marker at the end of ranges for easier computation
final ranges = [...matches.map((m) => m.tokenRange), Range(end, end)];

// move position from the start up to the end, checking uncovered ranges
for (var pos = 0; pos < end;) {
// check if inside a range
final skipToEnd = ranges
.where((r) => r.containsIndex(pos))
.map((r) => r.end)
.fold(-1, max);
if (skipToEnd > -1) {
assert(pos < skipToEnd);
pos = skipToEnd;
continue;
}

for (var i = 1; i < ranges.length; i++) {
maxTokenSequence = max(
ranges[i].start - ranges[i - 1].end,
maxTokenSequence,
);
// jump to next range
final nextRange = ranges
.where((r) => r.start > pos)
.reduce((a, b) => a.start.compareTo(b.start) <= 0 ? a : b);
maxUnclaimed = max(maxUnclaimed, nextRange.start - pos);
pos = nextRange.end;
}

maxTokenSequence = max(maxTokenSequence, end - ranges.last.end);
return maxTokenSequence;
return maxUnclaimed;
}

int sortRangeOnStartValue(Range a, Range b) => a.start - b.start;
2 changes: 2 additions & 0 deletions lib/src/license_detection/token_matcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class Range {
(end >= other.start && end <= other.end);
}

bool containsIndex(int value) => start <= value && value < end;

@override
String toString() => 'Range($start-$end)';
}
Expand Down
10 changes: 10 additions & 0 deletions test/license_detector_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,16 @@ void main() {
160,
1000,
);

_testLongestUnclaimedTokenRange(
'Trailing contained range',
[
_dummyLicenseMatchInstance(0.9, '', start: 30, end: 940),
_dummyLicenseMatchInstance(0.9, '', start: 50, end: 70),
],
60,
1000,
);
});
}

Expand Down
4 changes: 0 additions & 4 deletions test/licenses/license_coverage_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ void main() {
license.content,
relativePath: 'LICENSE',
);
// TODO: fix detection and return at least one match (https://github.com/dart-lang/pana/issues/1484)
if (license.identifier == 'SSH-OpenSSH') {
continue;
}
final match = detected
.where((l) => l.spdxIdentifier == license.identifier)
.single;
Expand Down
Loading