From eabe3b381055d23e31006a17f15a0e9772e54afe Mon Sep 17 00:00:00 2001 From: "thomashii@dukefirehawk.com" Date: Sat, 20 Aug 2022 09:12:15 +0800 Subject: [PATCH] Fixed seralize_generator analysis warnings --- .../lib/angel3_serialize_generator.dart | 44 +++++++++---------- .../lib/build_context.dart | 4 +- .../lib/context.dart | 2 +- .../angel_serialize_generator/lib/model.dart | 8 ++-- .../lib/serialize.dart | 4 +- .../lib/typescript.dart | 10 ++--- 6 files changed, 34 insertions(+), 38 deletions(-) diff --git a/packages/serialize/angel_serialize_generator/lib/angel3_serialize_generator.dart b/packages/serialize/angel_serialize_generator/lib/angel3_serialize_generator.dart index 01ea5ed8..658c9ce1 100644 --- a/packages/serialize/angel_serialize_generator/lib/angel3_serialize_generator.dart +++ b/packages/serialize/angel_serialize_generator/lib/angel3_serialize_generator.dart @@ -40,7 +40,7 @@ Builder typescriptDefinitionBuilder(_) { /// Converts a [DartType] to a [TypeReference]. TypeReference convertTypeReference(DartType t, {bool forceNullable = false}) { return TypeReference((b) { - b.symbol = t.element?.displayName; + b.symbol = t.element2?.displayName; // Generate nullable type if (t.nullabilitySuffix == NullabilitySuffix.question || forceNullable) { @@ -59,7 +59,7 @@ Expression convertObject(DartObject o) { if (o.toIntValue() != null) return literalNum(o.toIntValue()!); if (o.toDoubleValue() != null) return literalNum(o.toDoubleValue()!); if (o.toSymbolValue() != null) { - return CodeExpression(Code('#' + o.toSymbolValue()!)); + return CodeExpression(Code('#${o.toSymbolValue()!}')); } if (o.toStringValue() != null) return literalString(o.toStringValue()!); if (o.toTypeValue() != null) return convertTypeReference(o.toTypeValue()!); @@ -92,37 +92,33 @@ String? dartObjectToString(DartObject v) { return v.toDoubleValue().toString(); } if (v.toSymbolValue() != null) { - return '#' + v.toSymbolValue()!; + return '#${v.toSymbolValue()!}'; } if (v.toTypeValue() != null) { return v.toTypeValue()!.getDisplayString(withNullability: true); } if (v.toListValue() != null) { - return 'const [' + - v.toListValue()!.map(dartObjectToString).join(', ') + - ']'; + return 'const [${v.toListValue()!.map(dartObjectToString).join(', ')}]'; } if (v.toMapValue() != null) { - return 'const {' + - v.toMapValue()!.entries.map((entry) { - var k = dartObjectToString(entry.key!); - var v = dartObjectToString(entry.value!); - return '$k: $v'; - }).join(', ') + - '}'; + return 'const {${v.toMapValue()!.entries.map((entry) { + var k = dartObjectToString(entry.key!); + var v = dartObjectToString(entry.value!); + return '$k: $v'; + }).join(', ')}}'; } if (v.toStringValue() != null) { return literalString(v.toStringValue()!) .accept(DartEmitter(useNullSafetySyntax: true)) .toString(); } - if (type is InterfaceType && type.element.isEnum) { + if (type is InterfaceType && type.element2 is Enum) { // Find the index of the enum, then find the member. - for (var field in type.element.fields) { + for (var field in type.element2.fields) { if (field.isEnumConstant && field.isStatic) { - var value = type.element.getField(field.name)!.computeConstantValue(); + var value = type.element2.getField(field.name)!.computeConstantValue(); if (value == v) { - return '${type.element.displayName}.${field.name}'; + return '${type.element2.displayName}.${field.name}'; } } } @@ -135,11 +131,11 @@ String? dartObjectToString(DartObject v) { bool isModelClass(DartType? t) { if (t == null) return false; - if (serializableTypeChecker.hasAnnotationOf(t.element!)) { + if (serializableTypeChecker.hasAnnotationOf(t.element2!)) { return true; } - if (generatedSerializableTypeChecker.hasAnnotationOf(t.element!)) { + if (generatedSerializableTypeChecker.hasAnnotationOf(t.element2!)) { return true; } @@ -172,7 +168,7 @@ bool isListOrMapType(DartType t) { bool isEnumType(DartType t) { if (t is InterfaceType) { - return t.element.isEnum; + return t.element2 is Enum; } return false; @@ -199,13 +195,13 @@ bool isAssignableToModel(DartType type) => String? typeToString(DartType type) { if (type is InterfaceType) { if (type.typeArguments.isEmpty) { - return type.element.displayName; + return type.element2.displayName; } - var name = type.element.displayName; + var name = type.element2.displayName; - return name + '<' + type.typeArguments.map(typeToString).join(', ') + '>'; + return '$name<${type.typeArguments.map(typeToString).join(', ')}>'; } else { - return type.element?.displayName; + return type.element2?.displayName; } } diff --git a/packages/serialize/angel_serialize_generator/lib/build_context.dart b/packages/serialize/angel_serialize_generator/lib/build_context.dart index f8a5b242..acbd0444 100644 --- a/packages/serialize/angel_serialize_generator/lib/build_context.dart +++ b/packages/serialize/angel_serialize_generator/lib/build_context.dart @@ -31,7 +31,7 @@ final Map _cache = {}; /// Create a [BuildContext]. Future buildContext( - ClassElement clazz, + InterfaceElement clazz, ConstantReader annotation, BuildStep buildStep, Resolver resolver, @@ -62,7 +62,7 @@ Future buildContext( // Crawl for classes from parent classes. void crawlClass(InterfaceType? t) { while (t != null) { - fields.insertAll(0, t.element.fields); + fields.insertAll(0, t.element2.fields); t.interfaces.forEach(crawlClass); t = t.superclass; } diff --git a/packages/serialize/angel_serialize_generator/lib/context.dart b/packages/serialize/angel_serialize_generator/lib/context.dart index 516541cf..faa31457 100644 --- a/packages/serialize/angel_serialize_generator/lib/context.dart +++ b/packages/serialize/angel_serialize_generator/lib/context.dart @@ -42,7 +42,7 @@ class BuildContext { final ConstantReader annotation; - final ClassElement clazz; + final InterfaceElement clazz; /// Any annotations to include in the generated class. final List includeAnnotations; diff --git a/packages/serialize/angel_serialize_generator/lib/model.dart b/packages/serialize/angel_serialize_generator/lib/model.dart index 3285cb5e..11096f72 100644 --- a/packages/serialize/angel_serialize_generator/lib/model.dart +++ b/packages/serialize/angel_serialize_generator/lib/model.dart @@ -254,7 +254,7 @@ class JsonModelGenerator extends GeneratorForAnnotation { if (const TypeChecker.fromRuntime(List).isAssignableFromType(type)) { if (type.typeArguments.length == 1) { var eq = generateEquality(type.typeArguments[0]); - return 'ListEquality<${type.typeArguments[0].element!.name}>($eq)'; + return 'ListEquality<${type.typeArguments[0].element2!.name}>($eq)'; } else { return 'ListEquality()'; } @@ -263,20 +263,20 @@ class JsonModelGenerator extends GeneratorForAnnotation { if (type.typeArguments.length == 2) { var keq = generateEquality(type.typeArguments[0]), veq = generateEquality(type.typeArguments[1]); - return 'MapEquality<${type.typeArguments[0].element!.name}, ${type.typeArguments[1].element!.name}>(keys: $keq, values: $veq)'; + return 'MapEquality<${type.typeArguments[0].element2!.name}, ${type.typeArguments[1].element2!.name}>(keys: $keq, values: $veq)'; } else { return 'MapEquality()'; } } - return nullable ? null : 'DefaultEquality<${type.element.name}>()'; + return nullable ? null : 'DefaultEquality<${type.element2.name}>()'; } else { return 'DefaultEquality()'; } } static String Function(String, String) generateComparator(DartType type) { - if (type is! InterfaceType || type.element.displayName == 'dynamic') { + if (type is! InterfaceType || type.element2.displayName == 'dynamic') { return (a, b) => '$a == $b'; } var eq = generateEquality(type, true); diff --git a/packages/serialize/angel_serialize_generator/lib/serialize.dart b/packages/serialize/angel_serialize_generator/lib/serialize.dart index bede5a26..4c5c26ce 100644 --- a/packages/serialize/angel_serialize_generator/lib/serialize.dart +++ b/packages/serialize/angel_serialize_generator/lib/serialize.dart @@ -214,7 +214,7 @@ class ${pascal}Decoder extends Converter { return (map as Map?)?..[key] = ${serializerToMap(rc, 'model.${field.name}[key]')}; })'''; - } else if (type.element.isEnum) { + } else if (type.element2 is Enum) { var convert = (field.type.nullabilitySuffix == NullabilitySuffix.question) ? '!' @@ -390,7 +390,7 @@ class ${pascal}Decoder extends Converter { })) : $defaultValue '''; - } else if (type.element.isEnum) { + } else if (type.element2 is Enum) { deserializedRepresentation = ''' map['$alias'] is ${type.getDisplayString(withNullability: true)} ? (map['$alias'] as ${type.getDisplayString(withNullability: true)}) ?? $defaultValue diff --git a/packages/serialize/angel_serialize_generator/lib/typescript.dart b/packages/serialize/angel_serialize_generator/lib/typescript.dart index 45bbf304..78187784 100644 --- a/packages/serialize/angel_serialize_generator/lib/typescript.dart +++ b/packages/serialize/angel_serialize_generator/lib/typescript.dart @@ -77,15 +77,15 @@ class TypeScriptDefinitionBuilder implements Builder { } } else if (isModelClass(type)) { var sourcePath = buildStep.inputId.uri.toString(); - var targetPath = type.element.source.uri.toString(); + var targetPath = type.element2.source.uri.toString(); if (!p.equals(sourcePath, targetPath)) { var relative = p.relative(targetPath, from: sourcePath); String? ref; - if (type.element.source.uri.scheme == 'asset') { + if (type.element2.source.uri.scheme == 'asset') { var id = - AssetId.resolve(Uri.parse(type.element.source.uri.toString())); + AssetId.resolve(Uri.parse(type.element2.source.uri.toString())); if (id.package != buildStep.inputId.package) { ref = '/// '; } @@ -105,9 +105,9 @@ class TypeScriptDefinitionBuilder implements Builder { } var ctx = await buildContext( - type.element, + type.element2, ConstantReader( - serializableTypeChecker.firstAnnotationOf(type.element)), + serializableTypeChecker.firstAnnotationOf(type.element2)), buildStep, buildStep.resolver, autoSnakeCaseNames,