Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

Commit dd2180c

Browse files
authored
Add '_i' to import prefixes (#160)
This matches old behavior and is a little more clear than just a digit. This will also satisfy the lint on the latest SDK. See https://github.com/dart-lang/linter/issues/684#issuecomment-343543273
1 parent b99d416 commit dd2180c

File tree

8 files changed

+24
-16
lines changed

8 files changed

+24
-16
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.1.1-dev
2+
3+
* Imports are prefixed with `_i1` rather than `_1` which satisfies the lint
4+
`lowercase_with_underscores`.
5+
16
## 2.1.0
27

38
We now require the Dart 2.0-dev branch SDK (`>= 2.0.0-dev`).

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ void main() {
8383

8484
Outputs:
8585
```dart
86-
import 'package:a/a.dart' as _1;
87-
import 'package:b/b.dart' as _2;
86+
import 'package:a/a.dart' as _i1;
87+
import 'package:b/b.dart' as _i2;
8888
89-
_1.Thing doThing() {}
90-
_2.Other doOther() {}
91-
```
89+
_i1.Thing doThing() {}
90+
_i2.Other doOther() {}
91+
```

lib/src/allocator.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ abstract class Allocator {
2323
/// style and instead takes a conservative approach of prefixing _every_
2424
/// import except references to `dart:core` (which are considered always
2525
/// imported).
26+
///
27+
/// The prefixes are not guaranteed to be stable and cannot be expected to
28+
/// have any particular value.
2629
factory Allocator.simplePrefixing() = _PrefixedAllocator;
2730

2831
/// Returns a reference string given a [reference] object.
@@ -81,15 +84,15 @@ class _PrefixedAllocator implements Allocator {
8184
if (reference.url == null || _doNotPrefix.contains(reference.url)) {
8285
return symbol;
8386
}
84-
return '_${_imports.putIfAbsent(reference.url, _nextKey)}.$symbol';
87+
return '_i${_imports.putIfAbsent(reference.url, _nextKey)}.$symbol';
8588
}
8689

8790
int _nextKey() => _keys++;
8891

8992
@override
9093
Iterable<Directive> get imports {
9194
return _imports.keys.map(
92-
(u) => new Directive.import(u, as: '_${_imports[u]}'),
95+
(u) => new Directive.import(u, as: '_i${_imports[u]}'),
9396
);
9497
}
9598
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: code_builder
2-
version: 2.1.0
2+
version: 2.1.1-dev
33
description: A fluent API for generating Dart code
44
author: Dart Team <[email protected]>
55
homepage: https://github.com/dart-lang/code_builder

test/allocator_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ void main() {
3939
);
4040
expect(
4141
allocator.allocate(refer('LinkedHashMap', 'dart:collection')),
42-
'_1.LinkedHashMap',
42+
'_i1.LinkedHashMap',
4343
);
4444
expect(allocator.imports.map((d) => '${d.url} as ${d.as}'), [
45-
'dart:collection as _1',
45+
'dart:collection as _i1',
4646
]);
4747
});
4848
});

test/e2e/injection_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ void main() {
5151
expect(
5252
clazz.build(),
5353
equalsDart(r'''
54-
class Injector implements _1.App {
54+
class Injector implements _i1.App {
5555
Injector(this._module);
5656
57-
final _2.Module _module;
57+
final _i2.Module _module;
5858
5959
@override
60-
_3.Thing getThing() => new _3.Thing(_module.get1(), _module.get2());
60+
_i3.Thing getThing() => new _i3.Thing(_module.get1(), _module.get2());
6161
}
6262
''', new DartEmitter(new Allocator.simplePrefixing())),
6363
);

test/specs/code/expression_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void main() {
8383
test('should emit a scoped type as an expression', () {
8484
expect(
8585
refer('Foo', 'package:foo/foo.dart'),
86-
equalsDart('_1.Foo', new DartEmitter(new Allocator.simplePrefixing())),
86+
equalsDart('_i1.Foo', new DartEmitter(new Allocator.simplePrefixing())),
8787
);
8888
});
8989

test/specs/library_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ void main() {
101101
..assignment =
102102
new Code.scope((a) => 'new ${a($LinkedHashMap)}()')))),
103103
equalsDart(r'''
104-
import 'dart:collection' as _1;
104+
import 'dart:collection' as _i1;
105105
106-
final test = new _1.LinkedHashMap();
106+
final test = new _i1.LinkedHashMap();
107107
''', new DartEmitter(new Allocator.simplePrefixing())),
108108
);
109109
});

0 commit comments

Comments
 (0)