Skip to content

Commit

Permalink
fix: error with code generation when file has unnamed extension
Browse files Browse the repository at this point in the history
  • Loading branch information
amondnet committed Oct 26, 2024
1 parent 0d2bcbb commit d643798
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 7 deletions.
2 changes: 1 addition & 1 deletion mobx_codegen/lib/src/template/method_override.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 5 additions & 6 deletions mobx_codegen/lib/src/type_names.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -58,14 +58,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!);
}
Expand All @@ -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();
}

Expand Down
15 changes: 15 additions & 0 deletions mobx_codegen/test/data/with_extension_input.dart
Original file line number Diff line number Diff line change
@@ -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 {}
36 changes: 36 additions & 0 deletions mobx_codegen/test/data/with_extension_output.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions mobx_codegen/test/mobx_codegen_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit d643798

Please sign in to comment.