Skip to content

Commit

Permalink
Revive to the object's variableElement if available (#713)
Browse files Browse the repository at this point in the history
Variables have been tracked on DartObject since https://dart.googlesource.com/sdk/+/54b7f4b72a1701f8f9a0334c94ce6f59732bd261. This change uses the variable in the reviver if it exists. I believe the existing tests in https://github.com/dart-lang/source_gen/blob/master/source_gen/test/constants_test.dart already cover this, but lmk if you'd like any additional tests
  • Loading branch information
mattrberry authored Jun 9, 2024
1 parent a1f8bc6 commit ac1837f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion source_gen/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
- Document deduplication behavior for the output of
`GeneratorForAnnotation.generateForAnnotatedElement`.
- Support all the glob quotes.
- Require Dart 3.4.0
- Revive to the object's `variableElement` if available
- Require `analyzer: ^6.4.0`
- Require Dart 3.4.0

## 1.5.0

Expand Down
15 changes: 15 additions & 0 deletions source_gen/lib/src/constants/revive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ import '../utils.dart';
/// Dart source code (such as referencing private constructors). It is up to the
/// build tool(s) using this library to surface error messages to the user.
Revivable reviveInstance(DartObject object, [LibraryElement? origin]) {
final variableElement = object.variable;
if (variableElement != null &&
variableElement.isConst &&
variableElement.isPublic) {
final url = Uri.parse(urlOfElement(variableElement)).removeFragment();
if (variableElement.enclosingElement
case final TypeDefiningElement enclosingElement?) {
return Revivable._(
source: url,
accessor: '${enclosingElement.name}.${variableElement.name}',
);
}
return Revivable._(source: url, accessor: variableElement.name);
}

final objectType = object.type;
Element? element = objectType!.alias?.element;
if (element == null) {
Expand Down
11 changes: 11 additions & 0 deletions source_gen/test/constants_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ void main() {
@_privateField
@Wrapper(_privateFunction)
@ProcessStartMode.normal
@ExtensionTypeWithStaticField.staticField
class Example {}
class Int64Like implements Int64LikeBase{
Expand Down Expand Up @@ -296,6 +297,10 @@ void main() {
}
void _privateFunction() {}
extension type const ExtensionTypeWithStaticField._(int _) {
static const staticField = ExtensionTypeWithStaticField._(1);
}
''',
(resolver) async => (await resolver.findLibraryByName('test_lib'))!,
);
Expand Down Expand Up @@ -393,5 +398,11 @@ void main() {
expect(staticFieldWithPrivateImpl.isPrivate, isFalse);
expect(staticFieldWithPrivateImpl.source.fragment, isEmpty);
});

test('should decode static fields on extension types', () {
final fieldOnly = constants[14].revive();
expect(fieldOnly.source.fragment, isEmpty);
expect(fieldOnly.accessor, 'ExtensionTypeWithStaticField.staticField');
});
});
}

0 comments on commit ac1837f

Please sign in to comment.