Skip to content

Commit 0cff6c2

Browse files
authored
Convert complex late final fields to getters: InheritingContainer, Library, and Package (#3519)
1 parent 2943545 commit 0cff6c2

File tree

3 files changed

+41
-36
lines changed

3 files changed

+41
-36
lines changed

lib/src/model/inheriting_container.dart

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ mixin Constructable on InheritingContainer {
7979
abstract class InheritingContainer extends Container
8080
with ExtensionTarget
8181
implements EnclosedElement {
82-
late final DefinedElementType? supertype = () {
82+
DefinedElementType? get supertype {
8383
final elementSupertype = element.supertype;
8484
return elementSupertype == null ||
8585
elementSupertype.element.supertype == null
8686
? null
8787
: modelBuilder.typeFrom(elementSupertype, library)
8888
as DefinedElementType;
89-
}();
89+
}
9090

9191
/// Class modifiers from the Dart feature specification.
9292
///
@@ -112,7 +112,7 @@ abstract class InheritingContainer extends Container
112112
...typeParameters,
113113
];
114114

115-
late final Iterable<Method> inheritedMethods = () {
115+
Iterable<Method> get inheritedMethods {
116116
var methodNames = declaredMethods.map((m) => m.element.name).toSet();
117117
var inheritedMethodElements = _inheritedElements
118118
.whereType<MethodElement>()
@@ -126,8 +126,9 @@ abstract class InheritingContainer extends Container
126126
for (var e in inheritedMethodElements)
127127
modelBuilder.from(e, library, enclosingContainer: this) as Method,
128128
];
129-
}();
130-
late final List<Operator> inheritedOperators = () {
129+
}
130+
131+
List<Operator> get inheritedOperators {
131132
var operatorNames = declaredOperators.map((o) => o.element.name).toSet();
132133
var inheritedOperatorElements = _inheritedElements
133134
.whereType<MethodElement>()
@@ -138,13 +139,16 @@ abstract class InheritingContainer extends Container
138139
for (var e in inheritedOperatorElements)
139140
modelBuilder.from(e, library, enclosingContainer: this) as Operator,
140141
];
141-
}();
142+
}
143+
142144
@override
143145
late final DefinedElementType modelType =
144146
modelBuilder.typeFrom(element.thisType, library) as DefinedElementType;
147+
145148
late final List<DefinedElementType> publicSuperChain =
146149
model_utils.filterNonPublic(superChain).toList(growable: false);
147-
late final List<ExecutableElement> _inheritedElements = () {
150+
151+
List<ExecutableElement> get _inheritedElements {
148152
if (element is ClassElement && (element as ClassElement).isDartCoreObject) {
149153
return const <ExecutableElement>[];
150154
}
@@ -189,12 +193,12 @@ abstract class InheritingContainer extends Container
189193
}
190194

191195
return combinedMap.values.toList(growable: false);
192-
}();
196+
}
193197

194198
static final InheritanceManager3 _inheritanceManager = InheritanceManager3();
195199

196200
/// All fields defined on this container, _including inherited fields_.
197-
late final List<Field> allFields = () {
201+
List<Field> get allFields {
198202
var inheritedAccessorElements = {
199203
..._inheritedElements.whereType<PropertyAccessorElement>()
200204
};
@@ -244,7 +248,7 @@ abstract class InheritingContainer extends Container
244248
});
245249

246250
return fields;
247-
}();
251+
}
248252

249253
@override
250254
late final Iterable<Method> declaredMethods =

lib/src/model/library.dart

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ class Library extends ModelElement
294294

295295
/// Map of each import prefix ('import "foo" as prefix;') to the set of
296296
/// libraries which are imported via that prefix.
297-
late final Map<String, Set<Library>> prefixToLibrary = () {
297+
Map<String, Set<Library>> get prefixToLibrary {
298298
var prefixToLibrary = <String, Set<Library>>{};
299299
// It is possible to have overlapping prefixes.
300300
for (var i in element.libraryImports) {
@@ -307,7 +307,7 @@ class Library extends ModelElement
307307
}
308308
}
309309
return prefixToLibrary;
310-
}();
310+
}
311311

312312
late final String dirName = (isAnonymous ? nameFromPath : name)
313313
.replaceAll(':', '-')
@@ -348,10 +348,10 @@ class Library extends ModelElement
348348
String get belowSidebarPath => sidebarPath;
349349

350350
@override
351-
late final List<ModelFunction> functions =
352-
_exportedAndLocalElements.whereType<FunctionElement>().map((e) {
353-
return modelBuilder.from(e, this) as ModelFunction;
354-
}).toList(growable: false);
351+
late final List<ModelFunction> functions = _exportedAndLocalElements
352+
.whereType<FunctionElement>()
353+
.map((e) => modelBuilder.from(e, this) as ModelFunction)
354+
.toList(growable: false);
355355

