|
2 | 2 | // for details. All rights reserved. Use of this source code is governed by a
|
3 | 3 | // BSD-style license that can be found in the LICENSE file.
|
4 | 4 |
|
| 5 | +import 'package:analyzer/dart/element/element.dart'; |
5 | 6 | import 'package:dartdoc/src/model/model.dart';
|
6 | 7 | import 'package:dartdoc/src/warnings.dart';
|
7 | 8 |
|
| 9 | +/// Searches [PackageGraph.libraryExports] for a public, documented library |
| 10 | +/// which exports this [ModelElement], ideally in its library's package. |
| 11 | +Library? canonicalLibraryCandidate(ModelElement modelElement) { |
| 12 | + var thisAndExported = |
| 13 | + modelElement.packageGraph.libraryExports[modelElement.library.element]; |
| 14 | + if (thisAndExported == null) { |
| 15 | + return null; |
| 16 | + } |
| 17 | + |
| 18 | + // Since we're looking for a library, find the [Element] immediately |
| 19 | + // contained by a [CompilationUnitElement] in the tree. |
| 20 | + var topLevelElement = modelElement.element; |
| 21 | + while (topLevelElement.enclosingElement3 is! LibraryElement && |
| 22 | + topLevelElement.enclosingElement3 is! CompilationUnitElement && |
| 23 | + topLevelElement.enclosingElement3 != null) { |
| 24 | + topLevelElement = topLevelElement.enclosingElement3!; |
| 25 | + } |
| 26 | + var topLevelElementName = topLevelElement.name; |
| 27 | + if (topLevelElementName == null) { |
| 28 | + // Any member of an unnamed extension is not public, and has no |
| 29 | + // canonical library. |
| 30 | + return null; |
| 31 | + } |
| 32 | + |
| 33 | + final candidateLibraries = thisAndExported.where((l) { |
| 34 | + if (!l.isPublic) return false; |
| 35 | + if (l.package.documentedWhere == DocumentLocation.missing) return false; |
| 36 | + if (modelElement is Library) return true; |
| 37 | + var lookup = l.element.exportNamespace.definedNames[topLevelElementName]; |
| 38 | + return topLevelElement == |
| 39 | + (lookup is PropertyAccessorElement ? lookup.variable2 : lookup); |
| 40 | + }).toList(growable: true); |
| 41 | + |
| 42 | + if (candidateLibraries.isEmpty) { |
| 43 | + return null; |
| 44 | + } |
| 45 | + if (candidateLibraries.length == 1) { |
| 46 | + return candidateLibraries.single; |
| 47 | + } |
| 48 | + |
| 49 | + var remoteLibraries = candidateLibraries |
| 50 | + .where((l) => l.package.documentedWhere == DocumentLocation.remote); |
| 51 | + if (remoteLibraries.length == 1) { |
| 52 | + // If one or more local libraries export code from a remotely documented |
| 53 | + // library (and we're linking to remote libraries), then just use the remote |
| 54 | + // library. |
| 55 | + return remoteLibraries.single; |
| 56 | + } |
| 57 | + |
| 58 | + var topLevelModelElement = |
| 59 | + ModelElement.forElement(topLevelElement, modelElement.packageGraph); |
| 60 | + |
| 61 | + return _Canonicalization(topLevelModelElement) |
| 62 | + .canonicalLibraryCandidate(candidateLibraries); |
| 63 | +} |
| 64 | + |
8 | 65 | /// Canonicalization support in Dartdoc.
|
9 | 66 | ///
|
10 | 67 | /// This provides heuristic scoring to determine which library a human likely
|
11 | 68 | /// considers this element to be primarily 'from', and therefore, canonical.
|
12 | 69 | /// Still warn if the heuristic isn't very confident.
|
13 |
| -final class Canonicalization { |
| 70 | +final class _Canonicalization { |
14 | 71 | final ModelElement _element;
|
15 | 72 |
|
16 |
| - Canonicalization(this._element); |
| 73 | + _Canonicalization(this._element); |
17 | 74 |
|
18 | 75 | /// Calculates a candidate for the canonical library of [_element], among [libraries].
|
19 |
| - Library calculateCanonicalCandidate(Iterable<Library> libraries) { |
| 76 | + Library canonicalLibraryCandidate(Iterable<Library> libraries) { |
20 | 77 | var locationPieces = _element.element.location
|
21 | 78 | .toString()
|
22 | 79 | .split(_locationSplitter)
|
|
0 commit comments