Avoid picking wrong import alias

Closes #2904
This commit is contained in:
Simon Binder 2024-02-29 18:08:51 +01:00
parent 98fd6a0ef0
commit 863dbb61a9
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
3 changed files with 22 additions and 7 deletions

View File

@ -1,3 +1,7 @@
## 2.17.0-dev
- Fix drift using the wrong import alias in generated part files.
## 2.16.0
- Keep import alias when referencing existing elements in generated code

View File

@ -31,13 +31,23 @@ class ImportManagerForPartFiles extends ImportManager {
// Part files can't add their own imports, so try to find the element in an
// existing import.
for (final MapEntry(:key, :value) in _namedImports.entries) {
if (value.containsKey(elementName)) {
final foundHere = value[elementName];
if (foundHere != null && _matchingUrl(definitionUri, foundHere)) {
return key;
}
}
return null;
}
static bool _matchingUrl(Uri wanted, Element target) {
final targetUri = target.librarySource?.uri;
if (targetUri == null || targetUri.scheme != wanted.scheme) {
return false;
}
return true;
}
}
class NullImportManager extends ImportManager {

View File

@ -38,16 +38,17 @@ CREATE INDEX b_idx /* comment should be stripped */ ON b (foo, upper(foo));
final result = await emulateDriftBuild(
inputs: {
'a|lib/main.dart': r'''
import 'dart:io' as io;
import 'package:drift/drift.dart' as drift;
import 'tables.dart' as tables;
@drift.DriftDatabase(tables: [tables.Texts])
@drift.DriftDatabase(tables: [tables.Files])
class MyDatabase extends _$MyDatabase {}
''',
'a|lib/tables.dart': '''
import 'package:drift/drift.dart';
class Texts extends Table {
class Files extends Table {
TextColumn get content => text()();
}
''',
@ -59,12 +60,12 @@ class Texts extends Table {
'a|lib/main.drift.dart': decodedMatches(
allOf(
contains(
r'class $TextsTable extends tables.Texts with '
r'drift.TableInfo<$TextsTable, Text>',
r'class $FilesTable extends tables.Files with '
r'drift.TableInfo<$FilesTable, File>',
),
contains(
'class Text extends drift.DataClass implements '
'drift.Insertable<Text>',
'class File extends drift.DataClass implements '
'drift.Insertable<File>',
),
),
),