Skip to content

Commit 4e6d576

Browse files
committed
fix(tool): Optimize changelog reading and list regex
Moves the regular expression for checking blank lines in lists to a static final field in `VersionCheckCommand` so that it is compiled only once instead of on every execution of `_findBlankLineInList`. Additionally, the `CHANGELOG.md` is now read entirely into a string first. This unified content is then used for both line-by-line version extraction and the subsequent regex-based formatting check. Signed-off-by: Lorenzo Croce <[email protected]>
1 parent b7b10ce commit 4e6d576

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

script/tool/lib/src/version_check_command.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ class VersionCheckCommand extends PackageLoopingCommand {
130130
hide: true);
131131
}
132132

133+
static final RegExp _blankLineInListRegex = RegExp(
134+
r'(^[ \t]*[*+-].*$\n)((^[ \t]*$\n)+)(^[ \t]*[*+-].*$)',
135+
multiLine: true);
136+
133137
static const String _againstPubFlag = 'against-pub';
134138
static const String _prLabelsArg = 'pr-labels';
135139
static const String _checkForMissingChanges = 'check-for-missing-changes';
@@ -377,7 +381,8 @@ ${indentation}HTTP response: ${pubVersionFinderResponse.httpResponse.body}
377381

378382
// get first version from CHANGELOG
379383
final File changelog = package.changelogFile;
380-
final List<String> lines = changelog.readAsLinesSync();
384+
final String changelogContent = changelog.readAsStringSync();
385+
final List<String> lines = changelogContent.split('\n');
381386
String? firstLineWithText;
382387
final Iterator<String> iterator = lines.iterator;
383388
while (iterator.moveNext()) {
@@ -456,7 +461,7 @@ ${indentation}The first version listed in CHANGELOG.md is $fromChangeLog.
456461
}
457462

458463
// Check for blank lines between list items in the version section.
459-
final Match? blankLineMatch = _findBlankLineInList(lines.join('\n'));
464+
final Match? blankLineMatch = _findBlankLineInList(changelogContent);
460465
if (blankLineMatch != null) {
461466
final String offendingLines = blankLineMatch
462467
.group(0)!
@@ -481,14 +486,9 @@ ${indentation}The first version listed in CHANGELOG.md is $fromChangeLog.
481486
///
482487
/// Returns the first invalid match found, or null if validation passes.
483488
Match? _findBlankLineInList(String changelogContent) {
484-
// This regex requires the multiLine flag to be true.
485-
final RegExp blankLineInListRegex = RegExp(
486-
r'(^[ \t]*[*+-].*$\n)((^[ \t]*$\n)+)(^[ \t]*[*+-].*$)',
487-
multiLine: true);
488-
489489
// If the regex finds a match, it means there is an error (a blank line
490490
// between list items).
491-
return blankLineInListRegex.firstMatch(changelogContent);
491+
return _blankLineInListRegex.firstMatch(changelogContent);
492492
}
493493

494494
Pubspec? _tryParsePubspec(RepositoryPackage package) {

0 commit comments

Comments
 (0)