runWithReporter method

  1. @override
void runWithReporter(
  1. SaropaDiagnosticReporter reporter,
  2. SaropaContext context
)
override

Override this method to implement your lint rule.

Use context to register callbacks for AST node types:

context.addMethodInvocation((node) {
  if (condition) {
    reporter.atNode(node);
  }
});

Implementation

@override
void runWithReporter(
  SaropaDiagnosticReporter reporter,
  SaropaContext context,
) {
  context.addAnnotation((Annotation node) {
    if (!_isPackageMetaTopLevelVariable(node, 'nonVirtual')) return;
    final AstNode parent = node.parent;

    if (parent is MethodDeclaration) {
      if (parent.isStatic ||
          parent.isAbstract ||
          parent.externalKeyword != null) {
        reporter.atNode(node, code);
        return;
      }
      if (!_methodHasConcreteBody(parent)) {
        reporter.atNode(node, code);
        return;
      }
      if (parent.thisOrAncestorOfType<ExtensionDeclaration>() != null ||
          parent.thisOrAncestorOfType<ExtensionTypeDeclaration>() != null) {
        reporter.atNode(node, code);
      }
      return;
    }

    if (parent is FieldDeclaration) {
      if (parent.isStatic || parent.abstractKeyword != null) {
        reporter.atNode(node, code);
        return;
      }
      if (parent.thisOrAncestorOfType<ExtensionDeclaration>() != null ||
          parent.thisOrAncestorOfType<ExtensionTypeDeclaration>() != null) {
        reporter.atNode(node, code);
      }
      return;
    }

    reporter.atNode(node, code);
  });
}