Skip to content

Commit

Permalink
Fix drift_dev tests
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Sep 10, 2023
1 parent da0392c commit 0fd19ab
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
35 changes: 24 additions & 11 deletions drift_dev/lib/src/cli/commands/migrate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import 'package:analyzer/dart/analysis/analysis_context.dart';
import 'package:analyzer/dart/analysis/analysis_context_collection.dart';
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/syntactic_entity.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:collection/collection.dart';
import 'package:drift_dev/src/utils/string_escaper.dart';
import 'package:io/ansi.dart';
import 'package:path/path.dart' as p;
import 'package:source_span/source_span.dart';
import 'package:sqlparser/sqlparser.dart' hide AnalysisContext, StringLiteral;
import 'package:sqlparser/sqlparser.dart'
hide AnalysisContext, StringLiteral, SyntacticEntity, AstNode;
import 'package:yaml/yaml.dart';

import '../cli.dart';
Expand Down Expand Up @@ -386,24 +389,23 @@ class _Moor2DriftDartRewriter extends GeneralizingAstVisitor<void> {
_writer.replace(uri.offset, uri.length, asDartLiteral(newImport));
}

@override
void visitSimpleIdentifier(SimpleIdentifier node) {
void _transformIdentifier(
SyntacticEntity identifier, String name, Element? element) {
String? newIdentifier;

if (node.name == 'FlutterQueryExecutor') {
if (name == 'FlutterQueryExecutor') {
newIdentifier = 'SqfliteQueryExecutor';
} else {
var element = node.staticElement;
if (element == null) {
if (element == null && identifier is AstNode) {
// It looks like left-hand identifiers of assignments don't have a
// static element, infer from parent.
if (node.parent is AssignmentExpression) {
element = (node.parent as AssignmentExpression).writeElement;
if (identifier.parent is AssignmentExpression) {
element = (identifier.parent as AssignmentExpression).writeElement;
}

if (element == null) return;
}

if (element == null) return;

for (final annotation in element.metadata) {
final value = annotation.computeConstantValue();
if (value == null) return;
Expand All @@ -423,10 +425,21 @@ class _Moor2DriftDartRewriter extends GeneralizingAstVisitor<void> {
}

if (newIdentifier != null) {
_writer.replace(node.offset, node.length, newIdentifier);
_writer.replace(identifier.offset, name.length, newIdentifier);
}
}

@override
void visitSimpleIdentifier(SimpleIdentifier node) {
_transformIdentifier(node, node.name, node.staticElement);
}

@override
void visitNamedType(NamedType node) {
_transformIdentifier(node.name2, node.name2.lexeme, node.element);
super.visitNamedType(node);
}

@override
void visitAnnotation(Annotation node) {
// Replace @UseMoor with @DriftDatabase first
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,9 @@ class FooData {
'a|lib/a.dart': '''
import 'package:drift/drift.dart';
@UseRowClass((id: int, name: String, birthday: DateTime))
typedef MyRecord = ({int id, String name, DateTime birthday});
@UseRowClass(MyRecord)
class Users extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get name => text()();
Expand Down

0 comments on commit 0fd19ab

Please sign in to comment.