diff --git a/source_gen/source_gen/source_gen/CHANGELOG.md b/source_gen/source_gen/source_gen/CHANGELOG.md index 82735ab51..6bd6b8ad9 100644 --- a/source_gen/source_gen/source_gen/CHANGELOG.md +++ b/source_gen/source_gen/source_gen/CHANGELOG.md @@ -1,3 +1,8 @@ +## 1.3.1 + +* Always use a Uri in `part of` directives (previously a name would be used if + the library had a non-empty one). + ## 1.3.0 * Add support for `build_extensions` configuration of builders producing diff --git a/source_gen/source_gen/source_gen/lib/builder.dart b/source_gen/source_gen/source_gen/lib/builder.dart index 80688b320..7a7a3b202 100644 --- a/source_gen/source_gen/source_gen/lib/builder.dart +++ b/source_gen/source_gen/source_gen/lib/builder.dart @@ -120,7 +120,7 @@ class CombiningBuilder implements Builder { final inputLibrary = await buildStep.inputLibrary; final outputId = buildStep.allowedOutputs.single; - final partOf = nameOfPartial(inputLibrary, buildStep.inputId, outputId); + final partOfUri = uriOfPartial(inputLibrary, buildStep.inputId, outputId); // Ensure that the input has a correct `part` statement. final libraryUnit = @@ -143,7 +143,7 @@ class CombiningBuilder implements Builder { final output = ''' $defaultFileHeader ${languageOverrideForLibrary(inputLibrary)}$ignoreForFile$preamble -part of $partOf; +part of '$partOfUri'; $assets '''; diff --git a/source_gen/source_gen/source_gen/lib/src/builder.dart b/source_gen/source_gen/source_gen/lib/src/builder.dart index 6c5054196..e3808282f 100644 --- a/source_gen/source_gen/source_gen/lib/src/builder.dart +++ b/source_gen/source_gen/source_gen/lib/src/builder.dart @@ -124,13 +124,13 @@ class _Builder extends Builder { if (!_isLibraryBuilder) { final asset = buildStep.inputId; - final name = nameOfPartial(library, asset, outputId); + final partOfUri = uriOfPartial(library, asset, outputId); contentBuffer.writeln(); if (this is PartBuilder) { contentBuffer ..write(languageOverrideForLibrary(library)) - ..writeln('part of $name;'); + ..writeln('part of \'$partOfUri\';'); final part = computePartUrl(buildStep.inputId, outputId); final libraryUnit = diff --git a/source_gen/source_gen/source_gen/lib/src/utils.dart b/source_gen/source_gen/source_gen/lib/src/utils.dart index 1ad029165..f15fd596b 100644 --- a/source_gen/source_gen/source_gen/lib/src/utils.dart +++ b/source_gen/source_gen/source_gen/lib/src/utils.dart @@ -43,29 +43,10 @@ bool hasExpectedPartDirective(CompilationUnit unit, String part) => .whereType() .any((e) => e.uri.stringValue == part); -/// Returns a name suitable for `part of "..."` when pointing to [element]. -String nameOfPartial(LibraryElement element, AssetId source, AssetId output) { - if (element.name.isNotEmpty) { - return element.name; - } - +/// Returns a uri suitable for `part of "..."` when pointing to [element]. +String uriOfPartial(LibraryElement element, AssetId source, AssetId output) { assert(source.package == output.package); - final relativeSourceUri = - p.url.relative(source.path, from: p.url.dirname(output.path)); - return '\'$relativeSourceUri\''; -} - -/// Returns a suggested library identifier based on [source] path and package. -String suggestLibraryName(AssetId source) { - // lib/test.dart --> [lib/test.dart] - final parts = source.path.split('/'); - // [lib/test.dart] --> [lib/test] - parts[parts.length - 1] = parts.last.split('.').first; - // [lib/test] --> [test] - if (parts.first == 'lib') { - parts.removeAt(0); - } - return '${source.package}.${parts.join('.')}'; + return p.url.relative(source.path, from: p.url.dirname(output.path)); } /// Returns what 'part "..."' URL is needed to import [output] from [input]. diff --git a/source_gen/source_gen/source_gen/pubspec.yaml b/source_gen/source_gen/source_gen/pubspec.yaml index b841ab869..19def4ee3 100644 --- a/source_gen/source_gen/source_gen/pubspec.yaml +++ b/source_gen/source_gen/source_gen/pubspec.yaml @@ -1,5 +1,5 @@ name: source_gen -version: 1.3.0 +version: 1.3.1 description: >- Source code generation builders and utilities for the Dart build system repository: https://github.com/dart-lang/source_gen/tree/master/source_gen diff --git a/source_gen/source_gen/source_gen/test/builder_test.dart b/source_gen/source_gen/source_gen/test/builder_test.dart index cf53fda3a..9a0198027 100644 --- a/source_gen/source_gen/source_gen/test/builder_test.dart +++ b/source_gen/source_gen/source_gen/test/builder_test.dart @@ -519,7 +519,7 @@ part "a.g.dart"; // GENERATED CODE - DO NOT MODIFY BY HAND // @dart=2.12 -part of a; +part of 'a.dart'; some generated content ''', @@ -668,7 +668,7 @@ some generated content r''' // ignore_for_file: lines_longer_than_80_chars, prefer_expression_function_bodies -part of a; +part of 'a.dart'; bar generated content @@ -703,7 +703,7 @@ foo generated content // bar -part of a; +part of 'a.dart'; bar generated content @@ -970,7 +970,7 @@ class MyGoodError { } '''; const _testLibPartContent = r''' -part of test_lib; +part of 'test_lib.dart'; final int bar = 42; class Customer { } '''; @@ -987,7 +987,7 @@ final int foo = 42 const _testGenPartContent = r'''// GENERATED CODE - DO NOT MODIFY BY HAND -part of test_lib; +part of 'test_lib.dart'; // ************************************************************************** // CommentGenerator @@ -1000,7 +1000,7 @@ part of test_lib; const _testGenPartContentForLibrary = r'''// GENERATED CODE - DO NOT MODIFY BY HAND -part of test_lib; +part of 'test_lib.dart'; // ************************************************************************** // CommentGenerator @@ -1022,7 +1022,7 @@ const _testGenStandaloneContent = r'''// GENERATED CODE - DO NOT MODIFY BY HAND const _testGenPartContentForClassesAndLibrary = r'''// GENERATED CODE - DO NOT MODIFY BY HAND -part of test_lib; +part of 'test_lib.dart'; // ************************************************************************** // CommentGenerator @@ -1047,7 +1047,7 @@ part of 'test_lib.dart'; const _whitespaceTrimmed = r'''// GENERATED CODE - DO NOT MODIFY BY HAND -part of test_lib; +part of 'test_lib.dart'; // ************************************************************************** // Generator: Literal