Skip to content

Commit

Permalink
Update generator, but stay on analyzer 5.13 (#1401)
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko authored Sep 15, 2023
1 parent 9a16bc4 commit 70ebe26
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 23 deletions.
1 change: 0 additions & 1 deletion generator/lib/src/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import 'dart:math';

import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/constant/value.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:realm_generator/src/annotation_value.dart';
import 'package:realm_generator/src/expanded_context_span.dart';
Expand Down
2 changes: 1 addition & 1 deletion generator/lib/src/error.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class RealmInvalidGenerationSourceError extends InvalidGenerationSourceError {
color = color ?? session.color,
super(message, todo: todo, element: element) {
if (element is FieldElement || element is ConstructorElement) {
final classElement = element.enclosingElement3!;
final classElement = element.enclosingElement!;
this.secondarySpans.addAll({
classElement.span!: "in realm model for '${session.mapping.entries.where((e) => e.value == classElement).singleOrNull?.key}'",
});
Expand Down
16 changes: 8 additions & 8 deletions generator/lib/src/field_element_ex.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ extension FieldElementEx on FieldElement {

TypeAnnotation? get typeAnnotation => declarationAstNode.fields.type;

Expression? get initializerExpression => declarationAstNode.fields.variables.singleWhere((v) => v.name2.toString() == name).initializer;
Expression? get initializerExpression => declarationAstNode.fields.variables.singleWhere((v) => v.name.toString() == name).initializer;

FileSpan? typeSpan(SourceFile file) => ExpandedContextSpan(
ExpandedContextSpan(
Expand Down Expand Up @@ -187,17 +187,17 @@ extension FieldElementEx on FieldElement {
String? linkOriginProperty;

// Validate field type
final modelSpan = enclosingElement3.span!;
final modelSpan = enclosingElement.span!;
final file = modelSpan.file;
final realmType = type.realmType;
if (realmType == null) {
final notARealmTypeSpan = type.element2?.span;
final notARealmTypeSpan = type.element?.span;
String todo;
if (notARealmTypeSpan != null) {
todo = //
"Add a @RealmModel annotation on '$mappedTypeName', "
"or an @Ignored annotation on '$displayName'.";
} else if (type.isDynamic && mappedTypeName != 'dynamic' && !mappedTypeName.startsWith(session.prefix)) {
} else if (session.mapping['_$mappedTypeName'] != null) {
todo = "Did you intend to use _$mappedTypeName as type for '$displayName'?";
} else {
todo = "Remove the invalid field or add an @Ignored annotation on '$displayName'.";
Expand All @@ -209,7 +209,7 @@ extension FieldElementEx on FieldElement {
primarySpan: typeSpan(file),
primaryLabel: '$modelTypeName is not a realm model type',
secondarySpans: {
modelSpan: "in realm model '${enclosingElement3.displayName}'",
modelSpan: "in realm model '${enclosingElement.displayName}'",
// may go both above and below, or stem from another file
if (notARealmTypeSpan != null) notARealmTypeSpan: ''
},
Expand Down Expand Up @@ -244,7 +244,7 @@ extension FieldElementEx on FieldElement {
primarySpan: typeSpan(file),
primaryLabel: 'Set element type is not supported',
element: this,
todo: 'Ensure set element type ${typeArgument} is a type supported by RealmSet.');
todo: 'Ensure set element type $typeArgument is a type supported by RealmSet.');
}

if (realmType == RealmPropertyType.mixed && typeArgument.isNullable) {
Expand Down Expand Up @@ -280,7 +280,7 @@ extension FieldElementEx on FieldElement {

final sourceFieldName = backlink.value.getField('fieldName')?.toSymbolValue();
final sourceType = (type as ParameterizedType).typeArguments.first;
final sourceField = (sourceType.element2 as ClassElement?)?.fields.where((f) => f.name == sourceFieldName).singleOrNull;
final sourceField = (sourceType.element as ClassElement?)?.fields.where((f) => f.name == sourceFieldName).singleOrNull;

if (sourceField == null) {
throw RealmInvalidGenerationSourceError(
Expand All @@ -292,7 +292,7 @@ extension FieldElementEx on FieldElement {
);
}

final thisType = (enclosingElement3 as ClassElement).thisType;
final thisType = (enclosingElement as ClassElement).thisType;
final linkType = thisType.asNullable;
final listOf = session.typeProvider.listType(thisType);
if (sourceField.type != linkType && sourceField.type != listOf) {
Expand Down
2 changes: 1 addition & 1 deletion generator/lib/src/pseudo_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ class PseudoType extends TypeImpl {
Element? get element2 => null;

@override
Element? get element => _never;
Element? get element => null;
}
2 changes: 1 addition & 1 deletion generator/lib/src/realm_field_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class RealmFieldInfo {
String get basicNonNullableMappedTypeName => type.basicType.asNonNullable.mappedName;

String get basicRealmTypeName =>
fieldElement.modelType.basicType.asNonNullable.element2?.remappedRealmName ?? fieldElement.modelType.asNonNullable.basicMappedName;
fieldElement.modelType.basicType.asNonNullable.element?.remappedRealmName ?? fieldElement.modelType.asNonNullable.basicMappedName;

String get modelTypeName => fieldElement.modelTypeName;

Expand Down
2 changes: 1 addition & 1 deletion generator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ environment:
sdk: ^3.0.2

dependencies:
analyzer: ^5.0.0
analyzer: ^5.13.0
build_resolvers: ^2.0.9
build: ^2.0.0
dart_style: ^2.2.0
Expand Down
3 changes: 2 additions & 1 deletion generator/test/error_test_data/missing_underscore.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:realm_common/realm_common.dart';

part 'missing_underscore.g.dart';
// part 'missing_underscore.g.dart';
class Other {} // dummy stand-in for the generated class

@RealmModel()
class _Bad {
Expand Down
21 changes: 12 additions & 9 deletions generator/test/error_test_data/missing_underscore.expected
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
Not a realm type

in: asset:pkg/test/error_test_data/missing_underscore.dart:7:8
5 │ @RealmModel()
6 │ class _Bad {
│ ━━━━ in realm model for 'Bad'
7 │ late Other other;
│ ^^^^^ Other is not a realm model type
Did you intend to use _Other as type for 'other'?
in: asset:pkg/test/error_test_data/missing_underscore.dart:8:8
4 │ class Other {} // dummy stand-in for the generated class
│ ━━━━━
... │
6 │ @RealmModel()
7 │ class _Bad {
│ ━━━━ in realm model for 'Bad'
8 │ late Other other;
│ ^^^^^ Other is not a realm model type
Add a @RealmModel annotation on 'Other', or an @Ignored annotation on 'other'.

0 comments on commit 70ebe26

Please sign in to comment.