mobx.dart icon indicating copy to clipboard operation
mobx.dart copied to clipboard

FormatterException when using generics

Open cannoneyed opened this issue 4 years ago • 1 comments

The mobx_codegen:mobx_generator library throws a FormatterException when trying to save the generated code when using the observable, computed, or action decorators on a member or method using a generic type:

Error:

[SEVERE] mobx_codegen:mobx_generator on lib/core/helpers/test_class.dart (cached):
An error `FormatterException` occurred while formatting the generated source for
  `package:test_class.dart`
which was output to
  `helpers/test_class.store_generator.g.part`.
This may indicate an issue in the generator, the input source code, or in the
source formatter.
Could not format because the source could not be parsed:

line 15, column 8 of .: Expected to find '>'.
   ╷
15 │   List<T*> get items {
   │        ^
   ╵
line 21, column 17 of .: Expected to find ')'.
   ╷
21 │   set items(List<T*> value) {
   │                 ^
   ╵
[SEVERE] Failed after 85ms

This is a simplified version of the code that causes the error in formatting the generated code:

class TestData {}

class TestClass<T extends TestData> = _TestClass<T> with _$TestClass;

abstract class _TestClass<T extends TestData> with Store {
  @observable
  List<T> items = [];
}

flutter 2.5.3 dart 2.14.4 mobx 2.0.5 mobx_codegen 2.0.4

cannoneyed avatar Oct 29 '21 17:10 cannoneyed

@cannoneyed Would you like to try the latest version? It works well in the following environment.

dart: 2.15.0 mobx: 2.0.6+1 mobx: 2.0.5+2

import 'package:mobx/mobx.dart';

part 'test.g.dart';

class TestData {}

class TestClass<T extends TestData> = _TestClass<T> with _$TestClass;

abstract class _TestClass<T extends TestData> with Store {
  @observable
  List<T> items = [];
}
dart pub run build_runner build --delete-conflicting-outputs
// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'test.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

mixin _$TestClass<T extends TestData> on _TestClass<T>, Store {
  final _$itemsAtom = Atom(name: '_TestClass.items');

  @override
  List<T> get items {
    _$itemsAtom.reportRead();
    return super.items;
  }

  @override
  set items(List<T> value) {
    _$itemsAtom.reportWrite(value, super.items, () {
      super.items = value;
    });
  }

  @override
  String toString() {
    return '''
items: ${items}
    ''';
  }
}

amondnet avatar Mar 07 '22 04:03 amondnet