Skip to content

Commit

Permalink
Upgrade to latest analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Aug 9, 2022
1 parent 68df9c3 commit 2b5fdbc
Show file tree
Hide file tree
Showing 19 changed files with 71 additions and 58 deletions.
10 changes: 5 additions & 5 deletions drift_dev/lib/src/analyzer/custom_row_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:drift_dev/src/analyzer/errors.dart';
import 'package:drift_dev/src/analyzer/runner/steps.dart';

class FoundDartClass {
final ClassElement classElement;
final InterfaceElement classElement;

/// The instantiation of the [classElement], if the found type was a generic
/// typedef.
Expand Down Expand Up @@ -143,8 +143,8 @@ void _checkType(ParameterElement element, MoorColumn column, Step step) {
final isAllowedUint8List = column.typeConverter == null &&
column.type == ColumnType.blob &&
type is InterfaceType &&
type.element.name == 'Uint8List' &&
type.element.library.name == 'dart.typed_data';
type.element2.name == 'Uint8List' &&
type.element2.library.name == 'dart.typed_data';

if (!typesystem.isAssignableTo(expectedDartType.type, type) &&
!isAllowedUint8List) {
Expand All @@ -159,14 +159,14 @@ extension on TypeProvider {
case ColumnType.integer:
return intType;
case ColumnType.bigInt:
return intElement.library.getType('BigInt')!.instantiate(
return intElement.library.getClass('BigInt')!.instantiate(
typeArguments: const [], nullabilitySuffix: NullabilitySuffix.none);
case ColumnType.text:
return stringType;
case ColumnType.boolean:
return boolType;
case ColumnType.datetime:
return intElement.library.getType('DateTime')!.instantiate(
return intElement.library.getClass('DateTime')!.instantiate(
typeArguments: const [], nullabilitySuffix: NullabilitySuffix.none);
case ColumnType.blob:
return listType(intType);
Expand Down
12 changes: 7 additions & 5 deletions drift_dev/lib/src/analyzer/dart/column_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ class ColumnParser {
if (foundExplicitName != null) {
name = ColumnName.explicitly(foundExplicitName);
} else {
name = ColumnName.implicitly(ReCase(getter.name.name).snakeCase);
name = ColumnName.implicitly(ReCase(getter.name2.lexeme).snakeCase);
}

final columnType = _startMethodToColumnType(foundStartMethod);
Expand Down Expand Up @@ -424,7 +424,7 @@ class ColumnParser {
getter.documentationComment?.tokens.map((t) => t.toString()).join('\n');
return MoorColumn(
type: columnType,
dartGetterName: getter.name.name,
dartGetterName: getter.name2.lexeme,
name: name,
overriddenJsonName: _readJsonKey(element),
customConstraints: foundCustomConstraint,
Expand Down Expand Up @@ -456,9 +456,11 @@ class ColumnParser {
final annotations = getter.metadata;
final object = annotations.firstWhereOrNull((e) {
final value = e.computeConstantValue();
return value != null &&
isFromMoor(value.type!) &&
value.type!.element!.name == 'JsonKey';
final valueType = value?.type;

return valueType is InterfaceType &&
isFromMoor(valueType) &&
valueType.element2.name == 'JsonKey';
});

if (object == null) return null;
Expand Down
10 changes: 5 additions & 5 deletions drift_dev/lib/src/analyzer/dart/table_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class TableParser {

for (final annotation in element.metadata) {
final computed = annotation.computeConstantValue();
final annotationClass = computed!.type!.element!.name;
final annotationClass = computed!.type!.nameIfInterfaceType;

if (annotationClass == 'DataClassName') {
dataClassName = computed;
Expand Down Expand Up @@ -124,8 +124,8 @@ class TableParser {
useRowClass.getField('generateInsertable')!.toBoolValue()!;

if (type is InterfaceType) {
existingClass = FoundDartClass(type.element, type.typeArguments);
name = type.element.name;
existingClass = FoundDartClass(type.element2, type.typeArguments);
name = type.element2.name;
} else {
base.step.reportError(ErrorInDartCode(
message: 'The @UseRowClass annotation must be used with a class',
Expand Down Expand Up @@ -309,7 +309,7 @@ class TableParser {

Future<Iterable<MoorColumn>> _parseColumns(ClassElement element) async {
final columnNames = element.allSupertypes
.map((t) => t.element)
.map((t) => t.element2)
.followedBy([element])
.expand((e) => e.fields)
.where((field) =>
Expand Down Expand Up @@ -350,7 +350,7 @@ class _DataClassInformation {

extension on Element {
bool get isFromDefaultTable {
final parent = enclosingElement;
final parent = enclosingElement3;

return parent is ClassElement &&
parent.name == 'Table' &&
Expand Down
2 changes: 1 addition & 1 deletion drift_dev/lib/src/analyzer/dart/use_dao_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class UseDaoParser {
/// declared by that class and the referenced tables.
Future<Dao?> parseDao(ClassElement element, ConstantReader annotation) async {
final dbType = element.allSupertypes
.firstWhereOrNull((i) => i.element.name == 'DatabaseAccessor');
.firstWhereOrNull((i) => i.element2.name == 'DatabaseAccessor');

if (dbType == null) {
step.reportError(ErrorInDartCode(
Expand Down
16 changes: 8 additions & 8 deletions drift_dev/lib/src/analyzer/dart/view_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ViewParser {

for (final annotation in element.metadata) {
final computed = annotation.computeConstantValue();
final annotationClass = computed!.type!.element!.name;
final annotationClass = computed!.type!.nameIfInterfaceType;

if (annotationClass == 'DriftView') {
driftView = computed;
Expand Down Expand Up @@ -80,8 +80,8 @@ class ViewParser {
useRowClass.getField('generateInsertable')!.toBoolValue()!;

if (type is InterfaceType) {
existingClass = FoundDartClass(type.element, type.typeArguments);
name = type.element.name;
existingClass = FoundDartClass(type.element2, type.typeArguments);
name = type.element2.name;
} else {
base.step.reportError(ErrorInDartCode(
message: 'The @UseRowClass annotation must be used with a class',
Expand All @@ -100,7 +100,7 @@ class ViewParser {
Future<String> _parseViewName(ClassElement element) async {
for (final annotation in element.metadata) {
final computed = annotation.computeConstantValue();
final annotationClass = computed!.type!.element!.name;
final annotationClass = computed!.type!.nameIfInterfaceType;

if (annotationClass == 'DriftView') {
final name = computed.getField('name')?.toStringValue();
Expand All @@ -116,7 +116,7 @@ class ViewParser {

Future<Iterable<MoorColumn>> _parseColumns(ClassElement element) async {
final columnNames = element.allSupertypes
.map((t) => t.element)
.map((t) => t.element2)
.followedBy([element])
.expand((e) => e.fields)
.where((field) =>
Expand All @@ -134,7 +134,7 @@ class ViewParser {

final results = await Future.wait(fields.map((field) async {
final dartType = (field.type as InterfaceType).typeArguments[0];
final typeName = dartType.element!.name!;
final typeName = dartType.nameIfInterfaceType!;
final sqlType = _dartTypeToColumnType(typeName);

if (sqlType == null) {
Expand Down Expand Up @@ -181,7 +181,7 @@ class ViewParser {
Future<List<TableReferenceInDartView>> _parseStaticReferences(
ClassElement element, List<MoorTable> tables) async {
return await Stream.fromIterable(element.allSupertypes
.map((t) => t.element)
.map((t) => t.element2)
.followedBy([element]).expand((e) => e.fields))
.asyncMap((field) => _getStaticReference(field, tables))
.where((ref) => ref != null)
Expand All @@ -198,7 +198,7 @@ class ViewParser {
final type = tables.firstWhereOrNull(
(tbl) => tbl.fromClass!.name == node.returnType.toString());
if (type != null) {
final name = node.name.toString();
final name = node.name2.lexeme;
return TableReferenceInDartView(type, name);
}
}
Expand Down
8 changes: 4 additions & 4 deletions drift_dev/lib/src/analyzer/data_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ String? parseCustomParentClass(String dartTypeName, DartObject dataClassName,
final extendingType = extending.toTypeValue();
if (extendingType is InterfaceType) {
final superType = extendingType.allSupertypes
.any((type) => isFromMoor(type) && type.element.name == 'DataClass');
.any((type) => isFromMoor(type) && type.element2.name == 'DataClass');
if (!superType) {
base.step.reportError(
ErrorInDartCode(
Expand All @@ -52,10 +52,10 @@ String? parseCustomParentClass(String dartTypeName, DartObject dataClassName,
return null;
}

final className = extendingType.element.name;
final className = extendingType.nameIfInterfaceType;
if (extendingType.typeArguments.length == 1) {
final genericType = extendingType.typeArguments[0].element?.name;
if (genericType == 'Object' || genericType == 'dynamic') {
final genericType = extendingType.typeArguments[0];
if (genericType.isDartCoreObject || genericType.isDynamic) {
return '$className<$dartTypeName>';
} else {
base.step.reportError(
Expand Down
4 changes: 2 additions & 2 deletions drift_dev/lib/src/analyzer/moor/create_table_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ class CreateTableReader {
}

final asTypeConverter = type.allSupertypes.firstWhere(
(type) => isFromMoor(type) && type.element.name == 'TypeConverter');
(type) => isFromMoor(type) && type.element2.name == 'TypeConverter');

// TypeConverter<D, S>, where D is the type in Dart
final typeInDart = asTypeConverter.typeArguments.first;
Expand All @@ -273,7 +273,7 @@ class CreateTableReader {
mappedType: DriftDartType.of(typeInDart),
sqlType: sqlType,
alsoAppliesToJsonConversion:
helper.isJsonAwareTypeConverter(type, type.element.library),
helper.isJsonAwareTypeConverter(type, type.element2.library),
);
}

Expand Down
2 changes: 1 addition & 1 deletion drift_dev/lib/src/analyzer/moor/find_dart_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Future<FoundDartClass?> findDartClass(
} else if (foundElement is TypeAliasElement) {
final innerType = foundElement.aliasedType;
if (innerType is InterfaceType) {
return FoundDartClass(innerType.element, innerType.typeArguments);
return FoundDartClass(innerType.element2, innerType.typeArguments);
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions drift_dev/lib/src/analyzer/runner/steps/parse_dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ class ParseDartStep extends Step {
Future<List<MoorTable>> parseTables(
Iterable<DartType> types, Element initializedBy) {
return Future.wait(types.map((type) {
if (!_tableTypeChecker.isAssignableFrom(type.element!)) {
if (!_tableTypeChecker.isAssignableFromType(type)) {
reportError(ErrorInDartCode(
severity: Severity.criticalError,
message: 'The type $type is not a moor table',
affectedElement: initializedBy,
));
return Future.value(null);
} else {
return _parseTable(type.element as ClassElement);
return _parseTable((type as InterfaceType).element2 as ClassElement);
}
})).then((list) {
// only keep tables that were resolved successfully
Expand All @@ -145,15 +145,16 @@ class ParseDartStep extends Step {
Future<List<MoorView>> parseViews(
Iterable<DartType> types, Element initializedBy, List<MoorTable> tables) {
return Future.wait(types.map((type) {
if (!_viewTypeChecker.isAssignableFrom(type.element!)) {
if (!_viewTypeChecker.isAssignableFromType(type)) {
reportError(ErrorInDartCode(
severity: Severity.criticalError,
message: 'The type $type is not a drift view',
affectedElement: initializedBy,
));
return Future.value(null);
} else {
return _parseView(type.element as ClassElement, tables);
return _parseView(
(type as InterfaceType).element2 as ClassElement, tables);
}
})).then((list) {
// only keep tables that were resolved successfully
Expand Down
7 changes: 4 additions & 3 deletions drift_dev/lib/src/cli/commands/migrate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,8 @@ class _Moor2DriftDartRewriter extends GeneralizingAstVisitor<void> {

if (type is! InterfaceType) continue;

if (type.element.library.isDartCore && type.element.name == 'pragma') {
if (type.element2.library.isDartCore &&
type.element2.name == 'pragma') {
final name = value.getField('name')!.toStringValue()!;

if (name == 'moor2drift') {
Expand Down Expand Up @@ -452,8 +453,8 @@ class _Moor2DriftDartRewriter extends GeneralizingAstVisitor<void> {
if (type is! InterfaceType ||
// note that even old moor code uses these names since UseMoor/UseDao
// are type aliases to the new interfaces.
(type.element.name != 'DriftDatabase' &&
type.element.name != 'DriftAccessor')) {
(type.element2.name != 'DriftDatabase' &&
type.element2.name != 'DriftAccessor')) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion drift_dev/lib/src/model/base_entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ abstract class MoorEntityWithResultSet extends MoorSchemaEntity {
/// Information used by the generator to generate code for a custom data class
/// written by users.
class ExistingRowClass {
final ClassElement targetClass;
final InterfaceElement targetClass;

/// The Dart types that should be used to instantiate the [targetClass].
final List<DartType> typeInstantiation;
Expand Down
8 changes: 4 additions & 4 deletions drift_dev/lib/src/model/used_type_converter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:drift_dev/src/model/table.dart';
import 'package:drift_dev/src/utils/type_utils.dart';
import 'package:drift_dev/src/writer/writer.dart';
import 'package:drift_dev/writer.dart';

import 'column.dart';
import 'types.dart';
Expand Down Expand Up @@ -48,12 +48,12 @@ class UsedTypeConverter {
mappedType.nullabilitySuffix == NullabilitySuffix.question;

factory UsedTypeConverter.forEnumColumn(DartType enumType, bool nullable) {
if (enumType.element is! ClassElement) {
if (enumType is! InterfaceType) {
throw InvalidTypeForEnumConverterException('Not a class', enumType);
}

final creatingClass = enumType.element as ClassElement;
if (!creatingClass.isEnum) {
final creatingClass = enumType.element2;
if (creatingClass is! EnumElement) {
throw InvalidTypeForEnumConverterException('Not an enum', enumType);
}

Expand Down
15 changes: 11 additions & 4 deletions drift_dev/lib/src/utils/type_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,35 @@ import 'package:analyzer/dart/element/type.dart';
import 'package:drift_dev/src/writer/writer.dart';

bool isFromMoor(DartType type) {
final firstComponent = type.element?.library?.location?.components.first;
if (type is! InterfaceType) return false;

final firstComponent = type.element2.library.location?.components.first;
if (firstComponent == null) return false;

return firstComponent.contains('drift') || firstComponent.contains('moor');
}

bool isColumn(DartType type) {
final name = type.element?.name ?? '';
final name = type.nameIfInterfaceType;

return isFromMoor(type) &&
name != null &&
name.contains('Column') &&
!name.contains('Builder');
}

bool isExpression(DartType type) {
final name = type.element?.name ?? '';
final name = type.nameIfInterfaceType;

return isFromMoor(type) && name.startsWith('Expression');
return name != null && isFromMoor(type) && name.startsWith('Expression');
}

extension TypeUtils on DartType {
String? get nameIfInterfaceType {
final $this = this;
return $this is InterfaceType ? $this.element2.name : null;
}

String get userVisibleName => getDisplayString(withNullability: true);

/// How this type should look like in generated code.
Expand Down
2 changes: 1 addition & 1 deletion drift_dev/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ dependencies:
sqlparser: ^0.22.0

# Dart analysis
analyzer: "^4.1.0"
analyzer: "^4.4.0"
analyzer_plugin: ^0.11.0
source_span: ^1.5.5
package_config: ^2.0.0
Expand Down
2 changes: 1 addition & 1 deletion drift_dev/test/analyzer/dart/dart_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void main() {
expect(parser.returnExpressionOfMethod(node)!.toSource(), source);
}

final testClass = library.getType('Test');
final testClass = library.getClass('Test');

await _verifyReturnExpressionMatches(
testClass!.getGetter('getter')!, "'foo'");
Expand Down
2 changes: 1 addition & 1 deletion drift_dev/test/analyzer/dart/table_parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ void main() {
});

Future<MoorTable?> parse(String name) async {
return parser.parseTable(dartStep.library.getType(name)!);
return parser.parseTable(dartStep.library.getClass(name)!);
}

group('table names', () {
Expand Down
Loading

0 comments on commit 2b5fdbc

Please sign in to comment.