From e33a38b00508f96283247b3601cb270c573fad94 Mon Sep 17 00:00:00 2001 From: "Lasse R.H. Nielsen" Date: Mon, 3 Nov 2025 16:29:44 +0100 Subject: [PATCH 1/4] Change classes and constructors to new/primary syntax. --- .../package_config/bin/package_config_of.dart | 30 +++--- pkgs/package_config/lib/src/errors.dart | 20 ++-- .../lib/src/package_config.dart | 96 ++++++++----------- 3 files changed, 59 insertions(+), 87 deletions(-) diff --git a/pkgs/package_config/bin/package_config_of.dart b/pkgs/package_config/bin/package_config_of.dart index 6e8f95b7f..966037607 100644 --- a/pkgs/package_config/bin/package_config_of.dart +++ b/pkgs/package_config/bin/package_config_of.dart @@ -147,30 +147,23 @@ Future _resolvePackageConfig( } /// Gathered package configuration information for [path]. -final class ConfigInfo { +final class ConfigInfo( /// Original path being resolved. - final String path; + final String path, /// Path to package configuration file, if any. - final String? configPath; + final String? configPath, /// Package that path belongs to, if any. - final Package? package; + final Package? package, /// Package URI for [path], if it has one. /// Always `null` if [package] is `null`. - final Uri? packageUri; + final Uri? packageUri, /// Language version override in file, if any. - final LanguageVersion? languageVersionOverride; - - ConfigInfo( - this.path, - this.configPath, - this.package, - this.packageUri, - this.languageVersionOverride, - ); + final LanguageVersion? languageVersionOverride, +) { Map toJson() { return { @@ -338,12 +331,13 @@ final leadRegExp = RegExp( // -------------------------------------------------------------------- // Find and load (and cache) package configurations -class PackageConfigLoader { +class PackageConfigLoader({ /// Stop searching at the current working directory. - final bool noParent; + final bool noParent = false, /// Stop searching if finding a `pubspec.yaml` with no package configuration. - final bool stopAtPubspec; + final bool stopAtPubspec = false, +}) { /// Cache lookup results in case someone does more lookups on the same path. final Map< @@ -352,8 +346,6 @@ class PackageConfigLoader { > _packageConfigCache = {}; - PackageConfigLoader({this.stopAtPubspec = false, this.noParent = false}); - /// Finds a package configuration relative to [path]. /// /// Caches result for each directory looked at. diff --git a/pkgs/package_config/lib/src/errors.dart b/pkgs/package_config/lib/src/errors.dart index ba6ad5d38..ba4aac7b6 100644 --- a/pkgs/package_config/lib/src/errors.dart +++ b/pkgs/package_config/lib/src/errors.dart @@ -6,31 +6,25 @@ /// /// Only covers errors thrown while parsing package configuration files. /// Programming errors and I/O exceptions are not covered. -abstract class PackageConfigError { - PackageConfigError._(); -} +abstract interface class PackageConfigError {} -class PackageConfigArgumentError extends ArgumentError - implements PackageConfigError { - PackageConfigArgumentError( +class PackageConfigArgumentError( Object? super.value, String super.name, String super.message, - ) : super.value(); +) extends ArgumentError.value() { - PackageConfigArgumentError.from(ArgumentError error) + new from(ArgumentError error) : super.value(error.invalidValue, error.name, error.message); } -class PackageConfigFormatException extends FormatException - implements PackageConfigError { - PackageConfigFormatException( +class PackageConfigFormatException( super.message, Object? super.source, [ super.offset, - ]); + ]) extends FormatException { - PackageConfigFormatException.from(FormatException exception) + new from(FormatException exception) : super(exception.message, exception.source, exception.offset); } diff --git a/pkgs/package_config/lib/src/package_config.dart b/pkgs/package_config/lib/src/package_config.dart index ce626e212..f470f8c48 100644 --- a/pkgs/package_config/lib/src/package_config.dart +++ b/pkgs/package_config/lib/src/package_config.dart @@ -18,7 +18,7 @@ import 'util.dart'; /// so classes outside of this package must not implement [PackageConfig] /// or any subclass of it. @sealed -abstract class PackageConfig { +abstract interface class PackageConfig { /// The lowest configuration version currently supported. static const int minVersion = 2; @@ -56,7 +56,7 @@ abstract class PackageConfig { /// [PackageConfig.extraData] of the created configuration. /// /// The version of the resulting configuration is always [maxVersion]. - factory PackageConfig(Iterable packages, {Object? extraData}) => + factory(Iterable packages, {Object? extraData}) => SimplePackageConfig(maxVersion, packages, extraData); /// Parses a package configuration file. @@ -230,7 +230,7 @@ abstract class PackageConfig { /// Configuration data for a single package. @sealed -abstract class Package { +abstract interface class Package { /// Creates a package with the provided properties. /// /// The [name] must be a valid package name. @@ -251,7 +251,7 @@ abstract class Package { /// /// If [extraData] is supplied, it will be available as the /// [Package.extraData] of the created package. - factory Package( + factory( String name, Uri root, { Uri? packageUriRoot, @@ -329,7 +329,8 @@ abstract class Package { /// then an *invalid* language version may be represented by an /// [InvalidLanguageVersion] object. @sealed -abstract class LanguageVersion implements Comparable { +abstract interface class LanguageVersion + implements Comparable { /// The maximal value allowed by [major] and [minor] values; static const int maxValue = 0x7FFFFFFF; @@ -338,7 +339,7 @@ abstract class LanguageVersion implements Comparable { /// /// Both [major] and [minor] must be greater than or equal to 0 /// and less than or equal to [maxValue]. - factory LanguageVersion(int major, int minor) => SimpleLanguageVersion( + factory(int major, int minor) => SimpleLanguageVersion( RangeError.checkValueInInterval(major, 0, maxValue, 'major'), RangeError.checkValueInInterval(minor, 0, maxValue, 'minor'), null, @@ -424,7 +425,7 @@ abstract class LanguageVersion implements Comparable { /// The caller which provided the non-throwing `onError` handler /// should be prepared to encounter invalid values. @sealed -abstract class InvalidLanguageVersion implements LanguageVersion { +abstract interface class InvalidLanguageVersion implements LanguageVersion { /// The value -1 for an invalid language version. @override int get major; @@ -526,7 +527,13 @@ const bool _disallowPackagesInsidePackageUriRoot = false; // Implementations of the main data types exposed by the API of this package. @sealed -class SimplePackageConfig implements PackageConfig { +class SimplePackageConfig._( + this.version, + this._packageTree, + this._packages, + this.extraData, +) implements PackageConfig { + @override final int version; final Map _packages; @@ -534,7 +541,7 @@ class SimplePackageConfig implements PackageConfig { @override final Object? extraData; - factory SimplePackageConfig( + factory( int version, Iterable packages, [ Object? extraData, @@ -549,13 +556,6 @@ class SimplePackageConfig implements PackageConfig { }, extraData); } - SimplePackageConfig._( - this.version, - this._packageTree, - this._packages, - this.extraData, - ); - /// Creates empty configuration. /// /// The empty configuration can be used in cases where no configuration is @@ -738,28 +738,20 @@ class SimplePackageConfig implements PackageConfig { /// Configuration data for a single package. @sealed -class SimplePackage implements Package { +class SimplePackage._( @override - final String name; + final String name, @override - final Uri root; + final Uri root, @override - final Uri packageUriRoot; + final Uri packageUriRoot, @override - final LanguageVersion? languageVersion; + final LanguageVersion? languageVersion, @override - final Object? extraData; + final Object? extraData, @override - final bool relativeRoot; - - SimplePackage._( - this.name, - this.root, - this.packageUriRoot, - this.languageVersion, - this.extraData, - this.relativeRoot, - ); + final bool relativeRoot, +) implements Package { /// Creates a [SimplePackage] with the provided content. /// @@ -941,19 +933,17 @@ LanguageVersion parseLanguageVersion( } @sealed -class SimpleLanguageVersion implements LanguageVersion { +class SimpleLanguageVersion( @override - final int major; + final int major, @override - final int minor; - + final int minor, /// A cache for `toString`, pre-filled with source if created by parsing. /// /// Also used by [SimpleInvalidLanguageVersion] for its invalid source /// or a suitably invalid `toString` value. - String? _source; - - SimpleLanguageVersion(this.major, this.minor, this._source); + String? _source, +) implements LanguageVersion { @override bool operator ==(Object other) => @@ -974,9 +964,9 @@ class SimpleLanguageVersion implements LanguageVersion { } @sealed -class SimpleInvalidLanguageVersion extends SimpleLanguageVersion +class SimpleInvalidLanguageVersion(String source) + extends SimpleLanguageVersion(-1, -1, source) implements InvalidLanguageVersion { - SimpleInvalidLanguageVersion(String source) : super(-1, -1, source); @override int get hashCode => identityHashCode(this); @@ -984,12 +974,12 @@ class SimpleInvalidLanguageVersion extends SimpleLanguageVersion bool operator ==(Object other) => identical(this, other); } -abstract class PackageTree { +abstract interface class PackageTree { Iterable get allPackages; SimplePackage? packageOf(Uri file); } -class _PackageTrieNode { +class _PackageTrieNode() { SimplePackage? package; /// Indexed by path segment. @@ -1008,7 +998,7 @@ class _PackageTrieNode { /// The package root path of a package must not be inside another package's /// root path. /// Entire other packages are allowed inside a package's root. -class TriePackageTree implements PackageTree { +class TriePackageTree() implements PackageTree { /// Indexed by URI scheme. final Map _map = {}; @@ -1155,9 +1145,7 @@ class TriePackageTree implements PackageTree { } } -class EmptyPackageTree implements PackageTree { - const EmptyPackageTree(); - +class const EmptyPackageTree() implements PackageTree { @override Iterable get allPackages => const Iterable.empty(); @@ -1184,18 +1172,16 @@ enum ConflictType { sameRoots, interleaving, insidePackageRoot } /// The [package] conflicts with [existingPackage] if it has /// the same root path or the package URI root path /// of [existingPackage] is inside the root path of [package]. -class ConflictException { +class ConflictException( /// The existing package that [package] conflicts with. - final SimplePackage existingPackage; - + final SimplePackage existingPackage, /// The package that could not be added without a conflict. - final SimplePackage package; - + final SimplePackage package, /// Whether the conflict is with the package URI root of [existingPackage]. - final ConflictType conflictType; - + final ConflictType conflictType, +) { /// Creates a root conflict between [package] and [existingPackage]. - ConflictException(this.package, this.existingPackage, this.conflictType); + this; } /// Used for sorting packages by root path. From 297a91779c8c235facee28ea2a3eaadb632b6c9e Mon Sep 17 00:00:00 2001 From: "Lasse R.H. Nielsen" Date: Tue, 4 Nov 2025 10:11:19 +0100 Subject: [PATCH 2/4] Lost some implements clauses. --- pkgs/package_config/lib/src/errors.dart | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkgs/package_config/lib/src/errors.dart b/pkgs/package_config/lib/src/errors.dart index ba4aac7b6..92ff48c71 100644 --- a/pkgs/package_config/lib/src/errors.dart +++ b/pkgs/package_config/lib/src/errors.dart @@ -12,7 +12,8 @@ class PackageConfigArgumentError( Object? super.value, String super.name, String super.message, -) extends ArgumentError.value() { +) extends ArgumentError.value() + implements PackageConfigError { new from(ArgumentError error) : super.value(error.invalidValue, error.name, error.message); @@ -22,7 +23,8 @@ class PackageConfigFormatException( super.message, Object? super.source, [ super.offset, - ]) extends FormatException { + ]) extends FormatException + implements PackageConfigError { new from(FormatException exception) : super(exception.message, exception.source, exception.offset); From d6fd499be183afe7a0f641d6e4d562828eb80b07 Mon Sep 17 00:00:00 2001 From: "Lasse R.H. Nielsen" Date: Tue, 4 Nov 2025 13:25:22 +0100 Subject: [PATCH 3/4] Avoid primary + other initializing constructor. --- pkgs/package_config/lib/src/errors.dart | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pkgs/package_config/lib/src/errors.dart b/pkgs/package_config/lib/src/errors.dart index 92ff48c71..f60a2dcdf 100644 --- a/pkgs/package_config/lib/src/errors.dart +++ b/pkgs/package_config/lib/src/errors.dart @@ -8,13 +8,15 @@ /// Programming errors and I/O exceptions are not covered. abstract interface class PackageConfigError {} -class PackageConfigArgumentError( +class PackageConfigArgumentError extends ArgumentError + implements PackageConfigError { + + new( // Cannot be primary, `.from` cannot redirect to these types. Object? super.value, String super.name, String super.message, -) extends ArgumentError.value() - implements PackageConfigError { - + ) : super.value(); + new from(ArgumentError error) : super.value(error.invalidValue, error.name, error.message); } @@ -27,7 +29,7 @@ class PackageConfigFormatException( implements PackageConfigError { new from(FormatException exception) - : super(exception.message, exception.source, exception.offset); + : this(exception.message, exception.source, exception.offset); } /// The default `onError` handler. From ab279f0d10f43d9ac640535efd4465a5c99925f5 Mon Sep 17 00:00:00 2001 From: "Lasse R.H. Nielsen" Date: Tue, 4 Nov 2025 17:42:57 +0100 Subject: [PATCH 4/4] Undo bad edit. --- pkgs/package_config/lib/src/errors.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/package_config/lib/src/errors.dart b/pkgs/package_config/lib/src/errors.dart index f60a2dcdf..0fc77bbed 100644 --- a/pkgs/package_config/lib/src/errors.dart +++ b/pkgs/package_config/lib/src/errors.dart @@ -29,7 +29,7 @@ class PackageConfigFormatException( implements PackageConfigError { new from(FormatException exception) - : this(exception.message, exception.source, exception.offset); + : super.value(exception.message, exception.source, exception.offset); } /// The default `onError` handler.