From a721e896d3831336d64189cfd49cf73c31039fc4 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Mon, 4 Apr 2022 21:13:07 +0200 Subject: [PATCH] Add warnings for `customConstraint` misuse --- .../lib/src/analyzer/dart/column_parser.dart | 39 +++++++++++++++++++ .../test/analyzer/dart/table_parser_test.dart | 31 +++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/drift_dev/lib/src/analyzer/dart/column_parser.dart b/drift_dev/lib/src/analyzer/dart/column_parser.dart index f28598a1..a2005b8a 100644 --- a/drift_dev/lib/src/analyzer/dart/column_parser.dart +++ b/drift_dev/lib/src/analyzer/dart/column_parser.dart @@ -29,6 +29,14 @@ const String _methodDefault = 'withDefault'; const String _methodClientDefault = 'clientDefault'; const String _methodMap = 'map'; const String _methodGenerated = 'generatedAs'; +const Set _addsSqlConstraint = { + _methodNamed, + _methodReferences, + _methodAutoIncrement, + _methodUnique, + _methodDefault, + _methodGenerated +}; const String _errorMessage = 'This getter does not create a valid column that ' 'can be parsed by moor. Please refer to the readme from moor to see how ' @@ -74,6 +82,7 @@ class ColumnParser { DriftDartType? typeConverterRuntime; ColumnGeneratedAs? generatedAs; var nullable = false; + var hasDefaultConstraints = false; final foundFeatures = []; @@ -85,6 +94,10 @@ class ColumnParser { break; } + if (_addsSqlConstraint.contains(methodName)) { + hasDefaultConstraints = true; + } + switch (methodName) { case _methodNamed: if (foundExplicitName != null) { @@ -223,6 +236,19 @@ class ColumnParser { foundFeatures.add(const UniqueKey()); break; case _methodCustomConstraint: + if (foundCustomConstraint != null) { + base.step.reportError( + ErrorInDartCode( + severity: Severity.warning, + affectedElement: getter.declaredElement, + affectedNode: remainingExpr.methodName, + message: + "You've already set custom constraints on this column, " + 'they will be overriden by this call.', + ), + ); + } + foundCustomConstraint = base.readStringLiteral( remainingExpr.argumentList.arguments.first, () { base.step.reportError( @@ -370,6 +396,19 @@ class ColumnParser { ); } + if (hasDefaultConstraints && foundCustomConstraint != null) { + base.step.reportError( + ErrorInDartCode( + severity: Severity.warning, + affectedElement: getter.declaredElement, + message: 'This column definition is using both drift-defined ' + 'constraints (like references, autoIncrement, ...) and a ' + 'customConstraint(). Only the custom constraint will be added ' + 'to the column in SQL!', + ), + ); + } + final docString = getter.documentationComment?.tokens.map((t) => t.toString()).join('\n'); return MoorColumn( diff --git a/drift_dev/test/analyzer/dart/table_parser_test.dart b/drift_dev/test/analyzer/dart/table_parser_test.dart index 407b08c0..5573ab4c 100644 --- a/drift_dev/test/analyzer/dart/table_parser_test.dart +++ b/drift_dev/test/analyzer/dart/table_parser_test.dart @@ -11,6 +11,7 @@ import 'package:drift_dev/writer.dart'; import 'package:test/test.dart'; import '../../utils/test_backend.dart'; +import '../utils.dart'; void main() { late TestBackend backend; @@ -118,6 +119,14 @@ void main() { @DriftDatabase(tables: [Foo, DoesNotExist]) class Database {} ''', + AssetId.parse('test_lib|lib/invalid_constraints.dart'): ''' + import 'package:drift/drift.dart'; + + class InvalidConstraints extends Table { + IntColumn get a => integer().autoIncrement().customConstraint('foo')(); + IntColumn get b => integer().customConstraint('a').customConstraint('b')(); + } + ''', }); }); tearDownAll(() { @@ -366,4 +375,26 @@ void main() { ), ); }); + + test('reports errors around suspicous customConstraint uses', () async { + final session = MoorSession(backend); + final uri = Uri.parse('package:test_lib/invalid_constraints.dart'); + final backendTask = backend.startTask(uri); + final task = session.startTask(backendTask); + await task.runTask(); + + final file = session.registerFile(uri); + file.expectDartError( + allOf( + contains( + 'This column definition is using both drift-defined constraints'), + contains('and a customConstraint()'), + ), + 'a', + ); + file.expectDartError( + contains("You've already set custom constraints on this column"), + 'customConstraint', + ); + }); }