Fix extending classes in modular mode (#2260)

This commit is contained in:
Simon Binder 2023-01-10 12:24:48 +01:00
parent ac7aa48933
commit 6b4a186b15
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
5 changed files with 51 additions and 9 deletions

View File

@ -5,6 +5,8 @@ import 'package:path/path.dart' show url;
import 'package:recase/recase.dart';
import 'package:sqlparser/sqlparser.dart';
import 'dart.dart';
part '../../generated/analysis/results/element.g.dart';
@sealed
@ -87,9 +89,9 @@ abstract class DriftElement {
/// If this element was extracted from a defined Dart class, returns the name
/// of that class.
String? get definingDartClass {
AnnotatedDartCode? get definingDartClass {
if (id.isDefinedInDart) {
return declaration.name;
return AnnotatedDartCode.importedSymbol(id.libraryUri, declaration.name!);
}
return null;
}

View File

@ -342,13 +342,19 @@ class TableWriter extends TableOrViewWriter {
} else {
// Regular generation, write full table class
final dataClass = emitter.dartCode(emitter.writer.rowType(table));
final tableDslName = table.definingDartClass ?? emitter.drift('Table');
final tableDslName = table.definingDartClass ??
AnnotatedDartCode.importedSymbol(AnnotatedDartCode.drift, 'Table');
// class UsersTable extends Users implements TableInfo<Users, User> {
final typeArgs = '<${table.entityInfoName}, $dataClass>';
buffer.write('class ${table.entityInfoName} extends $tableDslName with '
'${emitter.drift('TableInfo')}$typeArgs ');
emitter
..write('class ${table.entityInfoName} extends ')
..writeDart(tableDslName)
..write(' with ')
..writeDart(AnnotatedDartCode.importedSymbol(
AnnotatedDartCode.drift, 'TableInfo'))
..write(typeArgs);
if (table.isVirtual) {
buffer.write(', ${emitter.drift('VirtualTableInfo')}$typeArgs ');

View File

@ -68,10 +68,10 @@ abstract class TodoItemWithCategoryNameView extends View {
result.analysis.values.map((e) => e.result).whereType<DriftView>();
expect(views, hasLength(2));
final todoCategoryItemCount = views
.singleWhere((e) => e.definingDartClass == 'TodoCategoryItemCount');
final todoItemWithCategoryName = views.singleWhere(
(e) => e.definingDartClass == 'TodoItemWithCategoryNameView');
final todoCategoryItemCount = views.singleWhere(
(e) => e.definingDartClass.toString() == 'TodoCategoryItemCount');
final todoItemWithCategoryName = views.singleWhere((e) =>
e.definingDartClass.toString() == 'TodoItemWithCategoryNameView');
expect(
todoCategoryItemCount.source,

View File

@ -0,0 +1,32 @@
import 'package:build_test/build_test.dart';
import 'package:test/test.dart';
import '../utils.dart';
void main() {
test('references right parent class for Dart-defined tables', () async {
final result = await emulateDriftBuild(
inputs: {
'a|lib/a.dart': '''
import 'package:drift/drift.dart';
class Tags extends Table {
IntColumn get id => integer().autoIncrement()();
}
''',
},
modularBuild: true,
);
checkOutputs({
'a|lib/a.drift.dart': decodedMatches(
allOf(
contains("import 'package:a/a.dart' as i2;"),
contains(
r'class $TagsTable extends i2.Tags with i0.TableInfo<$TagsTable, i1.Tag>',
),
),
),
}, result.dartOutputs, result);
});
}

View File

@ -2,6 +2,8 @@ targets:
$default:
builders:
drift_dev:
# Disable the default builder in favor of the modular builders configured
# below.
enabled: false
drift_dev:analyzer: