Skip to content

Commit

Permalink
Avoid picking wrong import alias
Browse files Browse the repository at this point in the history
Closes #2904
  • Loading branch information
simolus3 committed Feb 29, 2024
1 parent 98fd6a0 commit 863dbb6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
4 changes: 4 additions & 0 deletions drift_dev/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 11 additions & 1 deletion drift_dev/lib/src/writer/import_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 7 additions & 6 deletions drift_dev/test/backends/build/build_integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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()();
}
''',
Expand All @@ -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>',
),
),
),
Expand Down

0 comments on commit 863dbb6

Please sign in to comment.