Skip to content

Commit

Permalink
New HashedAllocator to avoid alias changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jamontes79 committed May 15, 2024
1 parent 00974a8 commit 783865c
Showing 1 changed file with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class InjectableConfigGenerator extends GeneratorForAnnotation<InjectableInit> {

final generatedLib = generator.generate();
final emitter = DartEmitter(
allocator: Allocator.simplePrefixing(),
allocator: _HashedAllocator(),
orderDirectives: true,
useNullSafetySyntax: usesNullSafety,
);
Expand Down Expand Up @@ -309,3 +309,28 @@ class InjectableConfigGenerator extends GeneratorForAnnotation<InjectableInit> {
}
}
}

class _HashedAllocator implements Allocator {
static const _doNotPrefix = ['dart:core'];

final _imports = <String, int>{};

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<Directive> get imports => _imports.keys.map(
(u) => Directive.import(u, as: '_i${_imports[u]}'),
);
}

0 comments on commit 783865c

Please sign in to comment.