mobxjs / mobx.dart

MobX for the Dart language. Hassle-free, reactive state-management for your Dart and Flutter apps.
https://mobx.netlify.app
MIT License
2.39k stars 311 forks source link

Let generated code also inherit meta protected #899

Closed praxamarnix closed 1 year ago

praxamarnix commented 1 year ago

I was working with mobx and figured out that @protected meta is not taken into account during the generate step. Because of that there is no proper analysis to be performed by dart analysis. It does not look deeper for @protected when the overriding class is exposing the method as public.

If the generator would also annotate the @protected function with @protected, calling the function from outside the class will nicely expose an analysis error.

Here is an example implementation:

import 'package:meta/meta.dart';
import 'package:mobx/mobx.dart';

part 'protected_bug_base.g.dart';

class Foo = _Foo with _$Foo;

abstract class _Foo with Store {
  @protected
  void doStuff() {
    print('stuff done');
  }

  @action
  @protected
  void bar() {
    print('bar done');
  }
}
// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'protected_bug_base.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 _$_FooActionController =
      ActionController(name: '_Foo', context: context);

  @override
  void bar() {
    final _$actionInfo = _$_FooActionController.startAction(name: '_Foo.bar');
    try {
      return super.bar();
    } finally {
      _$_FooActionController.endAction(_$actionInfo);
    }
  }

  @override
  String toString() {
    return '''

    ''';
  }
}

Because void bar() is missing @protected in the generated file, calling the function from outside the class does not show an analysis warning.

Would it be possible to look for protected (and perhaps others meta) and expose them in the generated code?

name: protected_bug
description: A starting point for Dart libraries or applications.
version: 1.0.0

environment:
  sdk: 2.17.6

dependencies:
  meta: 1.7.0
  mobx: 2.1.3

dev_dependencies:
  lints: 2.0.0
  test: 1.21.4
  build_runner: 2.3.0
  mobx_codegen: 2.1.1
amondnet commented 1 year ago

Hi @praxamarnix , Yes, that's a good suggestion. As a workaround, you can do the following ( although not nice )

class Foo extends _Foo with _$Foo {

  @override
  @protected
  void bar() {
    super.bar();
  }

}