356356
@override
357357
String? get href {
@@ -370,7 +370,7 @@ class Library extends ModelElement
370370
Library get library => this;
371371

372372
@override
373-
late final String name = () {
373+
String get name {
374374
var source = element.source;
375375
if (source.uri.isScheme('dart')) {
376376
// There are inconsistencies in library naming + URIs for the Dart
@@ -391,7 +391,7 @@ class Library extends ModelElement
391391
return baseName.substring(0, baseName.length - dartExtensionLength);
392392
}
393393
return baseName;
394-
}();
394+
}
395395

396396
/// Generate a name for this library based on its location.
397397
///
@@ -429,7 +429,7 @@ class Library extends ModelElement
429429
.map((e) => modelBuilder.from(e, this) as Class)
430430
.toList(growable: false);
431431

432-
late final List<TopLevelVariable> _variables = () {
432+
List<TopLevelVariable> get _variables {
433433
var elements =
434434
_exportedAndLocalElements.whereType<TopLevelVariableElement>().toSet();
435435
elements.addAll(_exportedAndLocalElements
@@ -452,7 +452,7 @@ class Library extends ModelElement
452452
variables.add(me as TopLevelVariable);
453453
}
454454
return variables;
455-
}();
455+
}
456456

457457
/// Reverses URIs if needed to get a package URI.
458458
///
@@ -482,6 +482,8 @@ class Library extends ModelElement
482482

483483
/// A mapping of all [Element]s in this library to the [ModelElement]s which
484484
/// represent them in dartdoc.
485+
// Note: Keep this a late final field; converting to a getter (without further
486+
// investigation) causes dartdoc to hang.
485487
late final HashMap<Element, Set<ModelElement>> modelElementsMap = () {
486488
var modelElements = HashMap<Element, Set<ModelElement>>();
487489
for (var modelElement in <ModelElement>[
@@ -508,7 +510,7 @@ class Library extends ModelElement
508510
];
509511

510512
@override
511-
late final Map<String, CommentReferable> referenceChildren = () {
513+
Map<String, CommentReferable> get referenceChildren {
512514
var referenceChildrenBuilder = <String, CommentReferable>{};
513515
var definedNamesModelElements = element.exportNamespace.definedNames.values
514516
.map(modelBuilder.fromElement);
@@ -523,7 +525,7 @@ class Library extends ModelElement
523525
referenceChildrenBuilder.putIfAbsent(prefix, () => libraries.first);
524526
}
525527
return referenceChildrenBuilder;
526-
}();
528+
}
527529

528530
@override
529531
Iterable<CommentReferable> get referenceParents => [package];

lib/src/model/package.dart

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ class Package extends LibraryContainer
116116

117117
/// The documentation from the README contents.
118118
@override
119-
late final String? documentation = () {
119+
String? get documentation {
120120
final docFile = packageMeta.getReadmeContents();
121121
return docFile != null
122122
? packageGraph.resourceProvider
123123
.readAsMalformedAllowedStringSync(docFile)
124124
: null;
125-
}();
125+
}
126126

127127
@override
128128
bool get hasDocumentation => documentation?.isNotEmpty == true;
@@ -143,7 +143,7 @@ class Package extends LibraryContainer
143143
/// Return true if this is the default package, this is part of an embedder
144144
/// SDK, or if [DartdocOptionContext.autoIncludeDependencies] is true -- but
145145
/// only if the package was not excluded on the command line.
146-
late final bool isLocal = () {
146+
bool get isLocal {
147147
// Do not document as local if we excluded this package by name.
148148
if (_isExcluded) return false;
149149
// Document as local if this is the default package.
@@ -157,7 +157,7 @@ class Package extends LibraryContainer
157157
final packagePath = packageGraph.packageMeta.dir.path;
158158
return libraries.any(
159159
(l) => _pathContext.isWithin(packagePath, l.element.source.fullName));
160-
}();
160+
}
161161

162162
/// True if the global config excludes this package by name.
163163
bool get _isExcluded => packageGraph.config.isPackageExcluded(name);
@@ -170,7 +170,7 @@ class Package extends LibraryContainer
170170

171171
/// Returns the location of documentation for this package, for linkToRemote
172172
/// and canonicalization decision making.
173-
late final DocumentLocation documentedWhere = () {
173+
DocumentLocation get documentedWhere {
174174
if (isLocal && isPublic) {
175175
return DocumentLocation.local;
176176
}
@@ -181,7 +181,7 @@ class Package extends LibraryContainer
181181
return DocumentLocation.remote;
182182
}
183183
return DocumentLocation.missing;
184-
}();
184+
}
185185

186186
@override
187187
String get enclosingName => packageGraph.defaultPackageName;
@@ -400,16 +400,15 @@ class Package extends LibraryContainer
400400
List<String> get containerOrder => config.packageOrder;
401401

402402
@override
403-
late final Map<String, CommentReferable> referenceChildren =
404-
<String, CommentReferable>{
403+
late final Map<String, CommentReferable> referenceChildren = {
405404
for (var library in publicLibrariesSorted) library.referenceName: library,
406405
}
407-
// Do not override any preexisting data, and insert based on the
408-
// public library sort order.
409-
// TODO(jcollins-g): warn when results require package-global
410-
// lookups like this.
411-
..addEntriesIfAbsent(
412-
publicLibrariesSorted.expand((l) => l.referenceChildren.entries));
406+
// Do not override any preexisting data, and insert based on the
407+
// public library sort order.
408+
// TODO(jcollins-g): warn when results require package-global
409+
// lookups like this.
410+
..addEntriesIfAbsent(
411+
publicLibrariesSorted.expand((l) => l.referenceChildren.entries));
413412

414413
@override
415414
Iterable<CommentReferable> get referenceParents => [packageGraph];

0 commit comments

Comments
 (0)