Apply pedantic lints

This commit is contained in:
Tobe O 2019-07-04 14:32:34 -04:00
parent 642a7d98a8
commit 447f479747
5 changed files with 30 additions and 18 deletions

View file

@ -53,12 +53,14 @@ Expression convertObject(DartObject o) {
if (o.toBoolValue() != null) return literalBool(o.toBoolValue()); if (o.toBoolValue() != null) return literalBool(o.toBoolValue());
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());
if (o.toListValue() != null) if (o.toListValue() != null) {
return literalList(o.toListValue().map(convertObject)); return literalList(o.toListValue().map(convertObject));
}
if (o.toMapValue() != null) { if (o.toMapValue() != null) {
return literalMap(o return literalMap(o
.toMapValue() .toMapValue()
@ -80,8 +82,9 @@ String dartObjectToString(DartObject v) {
if (v.toDoubleValue() != null) return v.toDoubleValue().toString(); if (v.toDoubleValue() != null) return v.toDoubleValue().toString();
if (v.toSymbolValue() != null) return '#' + v.toSymbolValue(); if (v.toSymbolValue() != null) return '#' + v.toSymbolValue();
if (v.toTypeValue() != null) return v.toTypeValue().name; if (v.toTypeValue() != null) return v.toTypeValue().name;
if (v.toListValue() != null) if (v.toListValue() != null) {
return 'const [' + v.toListValue().map(dartObjectToString).join(', ') + ']'; return 'const [' + 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) {

View file

@ -3,7 +3,6 @@ import 'package:analyzer/dart/constant/value.dart';
import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart'; import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/src/dart/element/element.dart'; import 'package:analyzer/src/dart/element/element.dart';
import 'package:angel_model/angel_model.dart';
import 'package:angel_serialize/angel_serialize.dart'; import 'package:angel_serialize/angel_serialize.dart';
import 'package:build/build.dart'; import 'package:build/build.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';

View file

@ -6,8 +6,9 @@ class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
@override @override
Future<String> generateForAnnotatedElement( Future<String> generateForAnnotatedElement(
Element element, ConstantReader annotation, BuildStep buildStep) async { Element element, ConstantReader annotation, BuildStep buildStep) async {
if (element.kind != ElementKind.CLASS) if (element.kind != ElementKind.CLASS) {
throw 'Only classes can be annotated with a @Serializable() annotation.'; throw 'Only classes can be annotated with a @Serializable() annotation.';
}
var ctx = await buildContext(element as ClassElement, annotation, buildStep, var ctx = await buildContext(element as ClassElement, annotation, buildStep,
await buildStep.resolver, true); await buildStep.resolver, true);
@ -138,9 +139,9 @@ class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
b.defaultTo = Code(dartObjectToString(existingDefault)); b.defaultTo = Code(dartObjectToString(existingDefault));
} }
if (!isListOrMapType(field.type)) if (!isListOrMapType(field.type)) {
b.toThis = true; b.toThis = true;
else { } else {
b.type = convertTypeReference(field.type); b.type = convertTypeReference(field.type);
} }
@ -153,9 +154,10 @@ class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
if (ctx.constructorParameters.isNotEmpty) { if (ctx.constructorParameters.isNotEmpty) {
if (!shouldBeConstant(ctx) || if (!shouldBeConstant(ctx) ||
ctx.clazz.unnamedConstructor?.isConst == true) ctx.clazz.unnamedConstructor?.isConst == true) {
constructor.initializers.add(Code( constructor.initializers.add(Code(
'super(${ctx.constructorParameters.map((p) => p.name).join(',')})')); 'super(${ctx.constructorParameters.map((p) => p.name).join(',')})'));
}
} }
})); }));
} }
@ -209,16 +211,18 @@ class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
if (type.typeParameters.length == 1) { if (type.typeParameters.length == 1) {
var eq = generateEquality(type.typeArguments[0]); var eq = generateEquality(type.typeArguments[0]);
return 'ListEquality<${type.typeArguments[0].name}>($eq)'; return 'ListEquality<${type.typeArguments[0].name}>($eq)';
} else } else {
return 'ListEquality()'; return 'ListEquality()';
}
} else if (const TypeChecker.fromRuntime(Map) } else if (const TypeChecker.fromRuntime(Map)
.isAssignableFromType(type)) { .isAssignableFromType(type)) {
if (type.typeParameters.length == 2) { if (type.typeParameters.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].name}, ${type.typeArguments[1].name}>(keys: $keq, values: $veq)'; return 'MapEquality<${type.typeArguments[0].name}, ${type.typeArguments[1].name}>(keys: $keq, values: $veq)';
} else } else {
return 'MapEquality()'; return 'MapEquality()';
}
} }
return nullable ? null : 'DefaultEquality<${type.name}>()'; return nullable ? null : 'DefaultEquality<${type.name}>()';
@ -228,8 +232,9 @@ class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
} }
static String Function(String, String) generateComparator(DartType type) { static String Function(String, String) generateComparator(DartType type) {
if (type is! InterfaceType || type.name == 'dynamic') if (type is! InterfaceType || type.name == 'dynamic') {
return (a, b) => '$a == $b'; return (a, b) => '$a == $b';
}
var eq = generateEquality(type, true); var eq = generateEquality(type, true);
if (eq == null) return (a, b) => '$a == $b'; if (eq == null) return (a, b) => '$a == $b';
return (a, b) => '$eq.equals($a, $b)'; return (a, b) => '$eq.equals($a, $b)';

View file

@ -8,8 +8,9 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
@override @override
Future<String> generateForAnnotatedElement( Future<String> generateForAnnotatedElement(
Element element, ConstantReader annotation, BuildStep buildStep) async { Element element, ConstantReader annotation, BuildStep buildStep) async {
if (element.kind != ElementKind.CLASS) if (element.kind != ElementKind.CLASS) {
throw 'Only classes can be annotated with a @Serializable() annotation.'; throw 'Only classes can be annotated with a @Serializable() annotation.';
}
var ctx = await buildContext(element as ClassElement, annotation, buildStep, var ctx = await buildContext(element as ClassElement, annotation, buildStep,
await buildStep.resolver, autoSnakeCaseNames != false); await buildStep.resolver, autoSnakeCaseNames != false);
@ -147,8 +148,9 @@ class ${pascal}Decoder extends Converter<Map, ${pascal}> {
} }
// Serialize dates // Serialize dates
else if (dateTimeTypeChecker.isAssignableFromType(type)) else if (dateTimeTypeChecker.isAssignableFromType(type)) {
serializedRepresentation = 'model.${field.name}?.toIso8601String()'; serializedRepresentation = 'model.${field.name}?.toIso8601String()';
}
// Serialize model classes via `XSerializer.toMap` // Serialize model classes via `XSerializer.toMap`
else if (isModelClass(type)) { else if (isModelClass(type)) {
@ -266,10 +268,11 @@ class ${pascal}Decoder extends Converter<Map, ${pascal}> {
var name = var name =
MirrorSystem.getName(ctx.fieldInfo[field.name].deserializer); MirrorSystem.getName(ctx.fieldInfo[field.name].deserializer);
deserializedRepresentation = "$name(map['$alias'])"; deserializedRepresentation = "$name(map['$alias'])";
} else if (dateTimeTypeChecker.isAssignableFromType(type)) } else if (dateTimeTypeChecker.isAssignableFromType(type)) {
deserializedRepresentation = "map['$alias'] != null ? " deserializedRepresentation = "map['$alias'] != null ? "
"(map['$alias'] is DateTime ? (map['$alias'] as DateTime) : DateTime.parse(map['$alias'].toString()))" "(map['$alias'] is DateTime ? (map['$alias'] as DateTime) : DateTime.parse(map['$alias'].toString()))"
" : $defaultValue"; " : $defaultValue";
}
// Serialize model classes via `XSerializer.toMap` // Serialize model classes via `XSerializer.toMap`
else if (isModelClass(type)) { else if (isModelClass(type)) {

View file

@ -29,8 +29,9 @@ class TypeScriptDefinitionBuilder implements Builder {
}; };
types.forEach((t, tsType) { types.forEach((t, tsType) {
if (TypeChecker.fromRuntime(t).isAssignableFromType(type)) if (TypeChecker.fromRuntime(t).isAssignableFromType(type)) {
typeScriptType = tsType; typeScriptType = tsType;
}
}); });
if (type is InterfaceType) { if (type is InterfaceType) {
@ -67,9 +68,9 @@ class TypeScriptDefinitionBuilder implements Builder {
..writeln('}')); ..writeln('}'));
} else if (const TypeChecker.fromRuntime(List) } else if (const TypeChecker.fromRuntime(List)
.isAssignableFromType(type)) { .isAssignableFromType(type)) {
if (type.typeArguments.isEmpty) if (type.typeArguments.isEmpty) {
typeScriptType = 'any[]'; typeScriptType = 'any[]';
else { } else {
var arg = await compileToTypeScriptType( var arg = await compileToTypeScriptType(
ctx, fieldName, type.typeArguments[0], refs, ext, buildStep); ctx, fieldName, type.typeArguments[0], refs, ext, buildStep);
typeScriptType = '$arg[]'; typeScriptType = '$arg[]';
@ -139,8 +140,9 @@ class TypeScriptDefinitionBuilder implements Builder {
} }
for (var element in elements) { for (var element in elements) {
if (element.element.kind != ElementKind.CLASS) if (element.element.kind != ElementKind.CLASS) {
throw 'Only classes can be annotated with a @Serializable() annotation.'; throw 'Only classes can be annotated with a @Serializable() annotation.';
}
var annotation = element.annotation; var annotation = element.annotation;