forked from dart-lang/dartdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage_graph.dart
949 lines (848 loc) · 34.9 KB
/
package_graph.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/file_system/file_system.dart';
import 'package:analyzer/src/generated/sdk.dart';
import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer/src/generated/source_io.dart';
import 'package:collection/collection.dart';
import 'package:dartdoc/src/dartdoc_options.dart';
import 'package:dartdoc/src/logging.dart';
import 'package:dartdoc/src/model/model.dart';
import 'package:dartdoc/src/model_utils.dart' as utils;
import 'package:dartdoc/src/package_meta.dart'
show PackageMeta, PackageMetaProvider;
import 'package:dartdoc/src/render/renderer_factory.dart';
import 'package:dartdoc/src/special_elements.dart';
import 'package:dartdoc/src/tuple.dart';
import 'package:dartdoc/src/warnings.dart';
class PackageGraph {
PackageGraph.UninitializedPackageGraph(
this.config,
this.sdk,
this.hasEmbedderSdk,
this.rendererFactory,
this.packageMetaProvider,
) : packageMeta = config.topLevelPackageMeta {
_packageWarningCounter = PackageWarningCounter(this);
// Make sure the default package exists, even if it has no libraries.
// This can happen for packages that only contain embedder SDKs.
Package.fromPackageMeta(packageMeta, this);
}
/// Call during initialization to add a library to this [PackageGraph].
///
/// Libraries added in this manner are assumed to be part of documented
/// packages, even if includes or embedder.yaml files cause these to
/// span packages.
void addLibraryToGraph(DartDocResolvedLibrary resolvedLibrary) {
assert(!allLibrariesAdded);
var element = resolvedLibrary.element;
var packageMeta = packageMetaProvider.fromElement(element, config.sdkDir);
var lib = Library.fromLibraryResult(
resolvedLibrary, this, Package.fromPackageMeta(packageMeta, this));
packageMap[packageMeta.name].libraries.add(lib);
allLibraries[element] = lib;
}
/// Call during initialization to add a library possibly containing
/// special/non-documented elements to this [PackageGraph]. Must be called
/// after any normal libraries.
void addSpecialLibraryToGraph(DartDocResolvedLibrary resolvedLibrary) {
allLibrariesAdded = true;
assert(!_localDocumentationBuilt);
findOrCreateLibraryFor(resolvedLibrary);
}
/// Call after all libraries are added.
Future<void> initializePackageGraph() async {
allLibrariesAdded = true;
assert(!_localDocumentationBuilt);
// From here on in, we might find special objects. Initialize the
// specialClasses handler so when we find them, they get added.
specialClasses = SpecialClasses();
// Go through docs of every ModelElement in package to pre-build the macros
// index. Uses toList() in order to get all the precaching on the stack.
await Future.wait(precacheLocalDocs());
_localDocumentationBuilt = true;
// Scan all model elements to insure that interceptor and other special
// objects are found.
// After the allModelElements traversal to be sure that all packages
// are picked up.
for (var package in documentedPackages) {
package.libraries.sort((a, b) => compareNatural(a.name, b.name));
for (var library in package.libraries) {
library.allClasses.forEach(_addToImplementors);
_extensions.addAll(library.extensions);
}
}
for (var l in _implementors.values) {
l.sort();
}
allImplementorsAdded = true;
allExtensionsAdded = true;
// We should have found all special classes by now.
specialClasses.assertSpecials();
}
/// Generate a list of futures for any docs that actually require precaching.
Iterable<Future<void>> precacheLocalDocs() sync* {
// Prevent reentrancy.
var precachedElements = <ModelElement>{};
Iterable<Future<void>> precacheOneElement(ModelElement m) sync* {
for (var d
in m.documentationFrom.where((d) => d.documentationComment != null)) {
if (needsPrecacheRegExp.hasMatch(d.documentationComment) &&
!precachedElements.contains(d)) {
precachedElements.add(d);
yield d.precacheLocalDocs();
logProgress(d.name);
// TopLevelVariables get their documentation from getters and setters,
// so should be precached if either has a template.
if (m is TopLevelVariable && !precachedElements.contains(m)) {
precachedElements.add(m);
yield m.precacheLocalDocs();
logProgress(d.name);
}
}
}
}
for (var m in allModelElements) {
// Skip if there is a canonicalModelElement somewhere else we can run this
// for. Not the same as allCanonicalModelElements since we need to run
// for any ModelElement that might not have a canonical ModelElement,
// too.
if (m.canonicalModelElement != null && !m.isCanonical) continue;
yield* precacheOneElement(m);
}
}
// Many ModelElements have the same ModelNode; don't build/cache this data more
// than once for them.
final Map<Element, ModelNode> _modelNodes = {};
void populateModelNodeFor(
Element element, Map<String, CompilationUnit> compilationUnitMap) {
_modelNodes.putIfAbsent(
element,
() =>
ModelNode(utils.getAstNode(element, compilationUnitMap), element));
}
ModelNode getModelNodeFor(Element element) => _modelNodes[element];
SpecialClasses specialClasses;
/// It is safe to cache values derived from the [_implementors] table if this
/// is true.
bool allImplementorsAdded = false;
/// It is safe to cache values derived from the [_extensions] table if this
/// is true.
bool allExtensionsAdded = false;
Map<String, List<Class>> get implementors {
assert(allImplementorsAdded);
return _implementors;
}
List<Extension> _documentedExtensions;
Iterable<Extension> get documentedExtensions {
_documentedExtensions ??=
utils.filterNonDocumented(extensions).toList(growable: false);
return _documentedExtensions;
}
Iterable<Extension> get extensions {
assert(allExtensionsAdded);
return _extensions;
}
Map<String, Set<ModelElement>> _findRefElementCache;
Map<String, Set<ModelElement>> get findRefElementCache {
if (_findRefElementCache == null) {
assert(packageGraph.allLibrariesAdded);
_findRefElementCache = {};
for (final modelElement
in utils.filterNonDocumented(packageGraph.allLocalModelElements)) {
_findRefElementCache.putIfAbsent(
modelElement.fullyQualifiedNameWithoutLibrary, () => {});
_findRefElementCache.putIfAbsent(
modelElement.fullyQualifiedName, () => {});
_findRefElementCache[modelElement.fullyQualifiedName].add(modelElement);
_findRefElementCache[modelElement.fullyQualifiedNameWithoutLibrary]
.add(modelElement);
}
}
return _findRefElementCache;
}
// All library objects related to this package; a superset of _libraries.
final Map<LibraryElement, Library> allLibraries = {};
/// Keep track of warnings
PackageWarningCounter _packageWarningCounter;
/// All ModelElements constructed for this package; a superset of [allModelElements].
final Map<Tuple3<Element, Library, Container>, ModelElement>
allConstructedModelElements = {};
/// Anything that might be inheritable, place here for later lookup.
final Map<Tuple2<Element, Library>, Set<ModelElement>>
allInheritableElements = {};
/// Map of Class.href to a list of classes implementing that class
final Map<String, List<Class>> _implementors = {};
/// A list of extensions that exist in the package graph.
final List<Extension> _extensions = [];
/// PackageMeta for the default package.
final PackageMeta packageMeta;
/// Name of the default package.
String get defaultPackageName => packageMeta.name;
/// Dartdoc's configuration flags.
final DartdocOptionContext config;
/// Factory for renderers
final RendererFactory rendererFactory;
/// PackageMeta Provider for building [PackageMeta]s.
final PackageMetaProvider packageMetaProvider;
Package _defaultPackage;
Package get defaultPackage {
_defaultPackage ??= Package.fromPackageMeta(packageMeta, this);
return _defaultPackage;
}
final bool hasEmbedderSdk;
bool get hasFooterVersion => !config.excludeFooterVersion;
PackageGraph get packageGraph => this;
/// Map of package name to Package.
final Map<String, Package> packageMap = {};
ResourceProvider get resourceProvider => config.resourceProvider;
final DartSdk sdk;
Map<Source, SdkLibrary> _sdkLibrarySources;
Map<Source, SdkLibrary> get sdkLibrarySources {
if (_sdkLibrarySources == null) {
_sdkLibrarySources = {};
for (var lib in sdk?.sdkLibraries) {
_sdkLibrarySources[sdk.mapDartUri(lib.shortName)] = lib;
}
}
return _sdkLibrarySources;
}
final Map<String, String> _macros = {};
final Map<String, String> _htmlFragments = {};
bool allLibrariesAdded = false;
bool _localDocumentationBuilt = false;
Set<String> _allRootDirs;
/// Returns true if there's at least one library documented in the package
/// that has the same package path as the library for the given element.
///
/// Usable as a cross-check for dartdoc's canonicalization to generate
/// warnings for ModelElement.isPublicAndPackageDocumented.
bool packageDocumentedFor(ModelElement element) {
_allRootDirs ??= {
...(publicLibraries.map((l) => l.packageMeta?.resolvedDir))
};
return _allRootDirs.contains(element.library.packageMeta?.resolvedDir);
}
PackageWarningCounter get packageWarningCounter => _packageWarningCounter;
final Set<Tuple3<Element, PackageWarning, String>> _warnAlreadySeen = {};
void warnOnElement(Warnable warnable, PackageWarning kind,
{String message,
Iterable<Locatable> referredFrom,
Iterable<String> extendedDebug}) {
var newEntry = Tuple3(warnable?.element, kind, message);
if (_warnAlreadySeen.contains(newEntry)) {
return;
}
// Warnings can cause other warnings. Queue them up via the stack but
// don't allow warnings we're already working on to get in there.
_warnAlreadySeen.add(newEntry);
_warnOnElement(warnable, kind,
message: message,
referredFrom: referredFrom,
extendedDebug: extendedDebug);
_warnAlreadySeen.remove(newEntry);
}
void _warnOnElement(Warnable warnable, PackageWarning kind,
{String message,
Iterable<Locatable> referredFrom,
Iterable<String> extendedDebug}) {
if (warnable != null) {
// This sort of warning is only applicable to top level elements.
if (kind == PackageWarning.ambiguousReexport) {
while (warnable.enclosingElement is! Library &&
warnable.enclosingElement != null) {
warnable = warnable.enclosingElement;
}
}
} else {
// If we don't have an element, we need a message to disambiguate.
assert(message != null);
}
if (_packageWarningCounter.hasWarning(warnable, kind, message)) {
return;
}
// Some kinds of warnings it is OK to drop if we're not documenting them.
// TODO(jcollins-g): drop this and use new flag system instead.
if (warnable != null &&
skipWarningIfNotDocumentedFor.contains(kind) &&
!warnable.isDocumented) {
return;
}
// Elements that are part of the Dart SDK can have colons in their FQNs.
// This confuses IntelliJ and makes it so it can't link to the location
// of the error in the console window, so separate out the library from
// the path.
// TODO(jcollins-g): What about messages that may include colons? Substituting
// them out doesn't work as well there since it might confuse
// the user, yet we still want IntelliJ to link properly.
final warnableName = _safeWarnableName(warnable);
var warnablePrefix = 'from';
var referredFromPrefix = 'referred to by';
String warningMessage;
switch (kind) {
case PackageWarning.noCanonicalFound:
// Fix these warnings by adding libraries with --include, or by using
// --auto-include-dependencies.
// TODO(jcollins-g): pipeline references through linkedName for error
// messages and warn for non-public canonicalization
// errors.
warningMessage =
'no canonical library found for $warnableName, not linking';
break;
case PackageWarning.ambiguousReexport:
// Fix these warnings by adding the original library exporting the
// symbol with --include, by using --auto-include-dependencies,
// or by using --exclude to hide one of the libraries involved
warningMessage =
'ambiguous reexport of $warnableName, canonicalization candidates: $message';
break;
case PackageWarning.noDefiningLibraryFound:
warningMessage =
'could not find the defining library for $warnableName; the '
'library may be imported or exported with a non-standard URI';
break;
case PackageWarning.noLibraryLevelDocs:
warningMessage =
'${warnable.fullyQualifiedName} has no library level documentation comments';
break;
case PackageWarning.ambiguousDocReference:
warningMessage = 'ambiguous doc reference $message';
break;
case PackageWarning.ignoredCanonicalFor:
warningMessage =
"library says it is {@canonicalFor $message} but $message can't be canonical there";
break;
case PackageWarning.packageOrderGivesMissingPackageName:
warningMessage =
"--package-order gives invalid package name: '$message'";
break;
case PackageWarning.reexportedPrivateApiAcrossPackages:
warningMessage =
'private API of $message is reexported by libraries in other packages: ';
break;
case PackageWarning.notImplemented:
warningMessage = message;
break;
case PackageWarning.unresolvedDocReference:
warningMessage = 'unresolved doc reference [$message]';
referredFromPrefix = 'in documentation inherited from';
break;
case PackageWarning.unknownDirective:
warningMessage = 'undefined directive: $message';
break;
case PackageWarning.unknownMacro:
warningMessage = 'undefined macro [$message]';
break;
case PackageWarning.unknownHtmlFragment:
warningMessage = 'undefined HTML fragment identifier [$message]';
break;
case PackageWarning.brokenLink:
warningMessage = 'dartdoc generated a broken link to: $message';
warnablePrefix = 'to element';
referredFromPrefix = 'linked to from';
break;
case PackageWarning.orphanedFile:
warningMessage = 'dartdoc generated a file orphan: $message';
break;
case PackageWarning.unknownFile:
warningMessage =
'dartdoc detected an unknown file in the doc tree: $message';
break;
case PackageWarning.missingFromSearchIndex:
warningMessage =
'dartdoc generated a file not in the search index: $message';
break;
case PackageWarning.typeAsHtml:
// The message for this warning can contain many punctuation and other symbols,
// so bracket with a triple quote for defense.
warningMessage = 'generic type handled as HTML: """$message"""';
break;
case PackageWarning.invalidParameter:
warningMessage = 'invalid parameter to dartdoc directive: $message';
break;
case PackageWarning.toolError:
warningMessage = 'tool execution failed: $message';
break;
case PackageWarning.deprecated:
warningMessage = 'deprecated dartdoc usage: $message';
break;
case PackageWarning.unresolvedExport:
warningMessage = 'unresolved export uri: $message';
break;
case PackageWarning.duplicateFile:
warningMessage = 'failed to write file at: $message';
warnablePrefix = 'for symbol';
referredFromPrefix = 'conflicting with file already generated by';
break;
case PackageWarning.missingConstantConstructor:
warningMessage = 'constant constructor missing: $message';
break;
case PackageWarning.missingExampleFile:
warningMessage = 'example file not found: $message';
break;
}
var messageParts = <String>[warningMessage];
if (warnable != null) {
messageParts.add('$warnablePrefix $warnableName: ${warnable.location}');
}
if (referredFrom != null) {
for (var referral in referredFrom) {
if (referral != warnable) {
var referredFromStrings = _safeWarnableName(referral);
messageParts.add(
'$referredFromPrefix $referredFromStrings: ${referral.location}');
}
}
}
if (config.verboseWarnings && extendedDebug != null) {
messageParts.addAll(extendedDebug.map((s) => ' $s'));
}
String fullMessage;
if (messageParts.length <= 2) {
fullMessage = messageParts.join(', ');
} else {
fullMessage = messageParts.join('\n ');
}
packageWarningCounter.addWarning(warnable, kind, message, fullMessage);
}
String _safeWarnableName(Locatable locatable) {
if (locatable == null) {
return '<unknown>';
}
return locatable.fullyQualifiedName.replaceFirst(':', '-');
}
List<Package> get packages => packageMap.values.toList();
List<Package> _publicPackages;
List<Package> get publicPackages {
if (_publicPackages == null) {
assert(allLibrariesAdded);
// Help the user if they pass us a package that doesn't exist.
for (var packageName in config.packageOrder) {
if (!packages.map((p) => p.name).contains(packageName)) {
warnOnElement(
null, PackageWarning.packageOrderGivesMissingPackageName,
message:
"${packageName}, packages: ${packages.map((p) => p.name).join(',')}");
}
}
_publicPackages = packages.where((p) => p.isPublic).toList()..sort();
}
return _publicPackages;
}
/// Local packages are to be documented locally vs. remote or not at all.
List<Package> get localPackages =>
publicPackages.where((p) => p.isLocal).toList();
/// Documented packages are documented somewhere (local or remote).
Iterable<Package> get documentedPackages =>
packages.where((p) => p.documentedWhere != DocumentLocation.missing);
Map<LibraryElement, Set<Library>> _libraryElementReexportedBy = {};
/// Prevent cycles from breaking our stack.
Set<Tuple2<Library, LibraryElement>> _reexportsTagged = {};
void _tagReexportsFor(
final Library topLevelLibrary, final LibraryElement libraryElement,
[ExportElement lastExportedElement]) {
var key = Tuple2<Library, LibraryElement>(topLevelLibrary, libraryElement);
if (_reexportsTagged.contains(key)) {
return;
}
_reexportsTagged.add(key);
if (libraryElement == null) {
// The first call to _tagReexportFor should not have a null libraryElement.
assert(lastExportedElement != null);
warnOnElement(
findButDoNotCreateLibraryFor(lastExportedElement.enclosingElement),
PackageWarning.unresolvedExport,
message: '"${lastExportedElement.uri}"',
referredFrom: <Locatable>[topLevelLibrary]);
return;
}
_libraryElementReexportedBy.putIfAbsent(libraryElement, () => {});
_libraryElementReexportedBy[libraryElement].add(topLevelLibrary);
for (var exportedElement in libraryElement.exports) {
_tagReexportsFor(
topLevelLibrary, exportedElement.exportedLibrary, exportedElement);
}
}
int _lastSizeOfAllLibraries = 0;
Map<LibraryElement, Set<Library>> get libraryElementReexportedBy {
// Table must be reset if we're still in the middle of adding libraries.
if (allLibraries.keys.length != _lastSizeOfAllLibraries) {
_lastSizeOfAllLibraries = allLibraries.keys.length;
_libraryElementReexportedBy = <LibraryElement, Set<Library>>{};
_reexportsTagged = {};
for (var library in publicLibraries) {
_tagReexportsFor(library, library.element);
}
}
return _libraryElementReexportedBy;
}
/// A lookup index for hrefs to allow warnings to indicate where a broken
/// link or orphaned file may have come from. Not cached because
/// [ModelElement]s can be created at any time and we're basing this
/// on more than just [allLocalModelElements] to make the error messages
/// comprehensive.
Map<String, Set<ModelElement>> get allHrefs {
var hrefMap = <String, Set<ModelElement>>{};
// TODO(jcollins-g ): handle calculating hrefs causing new elements better
// than toList().
for (var modelElement in allConstructedModelElements.values.toList()) {
// Technically speaking we should be able to use canonical model elements
// only here, but since the warnings that depend on this debug
// canonicalization problems, don't limit ourselves in case an href is
// generated for something non-canonical.
if (modelElement is Dynamic) continue;
// TODO: see [Accessor.enclosingCombo]
if (modelElement is Accessor) continue;
if (modelElement.href == null) continue;
hrefMap.putIfAbsent(modelElement.href, () => {});
hrefMap[modelElement.href].add(modelElement);
}
for (var package in packageMap.values) {
for (var library in package.libraries) {
if (library.href == null) continue;
hrefMap.putIfAbsent(library.href, () => {});
hrefMap[library.href].add(library);
}
}
return hrefMap;
}
void _addToImplementors(Class c) {
assert(!allImplementorsAdded);
_implementors.putIfAbsent(c.href, () => []);
void _checkAndAddClass(Class key, Class implClass) {
_implementors.putIfAbsent(key.href, () => []);
var list = _implementors[key.href];
if (!list.any((l) => l.element == c.element)) {
list.add(implClass);
}
}
for (var type in c.mixins) {
_checkAndAddClass(type.element, c);
}
if (c.supertype != null) {
_checkAndAddClass(c.supertype.element, c);
}
for (var type in c.interfaces) {
_checkAndAddClass(type.element, c);
}
}
Iterable<Library> get libraries =>
packages.expand((p) => p.libraries).toList()..sort();
List<Library> _publicLibraries;
Iterable<Library> get publicLibraries {
if (_publicLibraries == null) {
assert(allLibrariesAdded);
_publicLibraries = utils.filterNonPublic(libraries).toList();
}
return _publicLibraries;
}
List<Library> _localLibraries;
Iterable<Library> get localLibraries {
if (_localLibraries == null) {
assert(allLibrariesAdded);
_localLibraries = localPackages.expand((p) => p.libraries).toList()
..sort();
}
return _localLibraries;
}
List<Library> _localPublicLibraries;
Iterable<Library> get localPublicLibraries {
if (_localPublicLibraries == null) {
assert(allLibrariesAdded);
_localPublicLibraries = utils.filterNonPublic(localLibraries).toList();
}
return _localPublicLibraries;
}
Set<Class> _inheritThrough;
/// Return the set of [Class]es objects should inherit through if they
/// show up in the inheritance chain. Do not call before interceptorElement is
/// found. Add classes here if they are similar to Interceptor in that they
/// are to be ignored even when they are the implementors of [Inheritable]s,
/// and the class these inherit from should instead claim implementation.
Set<Class> get inheritThrough {
if (_inheritThrough == null) {
_inheritThrough = {};
_inheritThrough.add(specialClasses[SpecialClass.interceptor]);
}
return _inheritThrough;
}
Set<Class> _invisibleAnnotations;
/// Returns the set of [Class] objects that are similar to pragma
/// in that we should never count them as documentable annotations.
Set<Class> get invisibleAnnotations =>
_invisibleAnnotations ??= {specialClasses[SpecialClass.pragma]};
bool isAnnotationVisible(Class class_) =>
!invisibleAnnotations.contains(class_);
@override
String toString() {
final divider = '=========================================================';
final buffer =
StringBuffer('PackageGraph built from ${defaultPackage.name}');
buffer.writeln(divider);
buffer.writeln();
for (final name in packageMap.keys) {
final package = packageMap[name];
buffer.writeln('Package $name documented at ${package.documentedWhere} '
'with libraries: ${package.allLibraries}');
}
buffer.writeln(divider);
return buffer.toString();
}
final Map<Element, Library> _canonicalLibraryFor = {};
/// Tries to find a top level library that references this element.
Library findCanonicalLibraryFor(Element e) {
assert(allLibrariesAdded);
var searchElement = e;
if (e is PropertyAccessorElement) {
searchElement = e.variable;
}
if (e is GenericFunctionTypeElement) {
searchElement = e.enclosingElement;
}
if (_canonicalLibraryFor.containsKey(e)) {
return _canonicalLibraryFor[e];
}
_canonicalLibraryFor[e] = null;
for (var library in publicLibraries) {
if (library.modelElementsMap.containsKey(searchElement)) {
for (var modelElement in library.modelElementsMap[searchElement]) {
if (modelElement.isCanonical) {
return _canonicalLibraryFor[e] = library;
}
}
}
}
return _canonicalLibraryFor[e];
}
/// Tries to find a canonical ModelElement for this element. If we know
/// this element is related to a particular class, pass preferredClass to
/// disambiguate.
///
/// This doesn't know anything about [PackageGraph.inheritThrough] and probably
/// shouldn't, so using it with [Inheritable]s without special casing is
/// not advised.
ModelElement findCanonicalModelElementFor(Element e,
{Container preferredClass}) {
assert(allLibrariesAdded);
var lib = findCanonicalLibraryFor(e);
if (preferredClass != null && preferredClass is Container) {
Container canonicalClass =
findCanonicalModelElementFor(preferredClass.element);
if (canonicalClass != null) preferredClass = canonicalClass;
}
if (lib == null && preferredClass != null) {
lib = findCanonicalLibraryFor(preferredClass.element);
}
ModelElement modelElement;
// For elements defined in extensions, they are canonical.
if (e?.enclosingElement is ExtensionElement) {
lib ??= Library(e.enclosingElement.library, packageGraph);
// (TODO:keertip) Find a better way to exclude members of extensions
// when libraries are specified using the "--include" flag
if (lib?.isDocumented == true) {
return ModelElement.from(e, lib, packageGraph);
}
}
// TODO(jcollins-g): Special cases are pretty large here. Refactor to split
// out into helpers.
// TODO(jcollins-g): The data structures should be changed to eliminate guesswork
// with member elements.
if (e is ClassMemberElement || e is PropertyAccessorElement) {
e = e.declaration;
var candidates = <ModelElement>{};
var iKey = Tuple2<Element, Library>(e, lib);
var key =
Tuple4<Element, Library, Class, ModelElement>(e, lib, null, null);
var keyWithClass = Tuple4<Element, Library, Class, ModelElement>(
e, lib, preferredClass, null);
if (allConstructedModelElements.containsKey(key)) {
candidates.add(allConstructedModelElements[key]);
}
if (allConstructedModelElements.containsKey(keyWithClass)) {
candidates.add(allConstructedModelElements[keyWithClass]);
}
if (candidates.isEmpty && allInheritableElements.containsKey(iKey)) {
candidates
.addAll(allInheritableElements[iKey].where((me) => me.isCanonical));
}
Class canonicalClass = findCanonicalModelElementFor(e.enclosingElement);
if (canonicalClass != null) {
candidates.addAll(canonicalClass.allCanonicalModelElements.where((m) {
return m.element == e;
}));
}
var matches = <ModelElement>{...candidates.where((me) => me.isCanonical)};
// It's possible to find accessors but no combos. Be sure that if we
// have Accessors, we find their combos too.
if (matches.any((me) => me is Accessor)) {
var combos =
matches.whereType<Accessor>().map((a) => a.enclosingCombo).toList();
matches.addAll(combos);
assert(combos.every((c) => c.isCanonical));
}
// This is for situations where multiple classes may actually be canonical
// for an inherited element whose defining Class is not canonical.
if (matches.length > 1 &&
preferredClass != null &&
preferredClass is Class) {
// Search for matches inside our superchain.
var superChain = preferredClass.superChain
.map((et) => et.element)
.cast<Class>()
.toList();
superChain.add(preferredClass);
matches.removeWhere((me) =>
!superChain.contains((me as EnclosedElement).enclosingElement));
// Assumed all matches are EnclosedElement because we've been told about a
// preferredClass.
var enclosingElements = {
...matches
.map((me) => (me as EnclosedElement).enclosingElement as Class)
};
for (var c in superChain.reversed) {
if (enclosingElements.contains(c)) {
matches.removeWhere(
(me) => (me as EnclosedElement).enclosingElement != c);
}
if (matches.length <= 1) break;
}
}
// Prefer a GetterSetterCombo to Accessors.
if (matches.any((me) => me is GetterSetterCombo)) {
matches.removeWhere((me) => me is Accessor);
}
assert(matches.length <= 1);
if (matches.isNotEmpty) {
modelElement = matches.first;
}
} else {
if (lib != null) {
if (e is PropertyInducingElement) {
var getter = e.getter != null
? ModelElement.from(e.getter, lib, packageGraph)
: null;
var setter = e.setter != null
? ModelElement.from(e.setter, lib, packageGraph)
: null;
modelElement = ModelElement.fromPropertyInducingElement(
e, lib, packageGraph,
getter: getter, setter: setter);
} else {
modelElement = ModelElement.from(e, lib, packageGraph);
}
}
assert(modelElement is! Inheritable);
if (modelElement != null && !modelElement.isCanonical) {
modelElement = null;
}
}
// Prefer Fields.
if (e is PropertyAccessorElement && modelElement is Accessor) {
modelElement = (modelElement as Accessor).enclosingCombo;
}
return modelElement;
}
/// This is used when we might need a Library object that isn't actually
/// a documentation entry point (for elements that have no Library within the
/// set of canonical Libraries).
Library findButDoNotCreateLibraryFor(Element e) {
// This is just a cache to avoid creating lots of libraries over and over.
if (allLibraries.containsKey(e.library)) {
return allLibraries[e.library];
}
return null;
}
/// This is used when we might need a Library object that isn't actually
/// a documentation entry point (for elements that have no Library within the
/// set of canonical Libraries).
Library findOrCreateLibraryFor(DartDocResolvedLibrary resolvedLibrary) {
final elementLibrary = resolvedLibrary.library;
// This is just a cache to avoid creating lots of libraries over and over.
if (allLibraries.containsKey(elementLibrary)) {
return allLibraries[elementLibrary];
}
// can be null if e is for dynamic
if (elementLibrary == null) {
return null;
}
var foundLibrary = Library.fromLibraryResult(
resolvedLibrary,
this,
Package.fromPackageMeta(
packageMetaProvider.fromElement(elementLibrary, config.sdkDir),
packageGraph));
allLibraries[elementLibrary] = foundLibrary;
return foundLibrary;
}
List<ModelElement> _allModelElements;
Iterable<ModelElement> get allModelElements {
assert(allLibrariesAdded);
if (_allModelElements == null) {
_allModelElements = [];
var packagesToDo = packages.toSet();
var completedPackages = <Package>{};
while (packagesToDo.length > completedPackages.length) {
packagesToDo.difference(completedPackages).forEach((Package p) {
var librariesToDo = p.allLibraries.toSet();
var completedLibraries = <Library>{};
while (librariesToDo.length > completedLibraries.length) {
librariesToDo
.difference(completedLibraries)
.forEach((Library library) {
_allModelElements.addAll(library.allModelElements);
completedLibraries.add(library);
});
librariesToDo.addAll(p.allLibraries);
}
completedPackages.add(p);
});
packagesToDo.addAll(packages);
}
}
return _allModelElements;
}
List<ModelElement> _allLocalModelElements;
Iterable<ModelElement> get allLocalModelElements {
assert(allLibrariesAdded);
if (_allLocalModelElements == null) {
_allLocalModelElements = [];
localLibraries.forEach((library) {
_allLocalModelElements.addAll(library.allModelElements);
});
}
return _allLocalModelElements;
}
List<ModelElement> _allCanonicalModelElements;
Iterable<ModelElement> get allCanonicalModelElements {
return _allCanonicalModelElements ??=
allLocalModelElements.where((e) => e.isCanonical).toList();
}
String getMacro(String name) {
assert(_localDocumentationBuilt);
return _macros[name];
}
void addMacro(String name, String content) {
// TODO(srawlins): We have to add HTML fragments after documentation is
// built, in order to include fragments which come from macros which
// were generated by a tool. I think the macro/HTML-injection system needs
// to be overhauled to allow for this type of looping.
//assert(!_localDocumentationBuilt);
_macros[name] = content;
}
String getHtmlFragment(String name) {
assert(_localDocumentationBuilt);
return _htmlFragments[name];
}
void addHtmlFragment(String name, String content) {
// TODO(srawlins): We have to add HTML fragments after documentation is
// built, in order to include fragments which come from macros which
// were generated by a tool. I think the macro/HTML-injection system needs
// to be overhauled to allow for this type of looping.
//assert(!_localDocumentationBuilt);
_htmlFragments[name] = content;
}
}