Skip to content

Commit a0c9f10

Browse files
authored
refactor!: Seal the Dependency class (dart-archive/pubspec_parse#140)
* fix: Don't use runtimeType for toString
1 parent a7ddf35 commit a0c9f10

File tree

3 files changed

+20
-28
lines changed

3 files changed

+20
-28
lines changed

pkgs/pubspec_parse/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
## 1.3.1-wip
1+
## 1.4.0-wip
22

33
- Require Dart 3.2
4+
- Seal the `Dependency` class.
45
- Set `Pubspec.environment` to non-nullable.
56
- Remove deprecated package_api_docs rule
67

pkgs/pubspec_parse/lib/src/dependency.dart

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,7 @@ Dependency? _fromJson(Object? data, String name) {
9090
return null;
9191
}
9292

93-
abstract class Dependency {
94-
Dependency._();
95-
96-
String get _info;
97-
98-
@override
99-
String toString() => '$runtimeType: $_info';
100-
}
93+
sealed class Dependency {}
10194

10295
@JsonSerializable()
10396
class SdkDependency extends Dependency {
@@ -106,18 +99,17 @@ class SdkDependency extends Dependency {
10699
final VersionConstraint version;
107100

108101
SdkDependency(this.sdk, {VersionConstraint? version})
109-
: version = version ?? VersionConstraint.any,
110-
super._();
111-
112-
@override
113-
String get _info => sdk;
102+
: version = version ?? VersionConstraint.any;
114103

115104
@override
116105
bool operator ==(Object other) =>
117106
other is SdkDependency && other.sdk == sdk && other.version == version;
118107

119108
@override
120109
int get hashCode => Object.hash(sdk, version);
110+
111+
@override
112+
String toString() => 'SdkDependency: $sdk';
121113
}
122114

123115
@JsonSerializable()
@@ -127,7 +119,7 @@ class GitDependency extends Dependency {
127119
final String? ref;
128120
final String? path;
129121

130-
GitDependency(this.url, {this.ref, this.path}) : super._();
122+
GitDependency(this.url, {this.ref, this.path});
131123

132124
factory GitDependency.fromData(Object? data) {
133125
if (data is String) {
@@ -141,9 +133,6 @@ class GitDependency extends Dependency {
141133
throw ArgumentError.value(data, 'git', 'Must be a String or a Map.');
142134
}
143135

144-
@override
145-
String get _info => 'url@$url';
146-
147136
@override
148137
bool operator ==(Object other) =>
149138
other is GitDependency &&
@@ -153,6 +142,9 @@ class GitDependency extends Dependency {
153142

154143
@override
155144
int get hashCode => Object.hash(url, ref, path);
145+
146+
@override
147+
String toString() => 'GitDependency: url@$url';
156148
}
157149

158150
Uri? parseGitUriOrNull(String? value) =>
@@ -194,7 +186,7 @@ Uri? _tryParseScpUri(String value) {
194186
class PathDependency extends Dependency {
195187
final String path;
196188

197-
PathDependency(this.path) : super._();
189+
PathDependency(this.path);
198190

199191
factory PathDependency.fromData(Object? data) {
200192
if (data is String) {
@@ -203,15 +195,15 @@ class PathDependency extends Dependency {
203195
throw ArgumentError.value(data, 'path', 'Must be a String.');
204196
}
205197

206-
@override
207-
String get _info => 'path@$path';
208-
209198
@override
210199
bool operator ==(Object other) =>
211200
other is PathDependency && other.path == path;
212201

213202
@override
214203
int get hashCode => path.hashCode;
204+
205+
@override
206+
String toString() => 'PathDependency: path@$path';
215207
}
216208

217209
@JsonSerializable(disallowUnrecognizedKeys: true)
@@ -223,11 +215,7 @@ class HostedDependency extends Dependency {
223215
final HostedDetails? hosted;
224216

225217
HostedDependency({VersionConstraint? version, this.hosted})
226-
: version = version ?? VersionConstraint.any,
227-
super._();
228-
229-
@override
230-
String get _info => version.toString();
218+
: version = version ?? VersionConstraint.any;
231219

232220
@override
233221
bool operator ==(Object other) =>
@@ -237,6 +225,9 @@ class HostedDependency extends Dependency {
237225

238226
@override
239227
int get hashCode => Object.hash(version, hosted);
228+
229+
@override
230+
String toString() => 'HostedDependency: $version';
240231
}
241232

242233
@JsonSerializable(disallowUnrecognizedKeys: true)

pkgs/pubspec_parse/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: pubspec_parse
2-
version: 1.3.1-wip
2+
version: 1.4.0-wip
33
description: >-
44
Simple package for parsing pubspec.yaml files with a type-safe API and rich
55
error reporting.

0 commit comments

Comments
 (0)