From 46a313adf5c6ee8b8590404f5f52610f71a51441 Mon Sep 17 00:00:00 2001 From: Minsu Lee Date: Sat, 26 Oct 2024 13:27:03 +0900 Subject: [PATCH] fix: error with code generation when file has unnamed extension --- .../lib/src/template/method_override.dart | 2 +- mobx_codegen/lib/src/type_names.dart | 11 +++--- .../test/data/with_extension_input.dart | 15 ++++++++ .../test/data/with_extension_output.dart | 36 +++++++++++++++++++ mobx_codegen/test/mobx_codegen_test.dart | 5 +++ 5 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 mobx_codegen/test/data/with_extension_input.dart create mode 100644 mobx_codegen/test/data/with_extension_output.dart diff --git a/mobx_codegen/lib/src/template/method_override.dart b/mobx_codegen/lib/src/template/method_override.dart index d07e45ca7..1a31e6f2c 100644 --- a/mobx_codegen/lib/src/template/method_override.dart +++ b/mobx_codegen/lib/src/template/method_override.dart @@ -43,7 +43,7 @@ class MethodOverrideTemplate { late String name; late String returnType; - late SurroundedCommaList returnTypeArgs; + late SurroundedCommaList returnTypeArgs; late SurroundedCommaList _typeParams; late SurroundedCommaList _typeArgs; diff --git a/mobx_codegen/lib/src/type_names.dart b/mobx_codegen/lib/src/type_names.dart index 4992928c5..f52116518 100644 --- a/mobx_codegen/lib/src/type_names.dart +++ b/mobx_codegen/lib/src/type_names.dart @@ -22,14 +22,14 @@ class LibraryScopedNameFinder { final LibraryElement library; - final Map _namesByElement = {}; + final Map _namesByElement = {}; - Map get namesByElement { + Map get namesByElement { // Add all of this library's type-defining elements to the name map final libraryElements = library.topLevelElements.whereType(); for (final element in libraryElements) { - _namesByElement[element] = element.name!; + _namesByElement[element] = element.name; } // Reverse each import's export namespace so we can map elements to their @@ -58,14 +58,14 @@ class LibraryScopedNameFinder { String findReturnTypeName(FunctionTypedElement executable) => _getDartTypeName(executable.returnType); - List findReturnTypeArgumentTypeNames(ExecutableElement executable) { + List findReturnTypeArgumentTypeNames(ExecutableElement executable) { final returnType = executable.returnType; return returnType is ParameterizedType ? returnType.typeArguments.map(_getDartTypeName).toList() : []; } - String findTypeParameterBoundsTypeName(TypeParameterElement typeParameter) { + String? findTypeParameterBoundsTypeName(TypeParameterElement typeParameter) { assert(typeParameter.bound != null); return _getDartTypeName(typeParameter.bound!); } @@ -88,7 +88,6 @@ class LibraryScopedNameFinder { typeElement == null || // This is a bare type param, like "T" type is TypeParameterType) { - // ignore: deprecated_member_use_from_same_package return type.getDisplayString(); } diff --git a/mobx_codegen/test/data/with_extension_input.dart b/mobx_codegen/test/data/with_extension_input.dart new file mode 100644 index 000000000..5618a8ee5 --- /dev/null +++ b/mobx_codegen/test/data/with_extension_input.dart @@ -0,0 +1,15 @@ +library generator_sample; + +import 'package:mobx/mobx.dart'; + +part 'store_with_extension.g.dart'; + +// ignore: library_private_types_in_public_api +class Foo = _Foo with _$Foo; + +abstract class _Foo with Store { + @observable + late String name; +} + +extension on int {} \ No newline at end of file diff --git a/mobx_codegen/test/data/with_extension_output.dart b/mobx_codegen/test/data/with_extension_output.dart new file mode 100644 index 000000000..573e5ba05 --- /dev/null +++ b/mobx_codegen/test/data/with_extension_output.dart @@ -0,0 +1,36 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'store_with_extension.dart'; + +// ************************************************************************** +// StoreGenerator +// ************************************************************************** + +// ignore_for_file: non_constant_identifier_names, unnecessary_brace_in_string_interps, unnecessary_lambdas, prefer_expression_function_bodies, lines_longer_than_80_chars, avoid_as, avoid_annotating_with_dynamic, no_leading_underscores_for_local_identifiers + +mixin _$Foo on _Foo, Store { + late final _$nameAtom = Atom(name: '_Foo.name', context: context); + + @override + String get name { + _$nameAtom.reportRead(); + return super.name; + } + + bool _nameIsInitialized = false; + + @override + set name(String value) { + _$nameAtom.reportWrite(value, _nameIsInitialized ? super.name : null, () { + super.name = value; + _nameIsInitialized = true; + }); + } + + @override + String toString() { + return ''' +name: ${name} + '''; + } +} \ No newline at end of file diff --git a/mobx_codegen/test/mobx_codegen_test.dart b/mobx_codegen/test/mobx_codegen_test.dart index 87222fcfc..6e3516290 100644 --- a/mobx_codegen/test/mobx_codegen_test.dart +++ b/mobx_codegen/test/mobx_codegen_test.dart @@ -59,6 +59,11 @@ void main() { './data/valid_keep_alive_computed_output.dart'); }); + test('generates for a class with extension', () async { + await compareFiles('./data/with_extension_input.dart', + './data/with_extension_output.dart'); + }); + createTests([ const TestInfo( description: 'invalid output is handled',