Skip to content

Commit ac1837f

Browse files
authored
Revive to the object's variableElement if available (#713)
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
1 parent a1f8bc6 commit ac1837f

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

source_gen/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
- Document deduplication behavior for the output of
44
`GeneratorForAnnotation.generateForAnnotatedElement`.
55
- Support all the glob quotes.
6-
- Require Dart 3.4.0
6+
- Revive to the object's `variableElement` if available
77
- Require `analyzer: ^6.4.0`
8+
- Require Dart 3.4.0
89

910
## 1.5.0
1011

source_gen/lib/src/constants/revive.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@ import '../utils.dart';
2020
/// Dart source code (such as referencing private constructors). It is up to the
2121
/// build tool(s) using this library to surface error messages to the user.
2222
Revivable reviveInstance(DartObject object, [LibraryElement? origin]) {
23+
final variableElement = object.variable;
24+
if (variableElement != null &&
25+
variableElement.isConst &&
26+
variableElement.isPublic) {
27+
final url = Uri.parse(urlOfElement(variableElement)).removeFragment();
28+
if (variableElement.enclosingElement
29+
case final TypeDefiningElement enclosingElement?) {
30+
return Revivable._(
31+
source: url,
32+
accessor: '${enclosingElement.name}.${variableElement.name}',
33+
);
34+
}
35+
return Revivable._(source: url, accessor: variableElement.name);
36+
}
37+
2338
final objectType = object.type;
2439
Element? element = objectType!.alias?.element;
2540
if (element == null) {

source_gen/test/constants_test.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ void main() {
227227
@_privateField
228228
@Wrapper(_privateFunction)
229229
@ProcessStartMode.normal
230+
@ExtensionTypeWithStaticField.staticField
230231
class Example {}
231232
232233
class Int64Like implements Int64LikeBase{
@@ -296,6 +297,10 @@ void main() {
296297
}
297298
298299
void _privateFunction() {}
300+
301+
extension type const ExtensionTypeWithStaticField._(int _) {
302+
static const staticField = ExtensionTypeWithStaticField._(1);
303+
}
299304
''',
300305
(resolver) async => (await resolver.findLibraryByName('test_lib'))!,
301306
);
@@ -393,5 +398,11 @@ void main() {
393398
expect(staticFieldWithPrivateImpl.isPrivate, isFalse);
394399
expect(staticFieldWithPrivateImpl.source.fragment, isEmpty);
395400
});
401+
402+
test('should decode static fields on extension types', () {
403+
final fieldOnly = constants[14].revive();
404+
expect(fieldOnly.source.fragment, isEmpty);
405+
expect(fieldOnly.accessor, 'ExtensionTypeWithStaticField.staticField');
406+
});
396407
});
397408
}

0 commit comments

Comments
 (0)