From 783865ca76bfd580f62e8f997f0a33f302b708d9 Mon Sep 17 00:00:00 2001 From: Alberto Montes de Oca Date: Wed, 15 May 2024 12:06:35 +0200 Subject: [PATCH] New HashedAllocator to avoid alias changes --- .../injectable_config_generator.dart | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/injectable_generator/lib/generators/injectable_config_generator.dart b/injectable_generator/lib/generators/injectable_config_generator.dart index 6ca915c..1097959 100644 --- a/injectable_generator/lib/generators/injectable_config_generator.dart +++ b/injectable_generator/lib/generators/injectable_config_generator.dart @@ -168,7 +168,7 @@ class InjectableConfigGenerator extends GeneratorForAnnotation { final generatedLib = generator.generate(); final emitter = DartEmitter( - allocator: Allocator.simplePrefixing(), + allocator: _HashedAllocator(), orderDirectives: true, useNullSafetySyntax: usesNullSafety, ); @@ -309,3 +309,28 @@ class InjectableConfigGenerator extends GeneratorForAnnotation { } } } + +class _HashedAllocator implements Allocator { + static const _doNotPrefix = ['dart:core']; + + final _imports = {}; + + String? _url; + @override + String allocate(Reference reference) { + final symbol = reference.symbol; + _url = reference.url; + if (_url == null || _doNotPrefix.contains(_url)) { + return symbol!; + } + + return '_i${_imports.putIfAbsent(_url!, _hashedUrl)}.$symbol'; + } + + int _hashedUrl() => _url.hashCode; + + @override + Iterable get imports => _imports.keys.map( + (u) => Directive.import(u, as: '_i${_imports[u]}'), + ); +}