Skip to content

Commit 1a3f9c2

Browse files
authored
Convert to instantiateToBounds2 and eliminate deprecated analyzer API usage (#2192)
* cleanup * Cleanups and type fixing * dartfmt
1 parent 9625d07 commit 1a3f9c2

File tree

5 files changed

+37
-38
lines changed

5 files changed

+37
-38
lines changed

lib/src/element_type.dart

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import 'package:analyzer/dart/element/element.dart';
1111
import 'package:analyzer/dart/element/nullability_suffix.dart';
1212
import 'package:analyzer/dart/element/type.dart';
1313
import 'package:analyzer/src/dart/element/element.dart' show ClassElementImpl;
14-
import 'package:analyzer/src/generated/type_system.dart';
1514
import 'package:dartdoc/src/model/model.dart';
1615
import 'package:dartdoc/src/render/element_type_renderer.dart';
1716

@@ -143,16 +142,19 @@ class FunctionTypeElementType extends UndefinedElementType {
143142
PackageGraph packageGraph, ElementType returnedFrom)
144143
: super(f, library, packageGraph, returnedFrom);
145144

145+
@override
146+
FunctionType get type => super.type;
147+
146148
@override
147149
List<Parameter> get parameters {
148-
List<ParameterElement> params = (type as FunctionType).parameters;
150+
List<ParameterElement> params = type.parameters;
149151
return UnmodifiableListView<Parameter>(params
150152
.map((p) => ModelElement.from(p, library, packageGraph) as Parameter)
151153
.toList());
152154
}
153155

154-
ElementType get returnType => ElementType.from(
155-
(type as FunctionType).returnType, library, packageGraph, this);
156+
ElementType get returnType =>
157+
ElementType.from(type.returnType, library, packageGraph, this);
156158

157159
@override
158160
String get linkedName {
@@ -176,7 +178,7 @@ class FunctionTypeElementType extends UndefinedElementType {
176178
}
177179

178180
List<TypeParameter> get typeFormals {
179-
List<TypeParameterElement> typeFormals = (type as FunctionType).typeFormals;
181+
List<TypeParameterElement> typeFormals = type.typeFormals;
180182
return UnmodifiableListView<TypeParameter>(typeFormals
181183
.map(
182184
(p) => ModelElement.from(p, library, packageGraph) as TypeParameter)
@@ -223,16 +225,16 @@ class TypeParameterElementType extends DefinedElementType {
223225
: super(type, library, packageGraph, element, returnedFrom);
224226

225227
@override
226-
String get linkedName => name;
228+
TypeParameterType get type => super.type;
227229

228230
@override
229-
String get nameWithGenerics => name;
231+
String get linkedName => name;
230232

231233
@override
232-
ClassElement get _boundClassElement => type.element;
234+
String get nameWithGenerics => name;
233235

234236
@override
235-
DartType get _bound => (type as TypeParameterType).bound;
237+
DartType get _bound => type.bound;
236238
}
237239

238240
/// An [ElementType] associated with an [Element].
@@ -294,11 +296,6 @@ abstract class DefinedElementType extends ElementType {
294296
return _typeArguments;
295297
}
296298

297-
/// By default, the bound is the type of the declared class.
298-
ClassElement get _boundClassElement => (element.element as ClassElement);
299-
Class get boundClass =>
300-
ModelElement.fromElement(_boundClassElement, packageGraph);
301-
302299
DartType get _bound => type;
303300

304301
DartType _instantiatedType;
@@ -311,10 +308,10 @@ abstract class DefinedElementType extends ElementType {
311308
!(_bound as InterfaceType)
312309
.typeArguments
313310
.every((t) => t is InterfaceType)) {
314-
var typeSystem = library.element.typeSystem as TypeSystemImpl;
315-
// TODO(jcollins-g): convert to ClassElement.instantiateToBounds
316-
// dart-lang/dartdoc#2135
317-
_instantiatedType = typeSystem.instantiateToBounds(_bound);
311+
var typeSystem = library.element.typeSystem;
312+
_instantiatedType = typeSystem.instantiateToBounds2(
313+
classElement: _bound.element as ClassElement,
314+
nullabilitySuffix: _bound.nullabilitySuffix);
318315
} else {
319316
_instantiatedType = _bound;
320317
}

lib/src/model/class.dart

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,20 @@ class Class extends Container
2424
Class(ClassElement element, Library library, PackageGraph packageGraph)
2525
: super(element, library, packageGraph) {
2626
packageGraph.specialClasses.addSpecial(this);
27-
mixins = _cls.mixins
27+
mixins = element.mixins
2828
.map((f) {
2929
DefinedElementType t = ElementType.from(f, library, packageGraph);
3030
return t;
3131
})
3232
.where((mixin) => mixin != null)
3333
.toList(growable: false);
3434

35-
if (_cls.supertype != null && _cls.supertype.element.supertype != null) {
36-
supertype = ElementType.from(_cls.supertype, library, packageGraph);
35+
if (element.supertype != null &&
36+
element.supertype.element.supertype != null) {
37+
supertype = ElementType.from(element.supertype, library, packageGraph);
3738
}
3839

39-
_interfaces = _cls.interfaces
40+
_interfaces = element.interfaces
4041
.map((f) =>
4142
ElementType.from(f, library, packageGraph) as DefinedElementType)
4243
.toList(growable: false);
@@ -168,7 +169,7 @@ class Class extends Container
168169
List<Constructor> get constructors {
169170
if (_constructors != null) return _constructors;
170171

171-
_constructors = _cls.constructors.map((e) {
172+
_constructors = element.constructors.map((e) {
172173
return ModelElement.from(e, library, packageGraph) as Constructor;
173174
}).toList(growable: true)
174175
..sort(byName);
@@ -183,6 +184,9 @@ class Class extends Container
183184
@override
184185
ModelElement get enclosingElement => library;
185186

187+
@override
188+
ClassElement get element => super.element;
189+
186190
@override
187191
String get fileName => '$name-class.$fileType';
188192

@@ -319,7 +323,7 @@ class Class extends Container
319323
Iterable<DefinedElementType> get publicInterfaces =>
320324
model_utils.filterNonPublic(interfaces);
321325

322-
bool get isAbstract => _cls.isAbstract;
326+
bool get isAbstract => element.isAbstract;
323327

324328
@override
325329
bool get isCanonical => super.isCanonical && isPublic;
@@ -331,9 +335,9 @@ class Class extends Container
331335
}
332336

333337
// if this class is itself Error or Exception, return true
334-
if (_doCheck(_cls)) return true;
338+
if (_doCheck(element)) return true;
335339

336-
return _cls.allSupertypes.map((t) => t.element).any(_doCheck);
340+
return element.allSupertypes.map((t) => t.element).any(_doCheck);
337341
}
338342

339343
/// Returns true if [other] is a parent class for this class.
@@ -410,13 +414,12 @@ class Class extends Container
410414

411415
List<ExecutableElement> get _inheritedElements {
412416
if (__inheritedElements == null) {
413-
var classElement = element as ClassElement;
414-
if (classElement.isDartCoreObject) {
417+
if (element.isDartCoreObject) {
415418
return __inheritedElements = <ExecutableElement>[];
416419
}
417420

418421
var inheritance = definingLibrary.inheritanceManager;
419-
var classType = classElement.thisType;
422+
var classType = element.thisType;
420423
var cmap = inheritance.getInheritedConcreteMap(classType);
421424
var imap = inheritance.getInheritedMap(classType);
422425

@@ -454,7 +457,7 @@ class Class extends Container
454457
// For half-inherited fields, the analyzer only links the non-inherited
455458
// to the [FieldElement]. Compose our [Field] class by hand by looking up
456459
// inherited accessors that may be related.
457-
for (FieldElement f in _cls.fields) {
460+
for (FieldElement f in element.fields) {
458461
PropertyAccessorElement getterElement = f.getter;
459462
if (getterElement == null && accessorMap.containsKey(f.name)) {
460463
getterElement = accessorMap[f.name]
@@ -543,14 +546,12 @@ class Class extends Container
543546
_fields.add(field);
544547
}
545548

546-
ClassElement get _cls => (element as ClassElement);
547-
548549
List<Method> _methods;
549550

550551
@override
551552
List<Method> get methods {
552553
if (_methods == null) {
553-
_methods = _cls.methods.map((e) {
554+
_methods = element.methods.map((e) {
554555
return ModelElement.from(e, library, packageGraph) as Method;
555556
}).toList(growable: false)
556557
..sort(byName);
@@ -564,7 +565,7 @@ class Class extends Container
564565
@override
565566
List<TypeParameter> get typeParameters {
566567
if (_typeParameters == null) {
567-
_typeParameters = _cls.typeParameters.map((f) {
568+
_typeParameters = element.typeParameters.map((f) {
568569
var lib = Library(f.enclosingElement.library, packageGraph);
569570
return ModelElement.from(f, lib, packageGraph) as TypeParameter;
570571
}).toList();

lib/src/model/mixin.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ class Mixin extends Class {
4545
/// Returns a list of superclass constraints for this mixin.
4646
Iterable<ParameterizedElementType> get superclassConstraints {
4747
if (_superclassConstraints == null) {
48-
_superclassConstraints = (element as ClassElement)
49-
.superclassConstraints
48+
_superclassConstraints = element.superclassConstraints
5049
.map<ParameterizedElementType>(
5150
(InterfaceType i) => ElementType.from(i, library, packageGraph))
5251
.toList();

lib/src/model/model_element.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ abstract class ModelElement extends Canonicalization
838838
}
839839

840840
CompilationUnitElement get compilationUnitElement =>
841-
element.getAncestor((e) => e is CompilationUnitElement);
841+
element.thisOrAncestorOfType<CompilationUnitElement>();
842842

843843
bool get hasAnnotations => annotations.isNotEmpty;
844844

lib/src/model/package_builder.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:async';
66
import 'dart:io';
77

8+
import 'package:analyzer/dart/analysis/features.dart';
89
import 'package:analyzer/dart/analysis/results.dart';
910
import 'package:analyzer/dart/element/element.dart';
1011
import 'package:analyzer/file_system/file_system.dart' as file_system;
@@ -157,7 +158,8 @@ class PackageBuilder {
157158
AnalysisOptionsImpl options = AnalysisOptionsImpl();
158159

159160
// TODO(jcollins-g): pass in an ExperimentStatus instead?
160-
options.enabledExperiments = config.enableExperiment;
161+
options.contextFeatures =
162+
FeatureSet.fromEnableFlags(config.enableExperiment);
161163

162164
// TODO(jcollins-g): Make use of currently not existing API for managing
163165
// many AnalysisDrivers

0 commit comments

Comments
 (0)