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]. /// Converts a [DartType] to a [TypeReference].
TypeReference convertTypeReference(DartType t, {bool forceNullable = false}) { TypeReference convertTypeReference(DartType t, {bool forceNullable = false}) {
return TypeReference((b) { return TypeReference((b) {
b.symbol = t.element?.displayName; b.symbol = t.element2?.displayName;
// Generate nullable type // Generate nullable type
if (t.nullabilitySuffix == NullabilitySuffix.question || forceNullable) { if (t.nullabilitySuffix == NullabilitySuffix.question || forceNullable) {
@ -59,7 +59,7 @@ Expression convertObject(DartObject o) {
if (o.toIntValue() != null) return literalNum(o.toIntValue()!); if (o.toIntValue() != null) return literalNum(o.toIntValue()!);
if (o.toDoubleValue() != null) return literalNum(o.toDoubleValue()!); if (o.toDoubleValue() != null) return literalNum(o.toDoubleValue()!);
if (o.toSymbolValue() != null) { if (o.toSymbolValue() != null) {
return CodeExpression(Code('#' + o.toSymbolValue()!)); return CodeExpression(Code('#${o.toSymbolValue()!}'));
} }
if (o.toStringValue() != null) return literalString(o.toStringValue()!); if (o.toStringValue() != null) return literalString(o.toStringValue()!);
if (o.toTypeValue() != null) return convertTypeReference(o.toTypeValue()!); if (o.toTypeValue() != null) return convertTypeReference(o.toTypeValue()!);
@ -92,37 +92,33 @@ String? dartObjectToString(DartObject v) {
return v.toDoubleValue().toString(); return v.toDoubleValue().toString();
} }
if (v.toSymbolValue() != null) { if (v.toSymbolValue() != null) {
return '#' + v.toSymbolValue()!; return '#${v.toSymbolValue()!}';
} }
if (v.toTypeValue() != null) { if (v.toTypeValue() != null) {
return v.toTypeValue()!.getDisplayString(withNullability: true); return v.toTypeValue()!.getDisplayString(withNullability: true);
} }
if (v.toListValue() != null) { if (v.toListValue() != null) {
return 'const [' + return 'const [${v.toListValue()!.map(dartObjectToString).join(', ')}]';
v.toListValue()!.map(dartObjectToString).join(', ') +
']';
} }
if (v.toMapValue() != null) { if (v.toMapValue() != null) {
return 'const {' + return 'const {${v.toMapValue()!.entries.map((entry) {
v.toMapValue()!.entries.map((entry) {
var k = dartObjectToString(entry.key!); var k = dartObjectToString(entry.key!);
var v = dartObjectToString(entry.value!); var v = dartObjectToString(entry.value!);
return '$k: $v'; return '$k: $v';
}).join(', ') + }).join(', ')}}';
'}';
} }
if (v.toStringValue() != null) { if (v.toStringValue() != null) {
return literalString(v.toStringValue()!) return literalString(v.toStringValue()!)
.accept(DartEmitter(useNullSafetySyntax: true)) .accept(DartEmitter(useNullSafetySyntax: true))
.toString(); .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. // 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) { if (field.isEnumConstant && field.isStatic) {
var value = type.element.getField(field.name)!.computeConstantValue(); var value = type.element2.getField(field.name)!.computeConstantValue();
if (value == v) { 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) { bool isModelClass(DartType? t) {
if (t == null) return false; if (t == null) return false;
if (serializableTypeChecker.hasAnnotationOf(t.element!)) { if (serializableTypeChecker.hasAnnotationOf(t.element2!)) {
return true; return true;
} }
if (generatedSerializableTypeChecker.hasAnnotationOf(t.element!)) { if (generatedSerializableTypeChecker.hasAnnotationOf(t.element2!)) {
return true; return true;
} }
@ -172,7 +168,7 @@ bool isListOrMapType(DartType t) {
bool isEnumType(DartType t) { bool isEnumType(DartType t) {
if (t is InterfaceType) { if (t is InterfaceType) {
return t.element.isEnum; return t.element2 is Enum;
} }
return false; return false;
@ -199,13 +195,13 @@ bool isAssignableToModel(DartType type) =>
String? typeToString(DartType type) { String? typeToString(DartType type) {
if (type is InterfaceType) { if (type is InterfaceType) {
if (type.typeArguments.isEmpty) { 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 { } else {
return type.element?.displayName; return type.element2?.displayName;
} }
} }

View file

@ -31,7 +31,7 @@ final Map<String, BuildContext> _cache = {};
/// Create a [BuildContext]. /// Create a [BuildContext].
Future<BuildContext?> buildContext( Future<BuildContext?> buildContext(
ClassElement clazz, InterfaceElement clazz,
ConstantReader annotation, ConstantReader annotation,
BuildStep buildStep, BuildStep buildStep,
Resolver resolver, Resolver resolver,
@ -62,7 +62,7 @@ Future<BuildContext?> buildContext(
// Crawl for classes from parent classes. // Crawl for classes from parent classes.
void crawlClass(InterfaceType? t) { void crawlClass(InterfaceType? t) {
while (t != null) { while (t != null) {
fields.insertAll(0, t.element.fields); fields.insertAll(0, t.element2.fields);
t.interfaces.forEach(crawlClass); t.interfaces.forEach(crawlClass);
t = t.superclass; t = t.superclass;
} }

View file

@ -42,7 +42,7 @@ class BuildContext {
final ConstantReader annotation; final ConstantReader annotation;
final ClassElement clazz; final InterfaceElement clazz;
/// Any annotations to include in the generated class. /// Any annotations to include in the generated class.
final List<DartObject> includeAnnotations; final List<DartObject> includeAnnotations;

View file

@ -254,7 +254,7 @@ class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
if (const TypeChecker.fromRuntime(List).isAssignableFromType(type)) { if (const TypeChecker.fromRuntime(List).isAssignableFromType(type)) {
if (type.typeArguments.length == 1) { if (type.typeArguments.length == 1) {
var eq = generateEquality(type.typeArguments[0]); var eq = generateEquality(type.typeArguments[0]);
return 'ListEquality<${type.typeArguments[0].element!.name}>($eq)'; return 'ListEquality<${type.typeArguments[0].element2!.name}>($eq)';
} else { } else {
return 'ListEquality()'; return 'ListEquality()';
} }
@ -263,20 +263,20 @@ class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
if (type.typeArguments.length == 2) { if (type.typeArguments.length == 2) {
var keq = generateEquality(type.typeArguments[0]), var keq = generateEquality(type.typeArguments[0]),
veq = generateEquality(type.typeArguments[1]); 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 { } else {
return 'MapEquality()'; return 'MapEquality()';
} }
} }
return nullable ? null : 'DefaultEquality<${type.element.name}>()'; return nullable ? null : 'DefaultEquality<${type.element2.name}>()';
} else { } else {
return 'DefaultEquality()'; return 'DefaultEquality()';
} }
} }
static String Function(String, String) generateComparator(DartType type) { 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'; return (a, b) => '$a == $b';
} }
var eq = generateEquality(type, true); 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] = return (map as Map<dynamic,dynamic>?)?..[key] =
${serializerToMap(rc, 'model.${field.name}[key]')}; ${serializerToMap(rc, 'model.${field.name}[key]')};
})'''; })''';
} else if (type.element.isEnum) { } else if (type.element2 is Enum) {
var convert = var convert =
(field.type.nullabilitySuffix == NullabilitySuffix.question) (field.type.nullabilitySuffix == NullabilitySuffix.question)
? '!' ? '!'
@ -390,7 +390,7 @@ class ${pascal}Decoder extends Converter<Map, $pascal> {
})) }))
: $defaultValue : $defaultValue
'''; ''';
} else if (type.element.isEnum) { } else if (type.element2 is Enum) {
deserializedRepresentation = ''' deserializedRepresentation = '''
map['$alias'] is ${type.getDisplayString(withNullability: true)} map['$alias'] is ${type.getDisplayString(withNullability: true)}
? (map['$alias'] as ${type.getDisplayString(withNullability: true)}) ?? $defaultValue ? (map['$alias'] as ${type.getDisplayString(withNullability: true)}) ?? $defaultValue

View file

@ -77,15 +77,15 @@ class TypeScriptDefinitionBuilder implements Builder {
} }
} else if (isModelClass(type)) { } else if (isModelClass(type)) {
var sourcePath = buildStep.inputId.uri.toString(); 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)) { if (!p.equals(sourcePath, targetPath)) {
var relative = p.relative(targetPath, from: sourcePath); var relative = p.relative(targetPath, from: sourcePath);
String? ref; String? ref;
if (type.element.source.uri.scheme == 'asset') { if (type.element2.source.uri.scheme == 'asset') {
var id = 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) { if (id.package != buildStep.inputId.package) {
ref = '/// <reference types="${id.package}" />'; ref = '/// <reference types="${id.package}" />';
} }
@ -105,9 +105,9 @@ class TypeScriptDefinitionBuilder implements Builder {
} }
var ctx = await buildContext( var ctx = await buildContext(
type.element, type.element2,
ConstantReader( ConstantReader(
serializableTypeChecker.firstAnnotationOf(type.element)), serializableTypeChecker.firstAnnotationOf(type.element2)),
buildStep, buildStep,
buildStep.resolver, buildStep.resolver,
autoSnakeCaseNames, autoSnakeCaseNames,