Skip to content

Commit 3d1f86b

Browse files
authored
always use a Uri in part of directives (#665)
Closes google/json_serializable.dart#1313
1 parent de29956 commit 3d1f86b

File tree

6 files changed

+21
-35
lines changed

6 files changed

+21
-35
lines changed

source_gen/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.3.1
2+
3+
* Always use a Uri in `part of` directives (previously a name would be used if
4+
the library had a non-empty one).
5+
16
## 1.3.0
27

38
* Add support for `build_extensions` configuration of builders producing

source_gen/lib/builder.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class CombiningBuilder implements Builder {
120120

121121
final inputLibrary = await buildStep.inputLibrary;
122122
final outputId = buildStep.allowedOutputs.single;
123-
final partOf = nameOfPartial(inputLibrary, buildStep.inputId, outputId);
123+
final partOfUri = uriOfPartial(inputLibrary, buildStep.inputId, outputId);
124124

125125
// Ensure that the input has a correct `part` statement.
126126
final libraryUnit =
@@ -143,7 +143,7 @@ class CombiningBuilder implements Builder {
143143
final output = '''
144144
$defaultFileHeader
145145
${languageOverrideForLibrary(inputLibrary)}$ignoreForFile$preamble
146-
part of $partOf;
146+
part of '$partOfUri';
147147
148148
$assets
149149
''';

source_gen/lib/src/builder.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,13 @@ class _Builder extends Builder {
124124

125125
if (!_isLibraryBuilder) {
126126
final asset = buildStep.inputId;
127-
final name = nameOfPartial(library, asset, outputId);
127+
final partOfUri = uriOfPartial(library, asset, outputId);
128128
contentBuffer.writeln();
129129

130130
if (this is PartBuilder) {
131131
contentBuffer
132132
..write(languageOverrideForLibrary(library))
133-
..writeln('part of $name;');
133+
..writeln('part of \'$partOfUri\';');
134134
final part = computePartUrl(buildStep.inputId, outputId);
135135

136136
final libraryUnit =

source_gen/lib/src/utils.dart

+3-22
Original file line numberDiff line numberDiff line change
@@ -43,29 +43,10 @@ bool hasExpectedPartDirective(CompilationUnit unit, String part) =>
4343
.whereType<PartDirective>()
4444
.any((e) => e.uri.stringValue == part);
4545

46-
/// Returns a name suitable for `part of "..."` when pointing to [element].
47-
String nameOfPartial(LibraryElement element, AssetId source, AssetId output) {
48-
if (element.name.isNotEmpty) {
49-
return element.name;
50-
}
51-
46+
/// Returns a uri suitable for `part of "..."` when pointing to [element].
47+
String uriOfPartial(LibraryElement element, AssetId source, AssetId output) {
5248
assert(source.package == output.package);
53-
final relativeSourceUri =
54-
p.url.relative(source.path, from: p.url.dirname(output.path));
55-
return '\'$relativeSourceUri\'';
56-
}
57-
58-
/// Returns a suggested library identifier based on [source] path and package.
59-
String suggestLibraryName(AssetId source) {
60-
// lib/test.dart --> [lib/test.dart]
61-
final parts = source.path.split('/');
62-
// [lib/test.dart] --> [lib/test]
63-
parts[parts.length - 1] = parts.last.split('.').first;
64-
// [lib/test] --> [test]
65-
if (parts.first == 'lib') {
66-
parts.removeAt(0);
67-
}
68-
return '${source.package}.${parts.join('.')}';
49+
return p.url.relative(source.path, from: p.url.dirname(output.path));
6950
}
7051

7152
/// Returns what 'part "..."' URL is needed to import [output] from [input].

source_gen/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: source_gen
2-
version: 1.3.0
2+
version: 1.3.1
33
description: >-
44
Source code generation builders and utilities for the Dart build system
55
repository: https://github.com/dart-lang/source_gen/tree/master/source_gen

source_gen/test/builder_test.dart

+8-8
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ part "a.g.dart";
519519
// GENERATED CODE - DO NOT MODIFY BY HAND
520520
// @dart=2.12
521521
522-
part of a;
522+
part of 'a.dart';
523523
524524
some generated content
525525
''',
@@ -668,7 +668,7 @@ some generated content
668668
r'''
669669
// ignore_for_file: lines_longer_than_80_chars, prefer_expression_function_bodies
670670
671-
part of a;
671+
part of 'a.dart';
672672
673673
bar generated content
674674
@@ -703,7 +703,7 @@ foo generated content
703703
704704
// bar
705705
706-
part of a;
706+
part of 'a.dart';
707707
708708
bar generated content
709709
@@ -970,7 +970,7 @@ class MyGoodError { }
970970
''';
971971

972972
const _testLibPartContent = r'''
973-
part of test_lib;
973+
part of 'test_lib.dart';
974974
final int bar = 42;
975975
class Customer { }
976976
''';
@@ -987,7 +987,7 @@ final int foo = 42
987987

988988
const _testGenPartContent = r'''// GENERATED CODE - DO NOT MODIFY BY HAND
989989
990-
part of test_lib;
990+
part of 'test_lib.dart';
991991
992992
// **************************************************************************
993993
// CommentGenerator
@@ -1000,7 +1000,7 @@ part of test_lib;
10001000
const _testGenPartContentForLibrary =
10011001
r'''// GENERATED CODE - DO NOT MODIFY BY HAND
10021002
1003-
part of test_lib;
1003+
part of 'test_lib.dart';
10041004
10051005
// **************************************************************************
10061006
// CommentGenerator
@@ -1022,7 +1022,7 @@ const _testGenStandaloneContent = r'''// GENERATED CODE - DO NOT MODIFY BY HAND
10221022
const _testGenPartContentForClassesAndLibrary =
10231023
r'''// GENERATED CODE - DO NOT MODIFY BY HAND
10241024
1025-
part of test_lib;
1025+
part of 'test_lib.dart';
10261026
10271027
// **************************************************************************
10281028
// CommentGenerator
@@ -1047,7 +1047,7 @@ part of 'test_lib.dart';
10471047

10481048
const _whitespaceTrimmed = r'''// GENERATED CODE - DO NOT MODIFY BY HAND
10491049
1050-
part of test_lib;
1050+
part of 'test_lib.dart';
10511051
10521052
// **************************************************************************
10531053
// Generator: Literal

0 commit comments

Comments
 (0)