Skip to content

Commit a2ef2c3

Browse files
authored
PartBuilder requireLibraryDirective now defaults to false (#310)
Require at least Dart 2.0.0-dev.19 Prepare to release 0.7.5
1 parent baa15b6 commit a2ef2c3

File tree

6 files changed

+50
-22
lines changed

6 files changed

+50
-22
lines changed

.travis.yml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,12 @@ language: dart
22

33
dart:
44
- dev
5-
- stable
6-
- 1.22.1
5+
# Flutter Alpha @ v0.0.23
6+
- 2.0.0-dev.19.0
77
dart_task:
88
- test
99
- dartfmt
1010
- dartanalyzer
11-
matrix:
12-
exclude:
13-
- dart: 1.22.1
14-
dart_task: dartfmt
15-
- dart: 1.22.1
16-
dart_task: dartanalyzer
1711

1812
# Only building master means that we don't run two builds for each pull request.
1913
branches:

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 0.7.5
2+
3+
* The `PartBuilder` constructor parameter `requireLibraryDirective` now defaults
4+
to `false`. It will be removed in `0.8.0`.
5+
6+
* Require at least Dart `2.0.0-dev.19.0`.
7+
18
## 0.7.4+3
29

310
* Support the latest `analyzer` package.

lib/src/builder.dart

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,24 @@ class _Builder extends Builder {
3737
final Map<String, List<String>> buildExtensions;
3838

3939
/// Wrap [_generators] to form a [Builder]-compatible API.
40-
_Builder(this._generators,
40+
_Builder(
41+
this._generators,
4142
{String formatOutput(String code),
4243
String generatedExtension: '.g.dart',
4344
List<String> additionalOutputExtensions: const [],
4445
bool isStandalone: false,
45-
bool requireLibraryDirective: true,
46+
@Deprecated(
47+
'Library directives are no longer required for part generation. '
48+
'This option will be removed in v0.8.0.')
49+
bool requireLibraryDirective: false,
4650
String header})
4751
: _generatedExtension = generatedExtension,
4852
buildExtensions = {
4953
'.dart': [generatedExtension]..addAll(additionalOutputExtensions)
5054
},
5155
_isStandalone = isStandalone,
5256
formatOutput = formatOutput ?? _formatter.format,
57+
// ignore: deprecated_member_use
5358
_requireLibraryDirective = requireLibraryDirective,
5459
_header = header ?? defaultFileHeader {
5560
if (_generatedExtension == null) {
@@ -167,23 +172,30 @@ class PartBuilder extends _Builder {
167172
/// If `null`, the content of [defaultFileHeader] is used.
168173
/// If [header] is an empty `String` no header is added.
169174
///
170-
/// May set [requireLibraryDirective] to `false` in order to opt-in to
171-
/// supporting a `2.0.0-dev` feature of `part of` being usable without an
172-
/// explicit `library` directive. Developers should restrict their `pubspec`
175+
/// May set [requireLibraryDirective] to `true` in order to opt-out of the
176+
/// Dart `2.0.0-dev` feature of `part of` being usable without an explicit
177+
/// `library` directive. Developers should restrict their `pubspec`
173178
/// accordingly:
174179
/// ```yaml
175180
/// sdk: '>=2.0.0-dev <2.0.0'
176181
/// ```
177-
PartBuilder(List<Generator> generators,
182+
///
183+
/// This option will be removed in version 0.8.0 of `source_gen`.
184+
PartBuilder(
185+
List<Generator> generators,
178186
{String formatOutput(String code),
179187
String generatedExtension: '.g.dart',
180188
List<String> additionalOutputExtensions: const [],
181-
bool requireLibraryDirective: true,
189+
@Deprecated(
190+
'Library directives are no longer required for part generation. '
191+
'This option will be removed in v0.8.0.')
192+
bool requireLibraryDirective: false,
182193
String header})
183194
: super(generators,
184195
formatOutput: formatOutput,
185196
generatedExtension: generatedExtension,
186197
additionalOutputExtensions: additionalOutputExtensions,
198+
// ignore: deprecated_member_use
187199
requireLibraryDirective: requireLibraryDirective,
188200
header: header);
189201
}

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
name: source_gen
2-
version: 0.7.4+3
2+
version: 0.7.5
33
author: Dart Team <[email protected]>
44
description: Automated source code generation for Dart.
55
homepage: https://github.com/dart-lang/source_gen
66
environment:
7-
sdk: '>=1.22.1 <2.0.0'
7+
sdk: '>=2.0.0-dev.19.0 <2.0.0'
88
dependencies:
99
analyzer: '>=0.29.10 <0.32.0'
1010
build: '>=0.10.0 <0.13.0'

test/builder_test.dart

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,21 @@ void main() {
7171
returnsNormally);
7272
});
7373

74-
test('Throws an exception when no library identifier is found', () async {
74+
test('Allow no "library" by default', () async {
7575
var sources = _createPackageStub(pkgName, testLibContent: 'class A {}');
7676
var builder = new PartBuilder([const CommentGenerator()]);
77+
78+
await testBuilder(builder, sources,
79+
outputs: {'$pkgName|lib/test_lib.g.dart': _testGenNoLibrary});
80+
});
81+
82+
test(
83+
'Throws when no library identifier and requireLibraryDirective is `true`',
84+
() async {
85+
var sources = _createPackageStub(pkgName, testLibContent: 'class A {}');
86+
var builder = new PartBuilder([const CommentGenerator()],
87+
// ignore: deprecated_member_use
88+
requireLibraryDirective: true);
7789
expect(
7890
testBuilder(builder, sources,
7991
outputs: {'$pkgName|lib/test_lib.g.dart': ''}),
@@ -86,10 +98,9 @@ void main() {
8698
await testBuilder(builder, sources, outputs: {});
8799
});
88100

89-
test('Allow no "library" when requireLibraryDirective=false', () async {
101+
test('Use new part syntax when no library directive exists', () async {
90102
var sources = _createPackageStub(pkgName, testLibContent: 'class A {}');
91-
var builder = new PartBuilder([const CommentGenerator()],
92-
requireLibraryDirective: false);
103+
var builder = new PartBuilder([const CommentGenerator()]);
93104
await testBuilder(builder, sources,
94105
outputs: {'$pkgName|lib/test_lib.g.dart': _testGenNoLibrary});
95106
});

test/src/comment_generator.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ class CommentGenerator extends Generator {
1717
Future<String> generate(LibraryReader library, _) async {
1818
var output = new StringBuffer();
1919
if (forLibrary) {
20-
output.writeln('// Code for "${library.element}"');
20+
var name = library.element.name;
21+
if (name.isEmpty) {
22+
name = library.element.source.uri.pathSegments.last;
23+
}
24+
output.writeln('// Code for "$name"');
2125
}
2226
if (forClasses) {
2327
for (var classElement

0 commit comments

Comments
 (0)