From 0fc89f6e3a5ef4d7243f0c10c94644c0770e8412 Mon Sep 17 00:00:00 2001 From: Kota Hayashi <90010509+K9i-0@users.noreply.github.com> Date: Fri, 7 Apr 2023 19:15:05 +0900 Subject: [PATCH] If dependencies is empty, add const (#2310) --- examples/pub/lib/pub_repository.freezed.dart | 127 +++++++++++++++--- packages/riverpod_generator/CHANGELOG.md | 5 + .../lib/src/templates/stateful_provider.dart | 4 +- .../test/dependencies_test.dart | 37 +++++ .../test/integration/dependencies.dart | 9 ++ .../test/integration/dependencies.g.dart | 34 +++++ .../fixes/provider_dependencies.g.dart | 16 +-- .../test/goldens/lints/dependencies.g.dart | 32 ++--- ...oviders_should_specify_dependencies.g.dart | 4 +- 9 files changed, 223 insertions(+), 45 deletions(-) diff --git a/examples/pub/lib/pub_repository.freezed.dart b/examples/pub/lib/pub_repository.freezed.dart index 71fb0dd93..cff8a08db 100644 --- a/examples/pub/lib/pub_repository.freezed.dart +++ b/examples/pub/lib/pub_repository.freezed.dart @@ -150,7 +150,9 @@ class __$$_PackageMetricsScoreCopyWithImpl<$Res> /// @nodoc @JsonSerializable() -class _$_PackageMetricsScore implements _PackageMetricsScore { +class _$_PackageMetricsScore + with DiagnosticableTreeMixin + implements _PackageMetricsScore { _$_PackageMetricsScore( {required this.grantedPoints, required this.maxPoints, @@ -179,10 +181,22 @@ class _$_PackageMetricsScore implements _PackageMetricsScore { } @override - String toString() { + String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) { return 'PackageMetricsScore(grantedPoints: $grantedPoints, maxPoints: $maxPoints, likeCount: $likeCount, popularityScore: $popularityScore, tags: $tags)'; } + @override + void debugFillProperties(DiagnosticPropertiesBuilder properties) { + super.debugFillProperties(properties); + properties + ..add(DiagnosticsProperty('type', 'PackageMetricsScore')) + ..add(DiagnosticsProperty('grantedPoints', grantedPoints)) + ..add(DiagnosticsProperty('maxPoints', maxPoints)) + ..add(DiagnosticsProperty('likeCount', likeCount)) + ..add(DiagnosticsProperty('popularityScore', popularityScore)) + ..add(DiagnosticsProperty('tags', tags)); + } + @override bool operator ==(dynamic other) { return identical(this, other) || @@ -344,7 +358,9 @@ class __$$_PackageMetricsResponseCopyWithImpl<$Res> /// @nodoc @JsonSerializable() -class _$_PackageMetricsResponse implements _PackageMetricsResponse { +class _$_PackageMetricsResponse + with DiagnosticableTreeMixin + implements _PackageMetricsResponse { _$_PackageMetricsResponse({required this.score}); factory _$_PackageMetricsResponse.fromJson(Map json) => @@ -354,10 +370,18 @@ class _$_PackageMetricsResponse implements _PackageMetricsResponse { final PackageMetricsScore score; @override - String toString() { + String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) { return 'PackageMetricsResponse(score: $score)'; } + @override + void debugFillProperties(DiagnosticPropertiesBuilder properties) { + super.debugFillProperties(properties); + properties + ..add(DiagnosticsProperty('type', 'PackageMetricsResponse')) + ..add(DiagnosticsProperty('score', score)); + } + @override bool operator ==(dynamic other) { return identical(this, other) || @@ -493,7 +517,9 @@ class __$$_PackageDetailsCopyWithImpl<$Res> /// @nodoc @JsonSerializable() -class _$_PackageDetails implements _PackageDetails { +class _$_PackageDetails + with DiagnosticableTreeMixin + implements _PackageDetails { _$_PackageDetails({required this.version, required this.pubspec}); factory _$_PackageDetails.fromJson(Map json) => @@ -505,10 +531,19 @@ class _$_PackageDetails implements _PackageDetails { final Pubspec pubspec; @override - String toString() { + String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) { return 'PackageDetails(version: $version, pubspec: $pubspec)'; } + @override + void debugFillProperties(DiagnosticPropertiesBuilder properties) { + super.debugFillProperties(properties); + properties + ..add(DiagnosticsProperty('type', 'PackageDetails')) + ..add(DiagnosticsProperty('version', version)) + ..add(DiagnosticsProperty('pubspec', pubspec)); + } + @override bool operator ==(dynamic other) { return identical(this, other) || @@ -656,7 +691,7 @@ class __$$_PackageCopyWithImpl<$Res> /// @nodoc @JsonSerializable() -class _$_Package implements _Package { +class _$_Package with DiagnosticableTreeMixin implements _Package { _$_Package({required this.name, required this.latest}); factory _$_Package.fromJson(Map json) => @@ -668,10 +703,19 @@ class _$_Package implements _Package { final PackageDetails latest; @override - String toString() { + String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) { return 'Package(name: $name, latest: $latest)'; } + @override + void debugFillProperties(DiagnosticPropertiesBuilder properties) { + super.debugFillProperties(properties); + properties + ..add(DiagnosticsProperty('type', 'Package')) + ..add(DiagnosticsProperty('name', name)) + ..add(DiagnosticsProperty('latest', latest)); + } + @override bool operator ==(dynamic other) { return identical(this, other) || @@ -809,7 +853,7 @@ class __$$_LikedPackageCopyWithImpl<$Res> /// @nodoc @JsonSerializable() -class _$_LikedPackage implements _LikedPackage { +class _$_LikedPackage with DiagnosticableTreeMixin implements _LikedPackage { _$_LikedPackage({required this.package, required this.liked}); factory _$_LikedPackage.fromJson(Map json) => @@ -821,10 +865,19 @@ class _$_LikedPackage implements _LikedPackage { final bool liked; @override - String toString() { + String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) { return 'LikedPackage(package: $package, liked: $liked)'; } + @override + void debugFillProperties(DiagnosticPropertiesBuilder properties) { + super.debugFillProperties(properties); + properties + ..add(DiagnosticsProperty('type', 'LikedPackage')) + ..add(DiagnosticsProperty('package', package)) + ..add(DiagnosticsProperty('liked', liked)); + } + @override bool operator ==(dynamic other) { return identical(this, other) || @@ -954,7 +1007,9 @@ class __$$_LikesPackagesResponseCopyWithImpl<$Res> /// @nodoc @JsonSerializable() -class _$_LikesPackagesResponse implements _LikesPackagesResponse { +class _$_LikesPackagesResponse + with DiagnosticableTreeMixin + implements _LikesPackagesResponse { _$_LikesPackagesResponse({required final List likedPackages}) : _likedPackages = likedPackages; @@ -970,10 +1025,18 @@ class _$_LikesPackagesResponse implements _LikesPackagesResponse { } @override - String toString() { + String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) { return 'LikedPackagesResponse(likedPackages: $likedPackages)'; } + @override + void debugFillProperties(DiagnosticPropertiesBuilder properties) { + super.debugFillProperties(properties); + properties + ..add(DiagnosticsProperty('type', 'LikedPackagesResponse')) + ..add(DiagnosticsProperty('likedPackages', likedPackages)); + } + @override bool operator ==(dynamic other) { return identical(this, other) || @@ -1101,7 +1164,9 @@ class __$$_PubPackagesResponseCopyWithImpl<$Res> /// @nodoc @JsonSerializable() -class _$_PubPackagesResponse implements _PubPackagesResponse { +class _$_PubPackagesResponse + with DiagnosticableTreeMixin + implements _PubPackagesResponse { _$_PubPackagesResponse({required final List packages}) : _packages = packages; @@ -1117,10 +1182,18 @@ class _$_PubPackagesResponse implements _PubPackagesResponse { } @override - String toString() { + String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) { return 'PubPackagesResponse(packages: $packages)'; } + @override + void debugFillProperties(DiagnosticPropertiesBuilder properties) { + super.debugFillProperties(properties); + properties + ..add(DiagnosticsProperty('type', 'PubPackagesResponse')) + ..add(DiagnosticsProperty('packages', packages)); + } + @override bool operator ==(dynamic other) { return identical(this, other) || @@ -1246,7 +1319,7 @@ class __$$_SearchPackageCopyWithImpl<$Res> /// @nodoc @JsonSerializable() -class _$_SearchPackage implements _SearchPackage { +class _$_SearchPackage with DiagnosticableTreeMixin implements _SearchPackage { _$_SearchPackage({required this.package}); factory _$_SearchPackage.fromJson(Map json) => @@ -1256,10 +1329,18 @@ class _$_SearchPackage implements _SearchPackage { final String package; @override - String toString() { + String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) { return 'SearchPackage(package: $package)'; } + @override + void debugFillProperties(DiagnosticPropertiesBuilder properties) { + super.debugFillProperties(properties); + properties + ..add(DiagnosticsProperty('type', 'SearchPackage')) + ..add(DiagnosticsProperty('package', package)); + } + @override bool operator ==(dynamic other) { return identical(this, other) || @@ -1382,7 +1463,9 @@ class __$$_PubSearchResponseCopyWithImpl<$Res> /// @nodoc @JsonSerializable() -class _$_PubSearchResponse implements _PubSearchResponse { +class _$_PubSearchResponse + with DiagnosticableTreeMixin + implements _PubSearchResponse { _$_PubSearchResponse({required final List packages}) : _packages = packages; @@ -1398,10 +1481,18 @@ class _$_PubSearchResponse implements _PubSearchResponse { } @override - String toString() { + String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) { return 'PubSearchResponse(packages: $packages)'; } + @override + void debugFillProperties(DiagnosticPropertiesBuilder properties) { + super.debugFillProperties(properties); + properties + ..add(DiagnosticsProperty('type', 'PubSearchResponse')) + ..add(DiagnosticsProperty('packages', packages)); + } + @override bool operator ==(dynamic other) { return identical(this, other) || diff --git a/packages/riverpod_generator/CHANGELOG.md b/packages/riverpod_generator/CHANGELOG.md index e099973dc..3a7ed1447 100644 --- a/packages/riverpod_generator/CHANGELOG.md +++ b/packages/riverpod_generator/CHANGELOG.md @@ -1,3 +1,8 @@ +## Unreleased patch + +- If a provider has an empty list of dependencies, the generated list is now `const` + (thanks to @K9i-0) + ## 2.1.5 - 2023-04-06 - `riverpod_annotation` upgraded to `2.0.3` diff --git a/packages/riverpod_generator/lib/src/templates/stateful_provider.dart b/packages/riverpod_generator/lib/src/templates/stateful_provider.dart index 35714a097..307261d27 100644 --- a/packages/riverpod_generator/lib/src/templates/stateful_provider.dart +++ b/packages/riverpod_generator/lib/src/templates/stateful_provider.dart @@ -16,7 +16,9 @@ String? serializeDependencies( ) { if (dependencies == null) return 'null'; - final buffer = StringBuffer(''); + final buffer = StringBuffer( + '${dependencies.isEmpty ? 'const ' : ''}', + ); // Use list vs set based on the number of dependencies to optimize "contains" call if (dependencies.length < 4) { buffer.write('['); diff --git a/packages/riverpod_generator/test/dependencies_test.dart b/packages/riverpod_generator/test/dependencies_test.dart index ac19cf67d..cf609a470 100644 --- a/packages/riverpod_generator/test/dependencies_test.dart +++ b/packages/riverpod_generator/test/dependencies_test.dart @@ -1,3 +1,4 @@ +import 'package:riverpod_annotation/riverpod_annotation.dart'; import 'package:test/test.dart'; import 'integration/dependencies.dart'; @@ -27,6 +28,16 @@ void main() { ); expect(transitiveDependenciesProvider.dependencies, [providerProvider]); + + expect( + emptyDependenciesStatelessProvider.dependencies, + same(const []), + ); + + expect( + emptyDependenciesStatefulProvider.dependencies, + same(const []), + ); }); test('Generates transitive dependencies', () { @@ -62,6 +73,16 @@ void main() { family2Provider, ], ); + + expect( + emptyDependenciesStatelessProvider.allTransitiveDependencies, + same(const []), + ); + + expect( + emptyDependenciesStatefulProvider.allTransitiveDependencies, + same(const []), + ); }); test('Caches dependencies', () { @@ -85,6 +106,18 @@ void main() { transitiveDependenciesProvider.dependencies, same(transitiveDependenciesProvider.dependencies), ); + expect( + smallTransitiveDependencyCountProvider.dependencies, + same(smallTransitiveDependencyCountProvider.dependencies), + ); + expect( + emptyDependenciesStatelessProvider.dependencies, + same(emptyDependenciesStatelessProvider.dependencies), + ); + expect( + emptyDependenciesStatefulProvider.dependencies, + same(emptyDependenciesStatefulProvider.dependencies), + ); expect( provider3Provider.allTransitiveDependencies, @@ -98,5 +131,9 @@ void main() { transitiveDependenciesProvider.allTransitiveDependencies, same(transitiveDependenciesProvider.allTransitiveDependencies), ); + expect( + smallTransitiveDependencyCountProvider.allTransitiveDependencies, + same(smallTransitiveDependencyCountProvider.allTransitiveDependencies), + ); }); } diff --git a/packages/riverpod_generator/test/integration/dependencies.dart b/packages/riverpod_generator/test/integration/dependencies.dart index 6b4fa91ef..18212c5f8 100644 --- a/packages/riverpod_generator/test/integration/dependencies.dart +++ b/packages/riverpod_generator/test/integration/dependencies.dart @@ -43,3 +43,12 @@ int transitiveDependencies(TransitiveDependenciesRef ref) => 0; @Riverpod(dependencies: [dep, family, Dep2]) int smallTransitiveDependencyCount(SmallTransitiveDependencyCountRef ref) => 0; + +@Riverpod(dependencies: []) +int emptyDependenciesStateless(EmptyDependenciesStatelessRef ref) => 0; + +@Riverpod(dependencies: []) +class EmptyDependenciesStateful extends _$EmptyDependenciesStateful { + @override + int build() => 0; +} diff --git a/packages/riverpod_generator/test/integration/dependencies.g.dart b/packages/riverpod_generator/test/integration/dependencies.g.dart index c4f9d4b54..80032655d 100644 --- a/packages/riverpod_generator/test/integration/dependencies.g.dart +++ b/packages/riverpod_generator/test/integration/dependencies.g.dart @@ -214,6 +214,22 @@ final smallTransitiveDependencyCountProvider = ); typedef SmallTransitiveDependencyCountRef = AutoDisposeProviderRef; +String _$emptyDependenciesStatelessHash() => + r'2415aab6f03b1cb67fa8fecc5d2af1ec5d261398'; + +/// See also [emptyDependenciesStateless]. +@ProviderFor(emptyDependenciesStateless) +final emptyDependenciesStatelessProvider = AutoDisposeProvider.internal( + emptyDependenciesStateless, + name: r'emptyDependenciesStatelessProvider', + debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') + ? null + : _$emptyDependenciesStatelessHash, + dependencies: const [], + allTransitiveDependencies: const [], +); + +typedef EmptyDependenciesStatelessRef = AutoDisposeProviderRef; String _$dep2Hash() => r'2778537df77f6431148c2ce400724da3e2ab4b94'; /// See also [Dep2]. @@ -451,4 +467,22 @@ class Provider4Provider ); } } + +String _$emptyDependenciesStatefulHash() => + r'7cd5d081bbfb866823b0d493e63bfc63b9d9c804'; + +/// See also [EmptyDependenciesStateful]. +@ProviderFor(EmptyDependenciesStateful) +final emptyDependenciesStatefulProvider = + AutoDisposeNotifierProvider.internal( + EmptyDependenciesStateful.new, + name: r'emptyDependenciesStatefulProvider', + debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') + ? null + : _$emptyDependenciesStatefulHash, + dependencies: const [], + allTransitiveDependencies: const [], +); + +typedef _$EmptyDependenciesStateful = AutoDisposeNotifier; // ignore_for_file: unnecessary_raw_strings, subtype_of_sealed_class, invalid_use_of_internal_member, do_not_use_environment, prefer_const_constructors, public_member_api_docs, avoid_private_typedef_functions diff --git a/packages/riverpod_lint_flutter_test/test/goldens/fixes/provider_dependencies.g.dart b/packages/riverpod_lint_flutter_test/test/goldens/fixes/provider_dependencies.g.dart index 6a7fd9e1d..825d3074a 100644 --- a/packages/riverpod_lint_flutter_test/test/goldens/fixes/provider_dependencies.g.dart +++ b/packages/riverpod_lint_flutter_test/test/goldens/fixes/provider_dependencies.g.dart @@ -15,8 +15,8 @@ final depProvider = AutoDisposeProvider.internal( name: r'depProvider', debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') ? null : _$depHash, - dependencies: [], - allTransitiveDependencies: [], + dependencies: const [], + allTransitiveDependencies: const [], ); typedef DepRef = AutoDisposeProviderRef; @@ -29,8 +29,8 @@ final dep2Provider = AutoDisposeProvider.internal( name: r'dep2Provider', debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') ? null : _$dep2Hash, - dependencies: [], - allTransitiveDependencies: [], + dependencies: const [], + allTransitiveDependencies: const [], ); typedef Dep2Ref = AutoDisposeProviderRef; @@ -91,8 +91,8 @@ final existingDepProvider = AutoDisposeProvider.internal( name: r'existingDepProvider', debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') ? null : _$existingDepHash, - dependencies: [], - allTransitiveDependencies: [], + dependencies: const [], + allTransitiveDependencies: const [], ); typedef ExistingDepRef = AutoDisposeProviderRef; @@ -105,8 +105,8 @@ final multipleDepsProvider = AutoDisposeProvider.internal( name: r'multipleDepsProvider', debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') ? null : _$multipleDepsHash, - dependencies: [], - allTransitiveDependencies: [], + dependencies: const [], + allTransitiveDependencies: const [], ); typedef MultipleDepsRef = AutoDisposeProviderRef; diff --git a/packages/riverpod_lint_flutter_test/test/goldens/lints/dependencies.g.dart b/packages/riverpod_lint_flutter_test/test/goldens/lints/dependencies.g.dart index 0850cd3a8..156da1e4e 100644 --- a/packages/riverpod_lint_flutter_test/test/goldens/lints/dependencies.g.dart +++ b/packages/riverpod_lint_flutter_test/test/goldens/lints/dependencies.g.dart @@ -34,8 +34,8 @@ final depProvider = AutoDisposeProvider.internal( name: r'depProvider', debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') ? null : _$depHash, - dependencies: [], - allTransitiveDependencies: [], + dependencies: const [], + allTransitiveDependencies: const [], ); typedef DepRef = AutoDisposeProviderRef; @@ -49,8 +49,8 @@ final generatedScopedProvider = AutoDisposeProvider.internal( debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') ? null : _$generatedScopedHash, - dependencies: [], - allTransitiveDependencies: [], + dependencies: const [], + allTransitiveDependencies: const [], ); typedef GeneratedScopedRef = AutoDisposeProviderRef; @@ -164,8 +164,8 @@ final watchScopedButEmptyDependenciesProvider = debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') ? null : _$watchScopedButEmptyDependenciesHash, - dependencies: [], - allTransitiveDependencies: [], + dependencies: const [], + allTransitiveDependencies: const [], ); typedef WatchScopedButEmptyDependenciesRef = AutoDisposeProviderRef; @@ -181,8 +181,8 @@ final watchGeneratedScopedButEmptyDependenciesProvider = debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') ? null : _$watchGeneratedScopedButEmptyDependenciesHash, - dependencies: [], - allTransitiveDependencies: [], + dependencies: const [], + allTransitiveDependencies: const [], ); typedef WatchGeneratedScopedButEmptyDependenciesRef @@ -198,8 +198,8 @@ final watchRootButEmptyDependenciesProvider = AutoDisposeProvider.internal( debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') ? null : _$watchRootButEmptyDependenciesHash, - dependencies: [], - allTransitiveDependencies: [], + dependencies: const [], + allTransitiveDependencies: const [], ); typedef WatchRootButEmptyDependenciesRef = AutoDisposeProviderRef; @@ -215,8 +215,8 @@ final watchGeneratedRootButEmptyDependenciesProvider = debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') ? null : _$watchGeneratedRootButEmptyDependenciesHash, - dependencies: [], - allTransitiveDependencies: [], + dependencies: const [], + allTransitiveDependencies: const [], ); typedef WatchGeneratedRootButEmptyDependenciesRef = AutoDisposeProviderRef; @@ -374,8 +374,8 @@ final classWatchGeneratedRootButMissingDependenciesProvider = debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') ? null : _$classWatchGeneratedRootButMissingDependenciesHash, - dependencies: [], - allTransitiveDependencies: [], + dependencies: const [], + allTransitiveDependencies: const [], ); typedef _$ClassWatchGeneratedRootButMissingDependencies @@ -393,8 +393,8 @@ final classWatchGeneratedScopedButMissingDependenciesProvider = debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') ? null : _$classWatchGeneratedScopedButMissingDependenciesHash, - dependencies: [], - allTransitiveDependencies: [], + dependencies: const [], + allTransitiveDependencies: const [], ); typedef _$ClassWatchGeneratedScopedButMissingDependencies diff --git a/packages/riverpod_lint_flutter_test/test/goldens/lints/scoped_providers_should_specify_dependencies.g.dart b/packages/riverpod_lint_flutter_test/test/goldens/lints/scoped_providers_should_specify_dependencies.g.dart index bfa569d60..d77449e36 100644 --- a/packages/riverpod_lint_flutter_test/test/goldens/lints/scoped_providers_should_specify_dependencies.g.dart +++ b/packages/riverpod_lint_flutter_test/test/goldens/lints/scoped_providers_should_specify_dependencies.g.dart @@ -15,8 +15,8 @@ final scopedProvider = AutoDisposeProvider.internal( name: r'scopedProvider', debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') ? null : _$scopedHash, - dependencies: [], - allTransitiveDependencies: [], + dependencies: const [], + allTransitiveDependencies: const [], ); typedef ScopedRef = AutoDisposeProviderRef;