Warn about unparsed customConstraints

This commit is contained in:
Simon Binder
2025-10-22 20:27:27 +02:00
parent 05e68ddafe
commit 2b093d7148
2 changed files with 37 additions and 8 deletions
@@ -418,15 +418,12 @@ class DartTableResolver extends LocalElementResolver<DiscoveredDartTable> {
if (expression is ListLiteral) {
for (final entry in expression.elements) {
if (entry is StringLiteral) {
final value = entry.stringValue;
if (value != null) {
foundConstraints.add(value);
foundConstraintSources.add(entry);
}
if (entry case StringLiteral(:final stringValue?)) {
foundConstraints.add(stringValue);
foundConstraintSources.add(entry);
} else {
reportError(DriftAnalysisError.inDartAst(
element, entry, 'This must be a string literal.'));
reportError(DriftAnalysisError.inDartAst(element, entry,
'Drift can only verify custom constraints set as constant string literals.'));
}
}
} else {
@@ -491,4 +491,36 @@ class Preferences extends Table {
expect(file.allErrors,
[isDriftError(contains('is only meaningful for `STRICT` tables'))]);
});
test('warns about non-constant custom constraints', () async {
final backend = await TestBackend.inTest({
'a|lib/main.dart': r'''
import 'package:drift/drift.dart';
@DataClassName('MyTable')
class MyTables extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get name => text().nullable()();
}
@DataClassName('MyOtherThing')
class MyOtherThings extends Table {
IntColumn get id => integer().autoIncrement()();
IntColumn get otherId => integer()();
IntColumn get blahId => integer()();
TextColumn get name => text().nullable()();
@override
List<String> get customConstraints => ["UNIQUE(${otherId.name}, ${blahId.name})"];
}
''',
});
final file = await backend.analyze('package:a/main.dart');
expect(file.allErrors, [
isDriftError(contains(
'can only verify custom constraints set as constant string literals.'))
]);
});
}