diff --git a/pkgs/code_builder/CHANGELOG.md b/pkgs/code_builder/CHANGELOG.md index fa40c8c84..e153bd5e3 100644 --- a/pkgs/code_builder/CHANGELOG.md +++ b/pkgs/code_builder/CHANGELOG.md @@ -1,4 +1,10 @@ -## 4.11.1-wip +## 4.11.1 + +* Convert imports of implementation libraries under `package:fixnum/src/*` into + imports of `package:fixnum/fixnum.dart` to prevent issues when version 1.2.0 + is released with platform-specific implementation libraries that generated + code should not import directly. This is a temporary workaround, it will be + replaced by a new way of managing imports in a future major version release. ## 4.11.0 diff --git a/pkgs/code_builder/lib/src/allocator.dart b/pkgs/code_builder/lib/src/allocator.dart index 2e732b361..c7e821b76 100644 --- a/pkgs/code_builder/lib/src/allocator.dart +++ b/pkgs/code_builder/lib/src/allocator.dart @@ -42,6 +42,11 @@ abstract class Allocator { String allocate(Reference reference); /// All imports that have so far been added implicitly via [allocate]. + /// + /// As a special case, imports of `package:fixnum/src/*` are changed to refer + /// to the public `package:fixnum/fixnum.dart`. Version 1.2.0 of the package + /// introduces platform-specific implementation libraries that must not be + /// imported directly. Iterable get imports; } @@ -50,8 +55,9 @@ class _Allocator implements Allocator { @override String allocate(Reference reference) { - final url = reference.url; + var url = reference.url; if (url != null) { + url = _fixUrl(url); _imports.add(url); } return reference.symbol!; @@ -80,10 +86,11 @@ class _PrefixedAllocator implements Allocator { @override String allocate(Reference reference) { final symbol = reference.symbol; - final url = reference.url; + var url = reference.url; if (url == null || _doNotPrefix.contains(url)) { return symbol!; } + url = _fixUrl(url); return '_i${_imports.putIfAbsent(url, _nextKey)}.$symbol'; } @@ -93,3 +100,13 @@ class _PrefixedAllocator implements Allocator { Iterable get imports => _imports.keys.map((u) => Directive.import(u, as: '_i${_imports[u]}')); } + +/// Applies hardcoded fixes to [url]. +/// +/// See [Allocator.imports] for explanations. +String _fixUrl(String url) { + if (url.startsWith('package:fixnum/src/')) { + return 'package:fixnum/fixnum.dart'; + } + return url; +} diff --git a/pkgs/code_builder/pubspec.yaml b/pkgs/code_builder/pubspec.yaml index d551bcbf7..b1e184146 100644 --- a/pkgs/code_builder/pubspec.yaml +++ b/pkgs/code_builder/pubspec.yaml @@ -1,5 +1,5 @@ name: code_builder -version: 4.11.1-wip +version: 4.11.1 description: A fluent, builder-based library for generating valid Dart code. repository: https://github.com/dart-lang/tools/tree/main/pkgs/code_builder issue_tracker: https://github.com/dart-lang/tools/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Acode_builder diff --git a/pkgs/code_builder/test/allocator_test.dart b/pkgs/code_builder/test/allocator_test.dart index 71c93cbf2..7e089f6b5 100644 --- a/pkgs/code_builder/test/allocator_test.dart +++ b/pkgs/code_builder/test/allocator_test.dart @@ -30,6 +30,15 @@ void main() { ]); }); + test('.simple replaces fixnum internal URIs', () { + allocator = + Allocator() + ..allocate(refer('Int64', 'package:fixnum/src/int64_native.dart')); + expect(allocator.imports.map((d) => d.url), [ + 'package:fixnum/fixnum.dart', + ]); + }); + test('.none should do nothing', () { allocator = Allocator.none; expect(allocator.allocate(refer('Foo', 'package:foo')), 'Foo'); @@ -47,5 +56,14 @@ void main() { 'dart:collection as _i1', ]); }); + + test('.simplePrefixing replaces fixnum internal URIs', () { + allocator = + Allocator.simplePrefixing() + ..allocate(refer('Int64', 'package:fixnum/src/int64_native.dart')); + expect(allocator.imports.map((d) => d.url), [ + 'package:fixnum/fixnum.dart', + ]); + }); }); }