Prepare for separating ClassElement, EnumElement, and MixinElement. (#2025)

* Prepare for separating ClassElement, EnumElement, and MixinElement.

* Improve error UX when casting to ClassElement

Co-authored-by: Simon Binder <oss@simonbinder.eu>
This commit is contained in:
Konstantin Scheglov 2022-08-29 11:44:09 -07:00 committed by GitHub
parent 98cb4be7cc
commit b399db78e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 11 deletions

View File

@ -23,7 +23,7 @@ Future<FoundDartClass?> findDartClass(
} }
final foundElement = library.exportNamespace.get(identifier); final foundElement = library.exportNamespace.get(identifier);
if (foundElement is ClassElement) { if (foundElement is InterfaceElement) {
return FoundDartClass(foundElement, null); return FoundDartClass(foundElement, null);
} else if (foundElement is TypeAliasElement) { } else if (foundElement is TypeAliasElement) {
final innerType = foundElement.aliasedType; final innerType = foundElement.aliasedType;

View File

@ -22,13 +22,13 @@ class HelperLibrary {
/// Returns `null` if [type] is not a subtype of `TypeConverter`. /// Returns `null` if [type] is not a subtype of `TypeConverter`.
InterfaceType? asTypeConverter(DartType type) { InterfaceType? asTypeConverter(DartType type) {
final converter = final converter =
helperLibrary.exportNamespace.get('TypeConverter') as ClassElement; helperLibrary.exportNamespace.get('TypeConverter') as InterfaceElement;
return type.asInstanceOf(converter); return type.asInstanceOf(converter);
} }
bool isJsonAwareTypeConverter(DartType? type, LibraryElement context) { bool isJsonAwareTypeConverter(DartType? type, LibraryElement context) {
final jsonMixin = final jsonMixin = helperLibrary.exportNamespace.get('JsonTypeConverter')
helperLibrary.exportNamespace.get('JsonTypeConverter') as ClassElement; as InterfaceElement;
final jsonConverterType = jsonMixin.instantiate( final jsonConverterType = jsonMixin.instantiate(
typeArguments: [ typeArguments: [
context.typeProvider.dynamicType, context.typeProvider.dynamicType,

View File

@ -122,15 +122,18 @@ class ParseDartStep extends Step {
Future<List<DriftTable>> parseTables( Future<List<DriftTable>> parseTables(
Iterable<DartType> types, Element initializedBy) { Iterable<DartType> types, Element initializedBy) {
return Future.wait(types.map((type) { return Future.wait(types.map((type) {
if (!_tableTypeChecker.isAssignableFromType(type)) { final element = type is InterfaceType ? type.element2 : null;
if (!_tableTypeChecker.isAssignableFromType(type) ||
element is! ClassElement) {
reportError(ErrorInDartCode( reportError(ErrorInDartCode(
severity: Severity.criticalError, severity: Severity.criticalError,
message: 'The type $type is not a moor table', message: 'The type $type is not a drift table class.',
affectedElement: initializedBy, affectedElement: initializedBy,
)); ));
return Future.value(null); return Future.value(null);
} else { } else {
return _parseTable((type as InterfaceType).element2 as ClassElement); return _parseTable(element);
} }
})).then((list) { })).then((list) {
// only keep tables that were resolved successfully // only keep tables that were resolved successfully
@ -145,16 +148,18 @@ class ParseDartStep extends Step {
Future<List<MoorView>> parseViews(Iterable<DartType> types, Future<List<MoorView>> parseViews(Iterable<DartType> types,
Element initializedBy, List<DriftTable> tables) { Element initializedBy, List<DriftTable> tables) {
return Future.wait(types.map((type) { return Future.wait(types.map((type) {
if (!_viewTypeChecker.isAssignableFromType(type)) { final element = type is InterfaceType ? type.element2 : null;
if (!_viewTypeChecker.isAssignableFromType(type) ||
element is! ClassElement) {
reportError(ErrorInDartCode( reportError(ErrorInDartCode(
severity: Severity.criticalError, severity: Severity.criticalError,
message: 'The type $type is not a drift view', message: 'The type $type is not a drift view class.',
affectedElement: initializedBy, affectedElement: initializedBy,
)); ));
return Future.value(null); return Future.value(null);
} else { } else {
return _parseView( return _parseView(element, tables);
(type as InterfaceType).element2 as ClassElement, tables);
} }
})).then((list) { })).then((list) {
// only keep tables that were resolved successfully // only keep tables that were resolved successfully