Fix spellfix1 module with new analyzer

This commit is contained in:
Simon Binder 2022-11-22 20:05:41 +01:00
parent fde7adde48
commit 59d347adbe
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
4 changed files with 56 additions and 10 deletions

View File

@ -75,6 +75,8 @@ class DriftAnalysisDriver {
const DriftNativeExtension(), const DriftNativeExtension(),
if (options.hasModule(SqlModule.math)) const BuiltInMathExtension(), if (options.hasModule(SqlModule.math)) const BuiltInMathExtension(),
if (options.hasModule(SqlModule.rtree)) const RTreeExtension(), if (options.hasModule(SqlModule.rtree)) const RTreeExtension(),
if (options.hasModule(SqlModule.spellfix1))
const Spellfix1Extension(),
], ],
version: options.sqliteVersion, version: options.sqliteVersion,
), ),
@ -175,7 +177,9 @@ class DriftAnalysisDriver {
try { try {
await resolver.resolveDiscovered(discovered); await resolver.resolveDiscovered(discovered);
} catch (e, s) { } catch (e, s) {
backend.log.warning('Could not analyze ${discovered.ownId}', e, s); if (e is! CouldNotResolveElementException) {
backend.log.warning('Could not analyze ${discovered.ownId}', e, s);
}
} }
} }
} }
@ -227,3 +231,10 @@ class DriftAnalysisDriver {
abstract class AnalysisResultCacheReader { abstract class AnalysisResultCacheReader {
Future<String?> readCacheFor(Uri uri); Future<String?> readCacheFor(Uri uri);
} }
/// Thrown by a local element resolver when an element could not be resolved and
/// a more helpful error has already been added as an analysis error for the
/// user to see.
class CouldNotResolveElementException implements Exception {
const CouldNotResolveElementException();
}

View File

@ -1,6 +1,7 @@
import 'package:analyzer/dart/ast/ast.dart' as dart; import 'package:analyzer/dart/ast/ast.dart' as dart;
import 'package:collection/collection.dart'; import 'package:collection/collection.dart';
import 'package:drift/drift.dart' show DriftSqlType; import 'package:drift/drift.dart' show DriftSqlType;
import 'package:drift_dev/src/analysis/driver/driver.dart';
import 'package:recase/recase.dart'; import 'package:recase/recase.dart';
import 'package:sqlparser/sqlparser.dart' hide PrimaryKeyColumn, UniqueColumn; import 'package:sqlparser/sqlparser.dart' hide PrimaryKeyColumn, UniqueColumn;
import 'package:sqlparser/sqlparser.dart' as sql; import 'package:sqlparser/sqlparser.dart' as sql;
@ -35,15 +36,10 @@ class DriftTableResolver extends LocalElementResolver<DiscoveredDriftTable> {
resolver.driver.options.storeDateTimeValuesAsText, resolver.driver.options.storeDateTimeValuesAsText,
); );
table = reader.read(stmt); table = reader.read(stmt);
} catch (e, s) { } on CantReadSchemaException catch (e) {
resolver.driver.backend.log reportError(DriftAnalysisError.inDriftFile(stmt.tableNameToken ?? stmt,
.warning('Error reading table from internal statement', e, s); 'Drift was unable to analyze this table: ${e.message}'));
reportError(DriftAnalysisError.inDriftFile( throw const CouldNotResolveElementException();
stmt.tableNameToken ?? stmt,
'The structure of this table could not be extracted, possibly due to a '
'bug in drift_dev.',
));
rethrow;
} }
final columns = <DriftColumn>[]; final columns = <DriftColumn>[];

View File

@ -3,6 +3,8 @@ import 'package:drift_dev/src/analysis/options.dart';
import 'package:sqlparser/sqlparser.dart'; import 'package:sqlparser/sqlparser.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'test_utils.dart';
void main() { void main() {
DriftOptions parse(String yaml) { DriftOptions parse(String yaml) {
return checkedYamlDecode(yaml, (m) => DriftOptions.fromJson(m!)); return checkedYamlDecode(yaml, (m) => DriftOptions.fromJson(m!));
@ -71,4 +73,17 @@ sqlite:
), ),
); );
}); });
test('reports error about table when module is not imported', () async {
final backend = TestBackend.inTest({
'a|lib/a.drift': 'CREATE VIRTUAL TABLE place_spellfix USING spellfix1;',
});
final file = await backend.analyze('package:a/a.drift');
expect(file.analyzedElements, isEmpty);
expect(file.allErrors, [
isDriftError(contains('Unknown module "spellfix1", did you register it?'))
.withSpan('place_spellfix'),
]);
});
} }

View File

@ -1,4 +1,6 @@
import 'package:drift/drift.dart';
import 'package:drift_dev/src/analysis/options.dart'; import 'package:drift_dev/src/analysis/options.dart';
import 'package:drift_dev/src/analysis/results/table.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import '../../test_utils.dart'; import '../../test_utils.dart';
@ -68,4 +70,26 @@ SELECT rowid, highlight(example_table_search, 0, '[match]', '[match]') name,
expect(result.allErrors, expect(result.allErrors,
[isDriftError(contains('Function simple_query could not be found'))]); [isDriftError(contains('Function simple_query could not be found'))]);
}); });
test('supports spellfix1 tables', () async {
final state = TestBackend.inTest(
{'a|lib/a.drift': 'CREATE VIRTUAL TABLE demo USING spellfix1;'},
options: DriftOptions.defaults(
dialect: DialectOptions(
SqlDialect.sqlite,
SqliteAnalysisOptions(
modules: [SqlModule.spellfix1],
),
),
),
);
final file = await state.analyze('package:a/a.drift');
state.expectNoErrors();
final table = file.analyzedElements.single as DriftTable;
expect(
table.columns.map((e) => e.nameInSql),
containsAll(['word', 'rank', 'distance', 'langid', 'score', 'matchlen']),
);
});
} }