Fixed seralize_generator analysis warnings

This commit is contained in:
thomashii@dukefirehawk.com 2022-08-20 09:12:15 +08:00
parent 1540394586
commit eabe3b3810
6 changed files with 34 additions and 38 deletions

View file

@ -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) {
return 'const {${v.toMapValue()!.entries.map((entry) {
var k = dartObjectToString(entry.key!);
var v = dartObjectToString(entry.value!);
return '$k: $v';
}).join(', ') +
'}';
}).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;
}
}

View file

@ -31,7 +31,7 @@ final Map<String, BuildContext> _cache = {};
/// Create a [BuildContext].
Future<BuildContext?> buildContext(
ClassElement clazz,
InterfaceElement clazz,
ConstantReader annotation,
BuildStep buildStep,
Resolver resolver,
@ -62,7 +62,7 @@ Future<BuildContext?> 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;
}

View file

@ -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<DartObject> includeAnnotations;

View file

@ -254,7 +254,7 @@ class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
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<Serializable> {
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);

View file

@ -214,7 +214,7 @@ class ${pascal}Decoder extends Converter<Map, $pascal> {
return (map as Map<dynamic,dynamic>?)?..[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<Map, $pascal> {
}))
: $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

View file

@ -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 = '/// <reference types="${id.package}" />';
}
@ -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,