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 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
8 changes: 8 additions & 0 deletions pkgs/pub_semver/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 2.1.6

- Clarify that the lists returned by
the `preRelease` and `build` properties of `Version` and
the `ranges` property of `VersionUnion` should not be modified.
- Note that `VersionConstraint.any` and `VersionConstraint.empty` static fields
shouldn't be reassigned and will be made `final` in a future release.

## 2.1.5

- Require Dart `3.4.0`.
Expand Down
10 changes: 8 additions & 2 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.
///
/// **Note:** The returned list shouldn't be modified.
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.
///
/// **Note:** The returned list shouldn't be modified.
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
? []
: _splitParts(preRelease),
build = build == null || build.isEmpty ? [] : _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
6 changes: 6 additions & 0 deletions pkgs/pub_semver/lib/src/version_constraint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ import 'version_union.dart';
/// version.
abstract class VersionConstraint {
/// A [VersionConstraint] that allows all versions.
///
/// **Note:** This property shouldn't be reassigned.
/// It will be made `final` in a future release.
static VersionConstraint any = VersionRange();

/// A [VersionConstraint] that allows no versions -- the empty set.
///
/// **Note:** This property shouldn't be reassigned.
/// It will be made `final` in a future release.
static VersionConstraint empty = const _EmptyVersion();

/// Parses a version constraint.
Expand Down
2 changes: 2 additions & 0 deletions pkgs/pub_semver/lib/src/version_union.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class VersionUnion implements VersionConstraint {
/// * Its contents are disjoint and non-adjacent. In other words, for any two
/// constraints next to each other in the list, there's some version between
/// those constraints that they don't match.
///
/// **Note:** The returned list shouldn't be modified.
final List<VersionRange> ranges;

@override
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.1.6
description: >-
Versions and version constraints implementing pub's versioning policy. This
is very similar to vanilla semver, with a few corner cases.
Expand Down