-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathextension.dart
125 lines (105 loc) · 3.72 KB
/
extension.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
// 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 'package:analyzer/dart/element/element.dart';
import 'package:dartdoc/src/element_type.dart';
import 'package:dartdoc/src/model/extension_target.dart';
import 'package:dartdoc/src/model/model.dart';
import 'package:dartdoc/src/quiver.dart' as quiver;
/// Extension methods
class Extension extends Container
with TypeParameters, Categorization
implements EnclosedElement {
ElementType extendedType;
Extension(
ExtensionElement element, Library library, PackageGraph packageGraph)
: super(element, library, packageGraph) {
extendedType =
ElementType.from(_extension.extendedType, library, packageGraph);
}
/// Detect if this extension applies to every object.
bool get alwaysApplies =>
extendedType.instantiatedType.isDynamic ||
extendedType.instantiatedType.isVoid ||
extendedType.instantiatedType.isDartCoreObject;
bool couldApplyTo<T extends ExtensionTarget>(T c) =>
_couldApplyTo(c.modelType);
/// Return true if this extension could apply to [t].
bool _couldApplyTo(DefinedElementType t) {
if (extendedType.instantiatedType.isDynamic ||
extendedType.instantiatedType.isVoid) {
return true;
}
return t.instantiatedType == extendedType.instantiatedType ||
(t.instantiatedType.element == extendedType.instantiatedType.element &&
extendedType.isSubtypeOf(t)) ||
extendedType.isBoundSupertypeTo(t);
}
@override
ModelElement get enclosingElement => library;
ExtensionElement get _extension => (element as ExtensionElement);
@override
String get kind => 'extension';
List<Method> _methods;
@override
List<Method> get declaredMethods {
_methods ??= _extension.methods.map((e) {
return ModelElement.from(e, library, packageGraph) as Method;
}).toList(growable: false)
..sort(byName);
return _methods;
}
@override
String get name => super.name ?? '';
List<Field> _declaredFields;
@override
List<Field> get declaredFields {
_declaredFields ??= _extension.fields.map((f) {
Accessor getter, setter;
if (f.getter != null) {
getter = ContainerAccessor(f.getter, library, packageGraph);
}
if (f.setter != null) {
setter = ContainerAccessor(f.setter, library, packageGraph);
}
return ModelElement.fromPropertyInducingElement(f, library, packageGraph,
getter: getter, setter: setter) as Field;
}).toList(growable: false)
..sort(byName);
return _declaredFields;
}
List<TypeParameter> _typeParameters;
// a stronger hash?
@override
List<TypeParameter> get typeParameters {
_typeParameters ??= _extension.typeParameters.map((f) {
var lib = Library(f.enclosingElement.library, packageGraph);
return ModelElement.from(f, lib, packageGraph) as TypeParameter;
}).toList();
return _typeParameters;
}
@override
ParameterizedElementType get modelType => super.modelType;
List<ModelElement> _allModelElements;
@override
List<ModelElement> get allModelElements {
_allModelElements ??= List.from(
quiver.concat([
super.allModelElements,
typeParameters,
]),
growable: false);
return _allModelElements;
}
@override
String get filePath => '${library.dirName}/$fileName';
@override
String get href {
if (!identical(canonicalModelElement, this)) {
return canonicalModelElement?.href;
}
assert(canonicalLibrary != null);
assert(canonicalLibrary == library);
return '${package.baseHref}$filePath';
}
}