Skip to content

Commit 729d946

Browse files
authored
Don't use 'Element.type'. (#2028)
1 parent 900137b commit 729d946

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

lib/src/element_type.dart

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ library dartdoc.element_type;
88
import 'dart:collection';
99

1010
import 'package:analyzer/dart/element/element.dart';
11+
import 'package:analyzer/dart/element/nullability_suffix.dart';
1112
import 'package:analyzer/dart/element/type.dart';
1213
import 'package:dartdoc/src/model.dart';
1314
import 'package:dartdoc/src/model_utils.dart';
@@ -341,7 +342,7 @@ abstract class CallableElementTypeMixin implements ParameterizedElementType {
341342
if (type.typeFormals.isEmpty) {
342343
dartTypeArguments = type.typeArguments;
343344
} else {
344-
dartTypeArguments = type.typeFormals.map((f) => f.type);
345+
dartTypeArguments = type.typeFormals.map(_legacyTypeParameterType);
345346
}
346347
} else {
347348
DefinedElementType elementType = returnedFrom as DefinedElementType;
@@ -353,7 +354,7 @@ abstract class CallableElementTypeMixin implements ParameterizedElementType {
353354
returnedFrom.type.element is GenericFunctionTypeElement) {
354355
_typeArguments = (returnedFrom as DefinedElementType).typeArguments;
355356
} else {
356-
dartTypeArguments = type.typeFormals.map((f) => f.type);
357+
dartTypeArguments = type.typeFormals.map(_legacyTypeParameterType);
357358
}
358359
}
359360
if (dartTypeArguments != null) {
@@ -364,6 +365,20 @@ abstract class CallableElementTypeMixin implements ParameterizedElementType {
364365
}
365366
return _typeArguments;
366367
}
368+
369+
/// Return the [TypeParameterType] with the legacy nullability for the given
370+
/// type parameter [element].
371+
///
372+
/// TODO(scheglov) This method is a work around that fact that DartDoc
373+
/// currently represents both type formals and uses of them as actual types,
374+
/// as [TypeParameterType]s. This was not perfect, but worked before NNBD.
375+
/// With NNBD types have nullability suffixes, but type formals should not.
376+
/// Eventually we should separate models for type formals and types.
377+
static TypeParameterType _legacyTypeParameterType(
378+
TypeParameterElement element,
379+
) {
380+
return element.instantiate(nullabilitySuffix: NullabilitySuffix.star);
381+
}
367382
}
368383

369384
/// A callable type that may or may not be backed by a declaration using the generic

lib/src/model.dart

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,15 +1033,15 @@ class Class extends Container
10331033
bool get isCanonical => super.isCanonical && isPublic;
10341034

10351035
bool get isErrorOrException {
1036-
bool _doCheck(InterfaceType type) {
1037-
return (type.element.library.isDartCore &&
1038-
(type.name == 'Exception' || type.name == 'Error'));
1036+
bool _doCheck(ClassElement element) {
1037+
return (element.library.isDartCore &&
1038+
(element.name == 'Exception' || element.name == 'Error'));
10391039
}
10401040

10411041
// if this class is itself Error or Exception, return true
1042-
if (_doCheck(_cls.type)) return true;
1042+
if (_doCheck(_cls)) return true;
10431043

1044-
return _cls.allSupertypes.any(_doCheck);
1044+
return _cls.allSupertypes.map((t) => t.element).any(_doCheck);
10451045
}
10461046

10471047
/// Returns true if [other] is a parent class for this class.
@@ -1121,12 +1121,12 @@ class Class extends Container
11211121
List<ExecutableElement> get _inheritedElements {
11221122
if (__inheritedElements == null) {
11231123
var classElement = element as ClassElement;
1124-
var classType = classElement.type;
1125-
if (classType.isObject) {
1124+
if (classElement.isDartCoreObject) {
11261125
return __inheritedElements = <ExecutableElement>[];
11271126
}
11281127

11291128
var inheritance = definingLibrary.inheritanceManager;
1129+
var classType = classElement.thisType;
11301130
var cmap = inheritance.getInheritedConcreteMap(classType);
11311131
var imap = inheritance.getInheritedMap(classType);
11321132

@@ -4049,8 +4049,7 @@ abstract class ModelElement extends Canonicalization
40494049
// If we're calling this with an empty name, we probably have the wrong
40504050
// element associated with a ModelElement or there's an analysis bug.
40514051
assert(name.isNotEmpty ||
4052-
(this.element is TypeDefiningElement &&
4053-
(this.element as TypeDefiningElement).type.name == "dynamic") ||
4052+
this.element?.kind == ElementKind.DYNAMIC ||
40544053
this is ModelFunction);
40554054

40564055
if (href == null) {

0 commit comments

Comments
 (0)