Skip to content

Don't use 'Element.type'. #2028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions lib/src/element_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ library dartdoc.element_type;
import 'dart:collection';

import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:dartdoc/src/model.dart';
import 'package:dartdoc/src/model_utils.dart';
Expand Down Expand Up @@ -341,7 +342,7 @@ abstract class CallableElementTypeMixin implements ParameterizedElementType {
if (type.typeFormals.isEmpty) {
dartTypeArguments = type.typeArguments;
} else {
dartTypeArguments = type.typeFormals.map((f) => f.type);
dartTypeArguments = type.typeFormals.map(_legacyTypeParameterType);
}
} else {
DefinedElementType elementType = returnedFrom as DefinedElementType;
Expand All @@ -353,7 +354,7 @@ abstract class CallableElementTypeMixin implements ParameterizedElementType {
returnedFrom.type.element is GenericFunctionTypeElement) {
_typeArguments = (returnedFrom as DefinedElementType).typeArguments;
} else {
dartTypeArguments = type.typeFormals.map((f) => f.type);
dartTypeArguments = type.typeFormals.map(_legacyTypeParameterType);
}
}
if (dartTypeArguments != null) {
Expand All @@ -364,6 +365,20 @@ abstract class CallableElementTypeMixin implements ParameterizedElementType {
}
return _typeArguments;
}

/// Return the [TypeParameterType] with the legacy nullability for the given
/// type parameter [element].
///
/// TODO(scheglov) This method is a work around that fact that DartDoc
/// currently represents both type formals and uses of them as actual types,
/// as [TypeParameterType]s. This was not perfect, but worked before NNBD.
/// With NNBD types have nullability suffixes, but type formals should not.
/// Eventually we should separate models for type formals and types.
static TypeParameterType _legacyTypeParameterType(
TypeParameterElement element,
) {
return element.instantiate(nullabilitySuffix: NullabilitySuffix.star);
}
}

/// A callable type that may or may not be backed by a declaration using the generic
Expand Down
17 changes: 8 additions & 9 deletions lib/src/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1033,15 +1033,15 @@ class Class extends Container
bool get isCanonical => super.isCanonical && isPublic;

bool get isErrorOrException {
bool _doCheck(InterfaceType type) {
return (type.element.library.isDartCore &&
(type.name == 'Exception' || type.name == 'Error'));
bool _doCheck(ClassElement element) {
return (element.library.isDartCore &&
(element.name == 'Exception' || element.name == 'Error'));
}

// if this class is itself Error or Exception, return true
if (_doCheck(_cls.type)) return true;
if (_doCheck(_cls)) return true;

return _cls.allSupertypes.any(_doCheck);
return _cls.allSupertypes.map((t) => t.element).any(_doCheck);
}

/// Returns true if [other] is a parent class for this class.
Expand Down Expand Up @@ -1121,12 +1121,12 @@ class Class extends Container
List<ExecutableElement> get _inheritedElements {
if (__inheritedElements == null) {
var classElement = element as ClassElement;
var classType = classElement.type;
if (classType.isObject) {
if (classElement.isDartCoreObject) {
return __inheritedElements = <ExecutableElement>[];
}

var inheritance = definingLibrary.inheritanceManager;
var classType = classElement.thisType;
var cmap = inheritance.getInheritedConcreteMap(classType);
var imap = inheritance.getInheritedMap(classType);

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

if (href == null) {
Expand Down