dart-code-metrics icon indicating copy to clipboard operation
dart-code-metrics copied to clipboard

[Feature] Support `// ignore:` for metrics

Open vasilich6107 opened this issue 3 years ago • 7 comments

  • Dart code metrics version: 4.16.0
  • Dart sdk version: >=2.17.0 <3.0.0

Please show your full configuration:

Configuration
include: package:lint/analysis_options.yaml

analyzer:
  language:
    strict-casts: true
    strict-inference: true
    strict-raw-types: true
  exclude:
    - "lib/generated/**"
    - "lib/generated_*"
    - "lib/i18n/messages_*.dart"
    - "**/*.freezed.dart"
    - "**/*.g.dart"
    - "**/*.graphql.dart"
  plugins:
    - dart_code_metrics

dart_code_metrics:
  anti-patterns:
    - long-method
  metrics:
    cyclomatic-complexity: 20
    lines-of-executable-code: 50
    number-of-parameters: 6
    maximum-nesting-level: 5
    source-lines-of-code: 40
  metrics-exclude:
    - test/**
  rules:
    - avoid-unused-parameters
    - newline-before-return
    - no-boolean-literal-compare
    - no-equal-then-else
    - prefer-trailing-comma
    - prefer-conditional-expressions
    #      flutter
    - always-remove-listener
    - avoid-returning-widgets
    - avoid-unnecessary-setstate
    - avoid-wrapping-in-padding
    - prefer-extracting-callbacks
  rules-exclude:
    - "**/*.freezed.dart"
    - "**/*.g.dart"

linter:
  rules:
    depend_on_referenced_packages: false

What did you do? Please include the source code example causing the issue.

extension GoRouterStateExt on GoRouterState {
  // ignore: number-of-parameters
  GoRouterState copyWith(
    GoRouteInformationParser delegate, {
    String? location,
    String? subloc,
    String? name,
    String? path,
    String? fullpath,
    Map<String, String>? params,
    Map<String, String>? queryParams,
    Object? extra,
    Exception? error,
    ValueKey<String>? pageKey,
  }) =>
      GoRouterState(
        delegate,
        location: location ?? this.location,
        subloc: subloc ?? this.subloc,
        name: name ?? this.name,
        path: path ?? this.path,
        fullpath: fullpath ?? this.fullpath,
        params: params ?? this.params,
        queryParams: queryParams ?? this.queryParams,
        extra: extra ?? this.extra,
        error: error ?? this.error,
        pageKey: pageKey ?? this.pageKey,
      );
}

What did you expect to happen? Dart code metrics should ignore the issue

What actually happened? Dart code metrics does not ignore the issue image

Are you willing to submit a pull request to fix this bug? not for now

vasilich6107 avatar Jul 22 '22 16:07 vasilich6107

Checked the code and partial metric ignores are not supported at all. You can only ignore the whole file with the metrics-exclude: option. Could you add more details, why you need to ignore a specific metric in the report?

incendial avatar Sep 15 '22 11:09 incendial

Hi. Thanks for response. I do not understand your question basically) There could be a different bunch of cases when I want to ignore something) I just tried to use what specified in documentation

vasilich6107 avatar Sep 15 '22 11:09 vasilich6107

I just tried to use what specified in documentation

Could you provide a link?

There could be a different bunch of cases when I want to ignore something)

Would be great to know why metrics-exclude: doesn't work for you? Also, why not to change the configuration for metrics instead of ignoring the report? Looks like you want to ignore since you don't want to fix this particular report.

incendial avatar Sep 15 '22 11:09 incendial

https://dartcodemetrics.dev/docs/getting-started/configuration#ignoring-a-rule-or-anti-pattern

vasilich6107 avatar Sep 15 '22 11:09 vasilich6107

Would be great to know why metrics-exclude: doesn't work for you?

Cause I want to exclude only one particular place

vasilich6107 avatar Sep 15 '22 11:09 vasilich6107

https://dartcodemetrics.dev/docs/getting-started/configuration#ignoring-a-rule-or-anti-pattern

It doesn't say anything about ignoring metrics

incendial avatar Sep 15 '22 11:09 incendial

sorry) my typo

anyway I still can't ignore particular place

vasilich6107 avatar Sep 15 '22 11:09 vasilich6107

i would like to add my vote for adding this feature.

yes, i could exclude the whole file to get around a specific warning, but that is not ideal. it might be only one function in a source file that has a problem, while i would prefer that all the remaining functions continue to get checked.

the other reason why it would be preferable to have partial excludes is because excluding a whole file effectively means that i have to maintain this preference in two places. suppose i edit the offending function so that it no longer violates the code metrics rules. if there was an exclusion comment right above it, i would for sure remember to remove that comment as well. but will i remember to go look at the analysis file to remove this file from the exclude list? doubtful!

if this feature doesn't get added, how about another one: give me an exception to the cyclomatic complexity rules to exclude switch statements. it is ridiculous for the analyzer to complain about a switch statement that has, say, 50 items. there is no realistic way to reduce that, if you've got an enum that legitimately has 50 separate items.

allenbrunson avatar Oct 18 '22 14:10 allenbrunson

Sounds interested. I'll start implement this feature.

dkrutskikh avatar Oct 19 '22 04:10 dkrutskikh

hey, great! seemed like an obvious addition, to me. heh

allenbrunson avatar Oct 19 '22 13:10 allenbrunson

Any updates on this feature?

maxpelna avatar Feb 09 '23 09:02 maxpelna

Nope, not yet

incendial avatar Feb 09 '23 11:02 incendial

it will be very good.

felipecastrosales avatar Feb 21 '23 00:02 felipecastrosales

I can ignore in this way: // ignore: prefer-match-file-name, e.g.

felipecastrosales avatar Feb 21 '23 00:02 felipecastrosales

Available in Teams 1.3.0

incendial avatar Apr 06 '23 18:04 incendial