Skip to content
Merged
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
8 changes: 7 additions & 1 deletion pkgs/code_builder/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
21 changes: 19 additions & 2 deletions pkgs/code_builder/lib/src/allocator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Directive> get imports;
}

Expand All @@ -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!;
Expand Down Expand Up @@ -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';
}

Expand All @@ -93,3 +100,13 @@ class _PrefixedAllocator implements Allocator {
Iterable<Directive> 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;
}
2 changes: 1 addition & 1 deletion pkgs/code_builder/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 18 additions & 0 deletions pkgs/code_builder/test/allocator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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',
]);
});
});
}