Skip to content

[pub_semver] Discourage modification of properties intended to be unmodifiable #2020

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions pkgs/pub_semver/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.2.0-wip

- Clarify the `preRelease` and `build` properties of `Version`
return unmodifiable lists.

## 2.1.5

- Require Dart `3.4.0`.
Expand Down
12 changes: 9 additions & 3 deletions pkgs/pub_semver/lib/src/version.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,17 @@ class Version implements VersionConstraint, VersionRange {
/// This is split into a list of components, each of which may be either a
/// string or a non-negative integer. It may also be empty, indicating that
/// this version has no pre-release identifier.
///
/// The returned list is unmodifiable.
final List<Object> preRelease;

/// The build identifier: "foo" in "1.2.3+foo".
///
/// This is split into a list of components, each of which may be either a
/// string or a non-negative integer. It may also be empty, indicating that
/// this version has no build identifier.
///
/// The returned list is unmodifiable.
final List<Object> build;

/// The original string representation of the version number.
Expand All @@ -96,8 +100,10 @@ class Version implements VersionConstraint, VersionRange {

Version._(this.major, this.minor, this.patch, String? preRelease,
String? build, this._text)
: preRelease = preRelease == null ? <Object>[] : _splitParts(preRelease),
build = build == null ? [] : _splitParts(build) {
: preRelease = preRelease == null || preRelease.isEmpty
? const []
: _splitParts(preRelease),
build = build == null || build.isEmpty ? const [] : _splitParts(build) {
if (major < 0) throw ArgumentError('Major version must be non-negative.');
if (minor < 0) throw ArgumentError('Minor version must be non-negative.');
if (patch < 0) throw ArgumentError('Patch version must be non-negative.');
Expand Down Expand Up @@ -160,7 +166,7 @@ class Version implements VersionConstraint, VersionRange {
// Return an integer part if possible, otherwise return the string
// as-is
int.tryParse(part) ?? part)
.toList();
.toList(growable: false);

@override
bool operator ==(Object other) =>
Expand Down
2 changes: 1 addition & 1 deletion pkgs/pub_semver/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: pub_semver
version: 2.1.5
version: 2.2.0-wip
description: >-
Versions and version constraints implementing pub's versioning policy. This
is very similar to vanilla semver, with a few corner cases.
Expand Down