diff --git a/generator/lib/src/generator.dart b/generator/lib/src/generator.dart index 45ce0d07..db693310 100644 --- a/generator/lib/src/generator.dart +++ b/generator/lib/src/generator.dart @@ -63,7 +63,7 @@ class RetrofitGenerator extends GeneratorForAnnotation { static const _onSendProgress = 'onSendProgress'; static const _onReceiveProgress = 'onReceiveProgress'; static const _path = 'path'; - static const _valueVar = 'value'; + static const _valueVar = '_value'; bool hasCustomOptions = false; /// Global options specified in the `build.yaml` @@ -415,7 +415,7 @@ class RetrofitGenerator extends GeneratorForAnnotation { final paths = _getAnnotations(m, retrofit.Path); var definePath = method.peek('path')?.stringValue; paths.forEach((k, v) { - final value = v.peek(_valueVar)?.stringValue ?? k.displayName; + final value = v.peek('value')?.stringValue ?? k.displayName; definePath = definePath?.replaceFirst( '{$value}', "\${${k.displayName}${k.type.element?.kind == ElementKind.ENUM ? _hasToJson(k.type) ? '.toJson()' : '.name' : ''}}", @@ -840,7 +840,7 @@ You should create a new class to encapsulate the response. ); } } else { - blocks.add(const Code('final value = $_resultVar.data!;')); + blocks.add(const Code('final $_valueVar = $_resultVar.data!;')); } } else { if (_isBasicType(returnType)) { @@ -868,7 +868,7 @@ You should create a new class to encapsulate the response. .assign(refer('await $_dioVar.fetch').call([options])) .statement, ) - ..add(const Code('final value = $_resultVar.data;')); + ..add(const Code('final $_valueVar = $_resultVar.data;')); } else if (_typeChecker(GeneratedMessage).isSuperTypeOf(returnType)) { blocks.add( declareFinal(_resultVar) @@ -877,7 +877,7 @@ You should create a new class to encapsulate the response. .statement, ); blocks.add(Code( - "final value = await compute(${_displayString(returnType)}.fromBuffer, $_resultVar.data!);")); + "final $_valueVar = await compute(${_displayString(returnType)}.fromBuffer, $_resultVar.data!);")); } else { final fetchType = returnType.isNullable ? 'Map?' @@ -961,12 +961,12 @@ You should create a new class to encapsulate the response. if (isWrapped) { blocks.add( Code(''' - final httpResponse = HttpResponse(value, $_resultVar); + final httpResponse = HttpResponse($_valueVar, $_resultVar); $returnAsyncWrapper httpResponse; '''), ); } else { - blocks.add(Code('$returnAsyncWrapper value;')); + blocks.add(Code('$returnAsyncWrapper $_valueVar;')); } } @@ -1420,7 +1420,7 @@ if (T != dynamic && ) { final queries = _getAnnotations(m, retrofit.Query); final queryParameters = queries.map((p, r) { - final key = r.peek(_valueVar)?.stringValue ?? p.displayName; + final key = r.peek('value')?.stringValue ?? p.displayName; final Expression value; if (_isBasicType(p.type) || p.type.isDartCoreList || @@ -1769,7 +1769,7 @@ ${bodyName.displayName} == null var anyNullable = false; final fields = _getAnnotations(m, retrofit.Field).map((p, r) { anyNullable |= p.type.nullabilitySuffix == NullabilitySuffix.question; - final fieldName = r.peek(_valueVar)?.stringValue ?? p.displayName; + final fieldName = r.peek('value')?.stringValue ?? p.displayName; final isFileField = _typeChecker(File).isAssignableFromType(p.type); if (isFileField) { log.severe( @@ -1811,7 +1811,7 @@ ${bodyName.displayName} == null parts.forEach((p, r) { final fieldName = r.peek('name')?.stringValue ?? - r.peek(_valueVar)?.stringValue ?? + r.peek('value')?.stringValue ?? p.displayName; final isFileField = _typeChecker(File).isAssignableFromType(p.type); final contentType = r.peek('contentType')?.stringValue; @@ -2061,7 +2061,7 @@ ${bodyName.displayName} == null Map _generateHeaders(MethodElement m) { final headers = _getMethodAnnotations(m, retrofit.Headers) - .map((e) => e.peek(_valueVar)) + .map((e) => e.peek('value')) .map( (value) => value?.mapValue.map( (k, v) { @@ -2090,7 +2090,7 @@ ${bodyName.displayName} == null final annotationsInParam = _getAnnotations(m, retrofit.Header); final headersInParams = annotationsInParam.map((k, v) { - final value = v.peek(_valueVar)?.stringValue ?? k.displayName; + final value = v.peek('value')?.stringValue ?? k.displayName; return MapEntry(value, refer(k.displayName)); }); headers.addAll(headersInParams); diff --git a/generator/test/src/generator_test_src.dart b/generator/test/src/generator_test_src.dart index 5c81e47a..dc444b54 100644 --- a/generator/test/src/generator_test_src.dart +++ b/generator/test/src/generator_test_src.dart @@ -125,8 +125,8 @@ abstract class TestExtrasWithMap { @GET('/list/') @Extra({'key': 'value'}) Future list( - @Extras() Map extras, - ); + @Extras() Map extras, + ); } @ShouldGenerate( @@ -140,7 +140,7 @@ abstract class TestExtrasWithMap { abstract class TestExtrasWithObject { @GET('/list/') Future list( - @Extras() User u, + @Extras() User u, ); } @@ -390,8 +390,8 @@ abstract class UploadFileInfoPartTest { @ShouldGenerate( ''' - final value = User.fromJson(_result.data!); - return value; + final _value = User.fromJson(_result.data!); + return _value; ''', contains: true, ) @@ -403,8 +403,8 @@ abstract class GenericCast { @ShouldGenerate( ''' - final value = _result.data == null ? null : User.fromJson(_result.data!); - return value; + final _value = _result.data == null ? null : User.fromJson(_result.data!); + return _value; ''', contains: true, ) @@ -416,7 +416,7 @@ abstract class NullableGenericCast { @ShouldGenerate( ''' - yield value; + yield _value; ''', contains: true, ) @@ -430,13 +430,13 @@ enum TestEnum { A, B } @ShouldGenerate( ''' - final value = TestEnum.values.firstWhere( + final _value = TestEnum.values.firstWhere( (e) => e.name == _result.data, orElse: () => throw ArgumentError( 'TestEnum does not contain value \${_result.data}', ), ); - return value; + return _value; ''', contains: true, ) @@ -473,8 +473,8 @@ enum FromJsonEnum { @ShouldGenerate( ''' - final value = FromJsonEnum.fromJson(_result.data!); - return value; + final _value = FromJsonEnum.fromJson(_result.data!); + return _value; ''', contains: true, ) @@ -571,8 +571,8 @@ Map serializeUser(User object) => object.toJson(); @ShouldGenerate( ''' - final value = _result.data!; - return value; + final _value = _result.data!; + return _value; ''', contains: true, ) @@ -584,8 +584,8 @@ abstract class GenericCastBasicType { @ShouldGenerate( ''' - final value = _result.data; - return value; + final _value = _result.data; + return _value; ''', contains: true, ) @@ -715,12 +715,12 @@ abstract class TestCustomObjectBody { @ShouldGenerate( ''' - var value = _result.data!.map((k, dynamic v) => MapEntry( + var _value = _result.data!.map((k, dynamic v) => MapEntry( k, (v as List) .map((i) => User.fromJson(i as Map)) .toList())); - return value; + return _value; ''', contains: true, ) @@ -732,12 +732,12 @@ abstract class TestMapBody { @ShouldGenerate( ''' - var value = _result.data?.map((k, dynamic v) => MapEntry( + var _value = _result.data?.map((k, dynamic v) => MapEntry( k, (v as List) .map((i) => User.fromJson(i as Map)) .toList())); - return value; + return _value; ''', contains: true, ) @@ -749,9 +749,9 @@ abstract class NullableTestMapBody { @ShouldGenerate( ''' - var value = _result.data!.map((k, dynamic v) => + var _value = _result.data!.map((k, dynamic v) => MapEntry(k, User.fromJson(v as Map))); - return value; + return _value; ''', contains: true, ) @@ -763,9 +763,9 @@ abstract class TestMapBody2 { @ShouldGenerate( ''' - var value = _result.data?.map((k, dynamic v) => + var _value = _result.data?.map((k, dynamic v) => MapEntry(k, User.fromJson(v as Map))); - return value; + return _value; ''', contains: true, ) @@ -777,8 +777,8 @@ abstract class NullableTestMapBody2 { @ShouldGenerate( ''' - final value = _result.data!.cast(); - return value; + final _value = _result.data!.cast(); + return _value; ''', contains: true, ) @@ -790,8 +790,8 @@ abstract class TestBasicListString { @ShouldGenerate( ''' - final value = _result.data?.cast(); - return value; + final _value = _result.data?.cast(); + return _value; ''', contains: true, ) @@ -803,8 +803,8 @@ abstract class NullableTestBasicListString { @ShouldGenerate( ''' - final value = _result.data!.cast(); - return value; + final _value = _result.data!.cast(); + return _value; ''', contains: true, ) @@ -816,8 +816,8 @@ abstract class TestBasicListBool { @ShouldGenerate( ''' - final value = _result.data?.cast(); - return value; + final _value = _result.data?.cast(); + return _value; ''', contains: true, ) @@ -829,8 +829,8 @@ abstract class NullableTestBasicListBool { @ShouldGenerate( ''' - final value = _result.data!.cast(); - return value; + final _value = _result.data!.cast(); + return _value; ''', contains: true, ) @@ -842,8 +842,8 @@ abstract class TestBasicListInt { @ShouldGenerate( ''' - final value = _result.data?.cast(); - return value; + final _value = _result.data?.cast(); + return _value; ''', contains: true, ) @@ -855,8 +855,8 @@ abstract class NullableTestBasicListInt { @ShouldGenerate( ''' - final value = _result.data!.cast(); - return value; + final _value = _result.data!.cast(); + return _value; ''', contains: true, ) @@ -868,8 +868,8 @@ abstract class TestBasicListDouble { @ShouldGenerate( ''' - final value = _result.data?.cast(); - return value; + final _value = _result.data?.cast(); + return _value; ''', contains: true, ) @@ -956,7 +956,7 @@ abstract class TestHttpResponseVoid { @ShouldGenerate( ''' - final httpResponse = HttpResponse(value, _result); + final httpResponse = HttpResponse(_value, _result); ''', contains: true, ) @@ -968,7 +968,7 @@ abstract class TestHttpResponseObject { @ShouldGenerate( ''' - final httpResponse = HttpResponse(value, _result); + final httpResponse = HttpResponse(_value, _result); ''', contains: true, ) @@ -1162,8 +1162,8 @@ abstract class CustomOptions { @ShouldGenerate( ''' - final value = JsonMapper.fromMap(_result.data!)!; - return value; + final _value = JsonMapper.fromMap(_result.data!)!; + return _value; ''', contains: true, ) @@ -1178,9 +1178,9 @@ abstract class JsonMapperGenericCast { @ShouldGenerate( ''' - final value = + final _value = _result.data == null ? null : JsonMapper.fromMap(_result.data!)!; - return value; + return _value; ''', contains: true, ) @@ -1195,11 +1195,11 @@ abstract class NullableJsonMapperGenericCast { @ShouldGenerate( ''' - var value = _result.data! + var _value = _result.data! .map( (dynamic i) => JsonMapper.fromMap(i as Map)!) .toList(); - return value; + return _value; ''', contains: true, ) @@ -1214,12 +1214,12 @@ abstract class JsonMapperTestListBody { @ShouldGenerate( ''' - var value = _result.data!.map((k, dynamic v) => MapEntry( + var _value = _result.data!.map((k, dynamic v) => MapEntry( k, (v as List) .map((i) => JsonMapper.fromMap(i as Map)!) .toList())); - return value; + return _value; ''', contains: true, ) @@ -1234,9 +1234,9 @@ abstract class JsonMapperTestMapBody { @ShouldGenerate( ''' - var value = _result.data!.map((k, dynamic v) => + var _value = _result.data!.map((k, dynamic v) => MapEntry(k, JsonMapper.fromMap(v as Map)!)); - return value; + return _value; ''', contains: true, ) @@ -1251,8 +1251,8 @@ abstract class JsonMapperTestMapBody2 { @ShouldGenerate( ''' - final value = User.fromMap(_result.data!); - return value; + final _value = User.fromMap(_result.data!); + return _value; ''', contains: true, ) @@ -1267,8 +1267,8 @@ abstract class MapSerializableGenericCast { @ShouldGenerate( ''' - final value = _result.data == null ? null : User.fromMap(_result.data!); - return value; + final _value = _result.data == null ? null : User.fromMap(_result.data!); + return _value; ''', contains: true, ) @@ -1283,10 +1283,10 @@ abstract class NullableMapSerializableGenericCast { @ShouldGenerate( ''' - var value = _result.data! + var _value = _result.data! .map((dynamic i) => User.fromMap(i as Map)) .toList(); - return value; + return _value; ''', contains: true, ) @@ -1301,10 +1301,10 @@ abstract class MapSerializableTestListBody { @ShouldGenerate( ''' - var value = _result.data + var _value = _result.data ?.map((dynamic i) => User.fromMap(i as Map)) .toList(); - return value; + return _value; ''', contains: true, ) @@ -1319,12 +1319,12 @@ abstract class NullableMapSerializableTestListBody { @ShouldGenerate( ''' - var value = _result.data!.map((k, dynamic v) => MapEntry( + var _value = _result.data!.map((k, dynamic v) => MapEntry( k, (v as List) .map((i) => User.fromMap(i as Map)) .toList())); - return value; + return _value; ''', contains: true, ) @@ -1339,12 +1339,12 @@ abstract class MapSerializableTestMapBody { @ShouldGenerate( ''' - var value = _result.data?.map((k, dynamic v) => MapEntry( + var _value = _result.data?.map((k, dynamic v) => MapEntry( k, (v as List) .map((i) => User.fromMap(i as Map)) .toList())); - return value; + return _value; ''', contains: true, ) @@ -1359,9 +1359,9 @@ abstract class NullableMapSerializableTestMapBody { @ShouldGenerate( ''' - var value = _result.data!.map( + var _value = _result.data!.map( (k, dynamic v) => MapEntry(k, User.fromMap(v as Map))); - return value; + return _value; ''', contains: true, ) @@ -1376,9 +1376,9 @@ abstract class MapSerializableTestMapBody2 { @ShouldGenerate( ''' - var value = _result.data?.map( + var _value = _result.data?.map( (k, dynamic v) => MapEntry(k, User.fromMap(v as Map))); - return value; + return _value; ''', contains: true, ) @@ -1393,8 +1393,8 @@ abstract class NullableMapSerializableTestMapBody2 { @ShouldGenerate( ''' - final value = await compute(deserializeUser, _result.data!); - return value; + final _value = await compute(deserializeUser, _result.data!); + return _value; ''', contains: true, ) @@ -1409,10 +1409,10 @@ abstract class ComputeGenericCast { @ShouldGenerate( ''' - final value = _result.data == null + final _value = _result.data == null ? null : await compute(deserializeUser, _result.data!); - return value; + return _value; ''', contains: true, ) @@ -1427,11 +1427,11 @@ abstract class NullableComputeGenericCast { @ShouldGenerate( ''' - var value = await compute( + var _value = await compute( deserializeUserList, _result.data!.cast>(), ); - return value; + return _value; ''', contains: true, ) @@ -1446,13 +1446,13 @@ abstract class ComputeTestListBody { @ShouldGenerate( ''' - var value = _result.data == null + var _value = _result.data == null ? null : await compute( deserializeUserList, _result.data!.cast>(), ); - return value; + return _value; ''', contains: true, ) @@ -1467,12 +1467,12 @@ abstract class NullableComputeTestListBody { @ShouldGenerate( ''' - var value = Map.fromEntries(await Future.wait(_result.data!.entries.map( + var _value = Map.fromEntries(await Future.wait(_result.data!.entries.map( (e) async => MapEntry( e.key, await compute(deserializeUserList, (e.value as List).cast>()))))); - return value; + return _value; ''', contains: true, expectedLogItems: [ @@ -1493,12 +1493,12 @@ abstract class ComputeTestMapBody { @ShouldGenerate( ''' - var value = Map.fromEntries(await Future.wait(_result.data!.entries.map( + var _value = Map.fromEntries(await Future.wait(_result.data!.entries.map( (e) async => MapEntry( e.key, await compute(deserializeUserList, (e.value as List).cast>()))))); - return value; + return _value; ''', contains: true, expectedLogItems: [ @@ -1519,10 +1519,10 @@ abstract class NullableComputeTestMapBody { @ShouldGenerate( ''' - var value = Map.fromEntries(await Future.wait(_result.data!.entries.map( + var _value = Map.fromEntries(await Future.wait(_result.data!.entries.map( (e) async => MapEntry(e.key, await compute(deserializeUser, e.value as Map))))); - return value; + return _value; ''', contains: true, expectedLogItems: [ @@ -1543,14 +1543,14 @@ abstract class ComputeTestMapBody2 { @ShouldGenerate( ''' - var value = _result.data == null + var _value = _result.data == null ? null : Map.fromEntries(await Future.wait(_result.data!.entries.map( (e) async => MapEntry( e.key, await compute( deserializeUser, e.value as Map))))); - return value; + return _value; ''', contains: true, expectedLogItems: [ @@ -1701,11 +1701,11 @@ abstract class ListBodyShouldNotBeCleanTest { @ShouldGenerate( ''' - final value = GenericUser.fromJson( + final _value = GenericUser.fromJson( _result.data!, (json) => json as dynamic, ); - return value; + return _value; ''', contains: true, ) @@ -1717,7 +1717,7 @@ abstract class DynamicInnerGenericTypeShouldBeCastedAsDynamic { @ShouldGenerate( ''' - final value = GenericUser>.fromJson( + final _value = GenericUser>.fromJson( _result.data!, (json) => json is List ? json @@ -1725,7 +1725,7 @@ abstract class DynamicInnerGenericTypeShouldBeCastedAsDynamic { .toList() : List.empty(), ); - return value; + return _value; ''', contains: true, ) @@ -1737,7 +1737,7 @@ abstract class DynamicInnerListGenericTypeShouldBeCastedRecursively { @ShouldGenerate( ''' - final value = _result.data == null + final _value = _result.data == null ? null : GenericUser>.fromJson( _result.data!, @@ -1747,7 +1747,7 @@ abstract class DynamicInnerListGenericTypeShouldBeCastedRecursively { .toList() : List.empty(), ); - return value; + return _value; ''', contains: true, ) @@ -1759,11 +1759,11 @@ abstract class NullableDynamicInnerListGenericTypeShouldBeCastedRecursively { @ShouldGenerate( ''' - final value = GenericUser.fromJson( + final _value = GenericUser.fromJson( _result.data!, (json) => User.fromJson(json as Map), ); - return value; + return _value; ''', contains: true, ) @@ -1775,14 +1775,14 @@ abstract class DynamicInnerGenericTypeShouldBeCastedAsMap { @ShouldGenerate( ''' - final value = GenericUser>.fromJson( + final _value = GenericUser>.fromJson( _result.data!, (json) => GenericUser.fromJson( json as Map, (json) => User.fromJson(json as Map), ), ); - return value; + return _value; ''', contains: true, ) @@ -1794,13 +1794,13 @@ abstract class NestGenericTypeShouldBeCastedRecursively { @ShouldGenerate( ''' - final value = _result.data == null + final _value = _result.data == null ? null : GenericUser.fromJson( _result.data!, (json) => User.fromJson(json as Map), ); - return value; + return _value; ''', contains: true, ) @@ -1812,12 +1812,12 @@ abstract class NullableDynamicInnerGenericTypeShouldBeCastedAsMap { @ShouldGenerate( ''' - final value = GenericUser.fromJson( + final _value = GenericUser.fromJson( _result.data!, (json) => json == null ? null : User.fromJson(json as Map), ); - return value; + return _value; ''', contains: true, ) @@ -1829,7 +1829,7 @@ abstract class DynamicNullableInnerGenericTypeShouldBeCastedAsMap { @ShouldGenerate( ''' - final value = _result.data == null + final _value = _result.data == null ? null : GenericUser.fromJson( _result.data!, @@ -1837,7 +1837,7 @@ abstract class DynamicNullableInnerGenericTypeShouldBeCastedAsMap { ? null : User.fromJson(json as Map), ); - return value; + return _value; ''', contains: true, ) @@ -1849,13 +1849,13 @@ abstract class NullableDynamicNullableInnerGenericTypeShouldBeCastedAsMap { @ShouldGenerate( ''' - final value = GenericUser>.fromJson( + final _value = GenericUser>.fromJson( _result.data!, (json) => json is List ? json.map((i) => i as double).toList() : List.empty(), ); - return value; + return _value; ''', contains: true, ) @@ -1867,7 +1867,7 @@ abstract class DynamicInnerListGenericPrimitiveTypeShouldBeCastedRecursively { @ShouldGenerate( ''' - final value = _result.data == null + final _value = _result.data == null ? null : GenericUser>.fromJson( _result.data!, @@ -1875,7 +1875,7 @@ abstract class DynamicInnerListGenericPrimitiveTypeShouldBeCastedRecursively { ? json.map((i) => i as double).toList() : List.empty(), ); - return value; + return _value; ''', contains: true, ) @@ -1887,9 +1887,9 @@ abstract class NullableDynamicInnerListGenericPrimitiveTypeShouldBeCastedRecursi @ShouldGenerate( ''' - final value = GenericUserWithoutGenericArgumentFactories.fromJson( + final _value = GenericUserWithoutGenericArgumentFactories.fromJson( _result.data!); - return value; + return _value; ''', contains: true, ) @@ -1901,11 +1901,11 @@ abstract class DynamicInnerGenericTypeShouldBeWithoutGenericArgumentType { @ShouldGenerate( ''' - final value = _result.data == null + final _value = _result.data == null ? null : GenericUserWithoutGenericArgumentFactories.fromJson( _result.data!); - return value; + return _value; ''', contains: true, )