Skip to content
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

Add option to disable throw on duplicate dependencies #453

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions injectable/lib/src/injectable_annotations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ class InjectableInit {
/// defaults to false
final bool throwOnMissingDependencies;

/// throw an error and abort generation
/// in case of duplicate dependencies
/// defaults to true
final bool throwOnDuplicateDependencies;

/// a List of external package modules to be registered
/// in the default package initializer before root dependencies
/// classes passed here must extend [MicroPackageModule]
Expand Down Expand Up @@ -89,6 +94,7 @@ class InjectableInit {
this.asExtension = true,
this.usesNullSafety = true,
this.throwOnMissingDependencies = false,
this.throwOnDuplicateDependencies = true,
this.includeMicroPackages = true,
this.externalPackageModulesAfter,
this.externalPackageModulesBefore,
Expand All @@ -104,6 +110,7 @@ class InjectableInit {
this.externalPackageModulesBefore,
this.usesConstructorCallback = false,
this.throwOnMissingDependencies = false,
this.throwOnDuplicateDependencies = true,
this.ignoreUnregisteredTypesInPackages = const [],
this.usesNullSafety = true,
}) : _isMicroPackage = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class InjectableConfigGenerator extends GeneratorForAnnotation<InjectableInit> {
annotation.read('usesConstructorCallback').boolValue;
final throwOnMissingDependencies =
annotation.read('throwOnMissingDependencies').boolValue;
final throwOnDuplicateDependencies =
annotation.read('throwOnDuplicateDependencies').boolValue;
final targetFile = element.source?.uri;
final preferRelativeImports =
annotation.read("preferRelativeImports").boolValue;
Expand Down Expand Up @@ -141,7 +143,10 @@ class InjectableConfigGenerator extends GeneratorForAnnotation<InjectableInit> {
targetFile,
throwOnMissingDependencies,
);
_validateDuplicateDependencies(deps);

if (throwOnDuplicateDependencies) {
_validateDuplicateDependencies(deps);
}

/// don't allow registering of the same dependency with both async and sync factories
final groupedByType = deps.groupListsBy((d) => (d.type, d.instanceName));
Expand Down