Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

always use a Uri in part of directives #665

Merged
merged 1 commit into from
May 9, 2023
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
5 changes: 5 additions & 0 deletions source_gen/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions source_gen/lib/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -143,7 +143,7 @@ class CombiningBuilder implements Builder {
final output = '''
$defaultFileHeader
${languageOverrideForLibrary(inputLibrary)}$ignoreForFile$preamble
part of $partOf;
part of '$partOfUri';

$assets
''';
Expand Down
4 changes: 2 additions & 2 deletions source_gen/lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
25 changes: 3 additions & 22 deletions source_gen/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,10 @@ bool hasExpectedPartDirective(CompilationUnit unit, String part) =>
.whereType<PartDirective>()
.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].
Expand Down
2 changes: 1 addition & 1 deletion source_gen/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
16 changes: 8 additions & 8 deletions source_gen/test/builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
''',
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -703,7 +703,7 @@ foo generated content

// bar

part of a;
part of 'a.dart';

bar generated content

Expand Down Expand Up @@ -970,7 +970,7 @@ class MyGoodError { }
''';

const _testLibPartContent = r'''
part of test_lib;
part of 'test_lib.dart';
final int bar = 42;
class Customer { }
''';
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down