invertase / dart_custom_lint

💡 Easily write powerful Dart & Flutter lint rules for your projects or for users of your packages.
https://pub.dev/packages/custom_lint
Apache License 2.0
288 stars 65 forks source link

Problem with report an element from another file #253

Closed ivangalkindeveloper closed 5 months ago

ivangalkindeveloper commented 5 months ago

Describe the bug Hello Thank you very much for the package, it is a very convenient tool for analysis, although it needs to be studied well.

I'm trying to make a rule to check the keyword for BLoC events and states. I thought that a class element provides a specific class declaration, but when reporting an error on a class element that may be located in another file, I get strange behavior - the error is highlighted in the analysis file of the block itself. Or am I misunderstanding something about declaration elements?

To Reproduce Rule:

class AddBlocEventSealedRule extends DartLintRule {
  const AddBlocEventSealedRule({
    required this.priority,
  }) : super(
          code: const LintCode(
            name: "add_bloc_event_sealed",
            problemMessage: "Add BLoC Event class sealed keyword",
            correctionMessage:
                "Please add 'sealed' keyword base Event class of this BLoC.",
            errorSeverity: ErrorSeverity.ERROR,
          ),
        );

  final int priority;

  @override
  void run(
    CustomLintResolver resolver,
    ErrorReporter reporter,
    CustomLintContext context,
  ) =>
      context.registry.addClassDeclaration(
        (
          ClassDeclaration node,
        ) {
          final ClassElement? blocElement = node.declaredElement;
          if (blocElement == null) {
            return;
          }

          if (blocTypeChecker.isAssignableFrom(
                blocElement,
              ) ==
              false) {
            return;
          }

          final InterfaceType? supertype = blocElement.supertype;
          if (supertype == null) {
            return;
          }

          final List<DartType> typeArguments = supertype.typeArguments;
          if (typeArguments.length != 2) {
            return;
          }

          final DartType eventType = typeArguments.first;
          if (eventType.element is! ClassElement) {
            return;
          }

          final ClassElement eventElement = eventType.element as ClassElement;
          final String blocPackagePath =
              blocElement.source.fullName.split("lib").first;
          final String eventPackagePath =
              eventElement.source.fullName.split("lib").first;
          if (blocPackagePath != eventPackagePath) {
            return;
          }
          if (eventElement.isSealed == true) {
            return;
          }

          reporter.atElement(
            eventElement,
            this.code,
          );
        },
      );
}

Example - Bloc and Event.

The eventElement.source property of the element under study shows correctly - .../pedant/example/lib/add/add_bloc_event_sealed_event.dart

Reporting problem: Снимок экрана 2024-06-09 в 02 30 45

Expected behavior Error highlighting in another file.

rrousselGit commented 5 months ago

Diagnostics can only be emitted on the file your lint is running against. There's no such thing as "analyze file A, but emit a warning on file B".

If you want to emit a warning on a file, the analyzed file should be that file.

You'll therefore need to either:

In any cases, that's expected.