Skip to content

Commit caffaef

Browse files
authored
Update docs for reviveInstance. (#291)
1 parent 8af5a7e commit caffaef

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

lib/src/constants/revive.dart

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ import 'package:analyzer/src/dart/constant/value.dart';
99

1010
import '../utils.dart';
1111

12-
/// Returns a revivable instance of [object].
12+
/// Attempts to extract what source code could be used to represent [object].
1313
///
14-
/// Optionally specify the [library] type that contains the reference.
14+
/// Returns `null` if it wasn't possible to parse [object], or [object] is a
15+
/// primitive value (such as a number, string, boolean) that does not need to be
16+
/// revived in order to represent it.
1517
///
16-
/// Returns `null` if not revivable.
17-
Revivable reviveInstance(DartObject object, [LibraryElement library]) {
18-
library ??= object.type.element.library;
18+
/// **NOTE**: Some returned [Revivable] instances are not representable as valid
19+
/// Dart source code (such as referencing private constructors). It is up to the
20+
/// build tool(s) using this library to surface error messages to the user.
21+
Revivable reviveInstance(DartObject object, [LibraryElement origin]) {
22+
origin ??= object.type.element.library;
1923
var url = Uri.parse(urlOfElement(object.type.element));
2024
final clazz = object?.type?.element as ClassElement;
2125
for (final e in clazz.fields.where(
@@ -35,14 +39,14 @@ Revivable reviveInstance(DartObject object, [LibraryElement library]) {
3539
positionalArguments: i.positionalArguments,
3640
);
3741
}
38-
if (library == null) {
42+
if (origin == null) {
3943
return null;
4044
}
41-
for (final e in library.definingCompilationUnit.topLevelVariables.where(
45+
for (final e in origin.definingCompilationUnit.topLevelVariables.where(
4246
(f) => f.isPublic && f.isConst && f.computeConstantValue() == object,
4347
)) {
4448
return new Revivable._(
45-
source: Uri.parse(urlOfElement(library)).replace(fragment: ''),
49+
source: Uri.parse(urlOfElement(origin)).replace(fragment: ''),
4650
accessor: e.name,
4751
);
4852
}
@@ -55,7 +59,9 @@ class Revivable {
5559
///
5660
/// For example, `LinkedHashMap` looks like: `dart:collection#LinkedHashMap`.
5761
///
58-
/// An accessor to a top-level do does not have a fragment.
62+
/// An accessor to a top-level field or method does not have a fragment and
63+
/// is instead represented as just something like `dart:collection`, with the
64+
/// [accessor] field as the name of the symbol.
5965
final Uri source;
6066

6167
/// Constructor or getter name used to invoke `const Class(...)`.
@@ -77,5 +83,8 @@ class Revivable {
7783
});
7884

7985
/// Whether this instance is visible outside the same library.
86+
///
87+
/// Builds tools may use this to fail when the symbol is expected to be
88+
/// importable (i.e. isn't used with `part of`).
8089
bool get isPrivate => accessor.startsWith('_');
8190
}

0 commit comments

Comments
 (0)