Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

[Question] Is it possible to "include" a file when running checks for unused code? #694

Closed
@dustincatap

Description

@dustincatap

What do you want to discuss?
I am using injectable. This generates a file that configures the dependency registrations, for my case it is named service_locator.config.dart. I also added this to the list of files to be excluded from Dart's analyzer:

analyzer:
  exclude:
    - lib/**/*.gen.dart
    - lib/**/*.config.dart
    - lib/**/*.g.dart
    - lib/**/generated/**

Here is a sample code of how I use injectable:

@Named(as: 'loginView')
@Injectable(as: Widget)
class LoginView extends StatelessWidget { // dart-code-metrics warns this as unused class
 ...
}

And here is a sample content of service_locator.config.dart:

import 'views/login_view.dart' as _i45;

Future<GetIt> $initGetIt(GetIt get, {String? environment, EnvironmentFilter? environmentFilter}) async {
    ...
    gh.factory<Widget>(() => _i45.LoginView(), instanceName: 'loginView');
    ...
}

When resolving any registered dependencies, I do not use the class' name, but instead use the instance name:

// view_locator.dart
Widget getView(String named) => locator<Widget>(instanceName: named);

When running flutter pub run dart_code_metrics:metrics check-unused-code lib --fatal-unused, here is the output:

lib\views\login_view.dart:
    ⚠ unused class LoginView
      at lib\views\login_view.dart:9:1

The reason I believe is because service_locator.config.dart was excluded from Dart's analyzer. It works fine when I remove it from the excluded files.

Is it possible to include service_locator.config.dart when running the above command?

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions