Repository: mobxjs/mobx.dart

Issue:
[mobx_codegen] error with code generation when file has unnamed `extension`
Reproducable code
```dart
import 'package:mobx/mobx.dart';

part 'view_model.g.dart';

class ViewModel = _ViewModel with _$ViewModel;

abstract class _ViewModel with Store {
  @observable
  List<int> list = [];
}

extension on int {}

```
Error
```console
[INFO] Starting Build
[INFO] Updating asset graph completed, took 1ms
[SEVERE] mobx_codegen:mobx_generator on lib/view_model.dart:

Null check operator used on a null value
package:mobx_codegen/src/type_names.dart 32:46            LibraryScopedNameFinder.namesByElement
package:mobx_codegen/src/type_names.dart 137:12           LibraryScopedNameFinder._getNamedElementTypeName
package:mobx_codegen/src/type_names.dart 94:12            LibraryScopedNameFinder._getDartTypeName
dart:_internal                                            ListIterable.toList
package:mobx_codegen/src/type_names.dart 133:56           LibraryScopedNameFinder._getNamedElementTypeName
package:mobx_codegen/src/type_names.dart 94:12            LibraryScopedNameFinder._getDartTypeName
package:mobx_codegen/src/type_names.dart 48:7             LibraryScopedNameFinder.findVariableTypeName
package:mobx_codegen/src/store_class_visitor.dart 100:28  StoreClassVisitor.visitFieldElement
package:analyzer/src/dart/element/element.dart 3110:54    FieldElementImpl.accept
package:analyzer/src/dart/element/element.dart 2491:13    ElementImpl.visitChildren
package:mobx_codegen/src/mobx_codegen_base.dart 85:9      StoreGenerator._generateCodeFromTemplate
package:mobx_codegen/src/mobx_codegen_base.dart 67:13     StoreGenerator._generateCodeForMixinStore
dart:core                                                 Iterable.toSet
package:mobx_codegen/src/mobx_codegen_base.dart 26:68     StoreGenerator.generate
package:source_gen/src/builder.dart 342:33                _generate
dart:async                                                Stream.toList.<fn>
package:source_gen/src/builder.dart 107:9                 _Builder._generateForLibrary
package:source_gen/src/builder.dart 99:5                  _Builder.build

[INFO] Running build completed, took 47ms
[INFO] Caching finalized dependency graph completed, took 23ms
[SEVERE] Failed after 74ms
```
Packages
```yaml
build_runner: ^2.4.11
mobx_codegen: ^2.6.1
flutter_mobx: ^2.2.1+1
mobx: ^2.3.3+2
```
Flutter version:
`3.22.2`
if I add name to extension, it will work fine
```dart
extension _intX on int {}
```


Discussion: Omg. This is such a life saver. I tried all my luck but couldn't get through the build runner. Thanks
This is also a problem with `extension type` since it does expect `on` and a name.



Fix patch:
diff --git a/mobx_codegen/CHANGELOG.md b/mobx_codegen/CHANGELOG.md
index eb21dd37..ba150a56 100644
--- a/mobx_codegen/CHANGELOG.md
+++ b/mobx_codegen/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 2.6.2
+
+- Fixes [error with code generation when file has unnamed extension](https://github.com/mobxjs/mobx.dart/issues/1010) by [@amondnet](https://github.com/amondnet) in [#1020](https://github.com/mobxjs/mobx.dart/pull/1020)
+
 ## 2.6.1+1
 
 - Fixing some analyzer errors related to deprecated types.
diff --git a/mobx_codegen/lib/src/template/method_override.dart b/mobx_codegen/lib/src/template/method_override.dart
index d07e45ca..1a31e6f2 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<String> returnTypeArgs;
+  late SurroundedCommaList<String?> returnTypeArgs;
 
   late SurroundedCommaList<TypeParamTemplate> _typeParams;
   late SurroundedCommaList<String> _typeArgs;
diff --git a/mobx_codegen/lib/src/type_names.dart b/mobx_codegen/lib/src/type_names.dart
index 179c71af..a83f1bab 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<Element, String> _namesByElement = {};
+  final Map<Element, String?> _namesByElement = {};
 
-  Map<Element, String> get namesByElement {
+  Map<Element, String?> get namesByElement {
     // Add all of this library's type-defining elements to the name map
     final libraryElements =
         library.topLevelElements.whereType<TypeDefiningElement>();
     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
@@ -59,14 +59,14 @@ class LibraryScopedNameFinder {
   String findReturnTypeName(FunctionTypedElement executable) =>
       _getDartTypeName(executable.returnType);
 
-  List<String> findReturnTypeArgumentTypeNames(ExecutableElement executable) {
+  List<String?> 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!);
   }
diff --git a/mobx_codegen/lib/version.dart b/mobx_codegen/lib/version.dart
index 9fa5fbca..99454540 100644
--- a/mobx_codegen/lib/version.dart
+++ b/mobx_codegen/lib/version.dart
@@ -1,4 +1,4 @@
 // Generated via set_version.dart. !!!DO NOT MODIFY BY HAND!!!
 
 /// The current version as per `pubspec.yaml`.
-const version = '2.6.1+1';
+const version = '2.6.2';
diff --git a/mobx_codegen/pubspec.yaml b/mobx_codegen/pubspec.yaml
index 22ced730..c673423b 100644
--- a/mobx_codegen/pubspec.yaml
+++ b/mobx_codegen/pubspec.yaml
@@ -1,6 +1,6 @@
 name: mobx_codegen
 description: Code generator for MobX that adds support for annotating your code with @observable, @computed, @action and also creating Store classes.
-version: 2.6.1+1
+version: 2.6.2
 
 repository: https://github.com/mobxjs/mobx.dart
 issue_tracker: https://github.com/mobxjs/mobx.dart/issues


Test patch:
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 00000000..12dcf1e3
--- /dev/null
+++ b/mobx_codegen/test/data/with_extension_input.dart
@@ -0,0 +1,15 @@
+library generator_sample;
+
+import 'package:mobx/mobx.dart';
+
+part 'generator_sample.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 00000000..4c6337fb
--- /dev/null
+++ b/mobx_codegen/test/data/with_extension_output.dart
@@ -0,0 +1,36 @@
+// GENERATED CODE - DO NOT MODIFY BY HAND
+
+part of 'generator_sample.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 87222fcf..6e351629 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',