diff --git a/moor_generator/lib/src/analyzer/dart/use_moor_parser.dart b/moor_generator/lib/src/analyzer/dart/use_moor_parser.dart index c5f6742f..803c0616 100644 --- a/moor_generator/lib/src/analyzer/dart/use_moor_parser.dart +++ b/moor_generator/lib/src/analyzer/dart/use_moor_parser.dart @@ -11,9 +11,17 @@ class UseMoorParser { Future parseDatabase( ClassElement element, ConstantReader annotation) async { // the types declared in UseMoor.tables - final tableTypes = - annotation.peek('tables')?.listValue?.map((obj) => obj.toTypeValue()) ?? - []; + final tablesOrNull = + annotation.peek('tables')?.listValue?.map((obj) => obj.toTypeValue()); + if (tablesOrNull == null) { + step.reportError(ErrorInDartCode( + message: 'Could not read tables from @UseMoor annotation! \n' + 'Please make sure that all table classes exist.', + affectedElement: element, + )); + } + + final tableTypes = tablesOrNull ?? []; final queryStrings = annotation.peek('queries')?.mapValue ?? {}; final includes = annotation .read('include') diff --git a/moor_generator/test/analyzer/dart/table_parser_test.dart b/moor_generator/test/analyzer/dart/table_parser_test.dart index 567d35f5..e1bca816 100644 --- a/moor_generator/test/analyzer/dart/table_parser_test.dart +++ b/moor_generator/test/analyzer/dart/table_parser_test.dart @@ -93,7 +93,17 @@ void main() { @override Set get primaryKey => {other}; } - ''' + ''', + AssetId.parse('test_lib|lib/invalid_reference.dart'): ''' + import 'package:moor/moor.dart'; + + class Foo extends Table { + IntColumn get id => integer().autoIncrement()(); + } + + @UseMoor(tables: [Foo, DoesNotExist]) + class Database {} + ''', }); }); tearDownAll(() { @@ -284,4 +294,27 @@ void main() { ), ); }); + + test('reports errors for unknown classes in UseMoor', () async { + final session = MoorSession(backend); + final uri = Uri.parse('package:test_lib/invalid_reference.dart'); + final backendTask = backend.startTask(uri); + final task = session.startTask(backendTask); + await task.runTask(); + + final file = session.registerFile(uri); + expect( + file.errors.errors, + contains( + isA().having( + (e) => e.message, + 'message', + allOf( + contains('Could not read tables from @UseMoor annotation!'), + contains('Please make sure that all table classes exist.'), + ), + ), + ), + ); + }); }