dart-code-checker / dart-code-metrics

Software analytics tool that helps developers analyse and improve software quality.
https://dcm.dev
Other
861 stars 264 forks source link

[New rule] Ordering Stateless/Statefull/State classes differently with flutter-member-ordering #858

Closed FMorschel closed 1 year ago

FMorschel commented 2 years ago

Please describe what the rule should do: Inspired by #454, I do believe that a new rule would be better, making it so that you could use a different ordering inside "Flutter classes". I'm not sure if the analyzer could identify if we extended an existing StatefulWidget child created by us, but at least it could warn when extends/implements. And this could have the defaults mentioned by zs-dima at #454. Here for easier reading: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#order-other-class-members-in-a-way-that-makes-sense

This is also something my team usually does, for example: Inside a State, we initialize static members, instance members, default overrides (initState, didChangeDependencies, dispose, build, etc) and all methods are usually private inside a state so they come after. And then, all the other classes follow the same ordering, but it's a little different, for example, all public methods stay at the end.

If your rule is inspired by other please provide link to it: https://dartcodemetrics.dev/docs/rules/common/member-ordering-extended https://dartcodemetrics.dev/docs/rules/common/member-ordering

What category of rule is this? (place an "X" next to just one item)

[ ] Warns about a potential error (problem) [ ] Suggests an alternate way of doing something (suggestion) [X] Other (please specify:) Style

Provide 2-3 code examples that this rule will warn about (it will be better if you can provide both good and bad examples):

dart_code_metrics:
  ...
  rules:
    ...
    - flutter-member-ordering:
        order:
          - constructors
          - public-fields
          - private-fields
          - public-methods
          - private-methods

IGNORED

class A {
  void publicMethod() {}
  final bool _privateField = true;
  void _privateMethod() {}
  final bool publicField = true;

  const A();
}

GOOD

class MyWidget extends StatelessWidget {
  const MyWidget({super.key});
  final bool publicField = true;

  @override
  Widget build(BuildContext context) {
    return Text(
      'Hello, World!',
      style: Theme.of(context).textTheme.headline4,
    );
  }

  void _privateMethod() {}
}

BAD

class MyWidget extends StatelessWidget {
  void _privateMethod() {} 

  // public-methods should be before private-methods // LINT
  @override
  Widget build(BuildContext context) {
    return Text(
      'Hello, World!',
      style: Theme.of(context).textTheme.headline4,
    );
  }

  // public-fields should be before public-methods // LINT
  final bool publicField = true;
  // constructors should be before public-fields // LINT
  const MyWidget({super.key});
}

Are you willing to submit a pull request to implement this rule? I don't have the knowledge.

incendial commented 1 year ago

Available in 5.0.0 🚀 with this order https://github.com/dart-code-checker/dart-code-metrics/blob/master/lib/src/analyzers/lint_analyzer/rules/rules_list/member_ordering/config_parser.dart#L16 as default for widgets