run method

  1. @override
void run(
  1. CustomLintResolver resolver,
  2. ErrorReporter reporter,
  3. CustomLintContext context
)

Emits lints for a given file.

run will only be invoked with files respecting filesToAnalyze

Implementation

@override
void run(
  CustomLintResolver resolver,
  ErrorReporter reporter,
  CustomLintContext context,
) {
  final path = resolver.source.fullName.replaceAll('\\', '/');
  if (_isExempt(path)) return;

  context.registry.addInstanceCreationExpression((node) {
    final type = node.staticType;
    if (type == null) return;
    final element = type.element;
    if (element == null) return;

    final uri = element.library?.source.uri.toString() ?? '';
    if (!uri.startsWith(_dioUri)) return;

    // 允许业务自定义 Interceptor 子类(business 写 `class MyXxx extends Interceptor {}`);
    // 触发条件是直接实例化"位于 dio 包"的类,所以 `MyXxx()` 不会触发——
    // MyXxx 的 library uri 是 business 包,不是 package:dio/。
    reporter.atNode(node, _code);
  });

  // 静态调用:`Dio.something(...)` / `BaseOptions.xxx(...)` 等
  context.registry.addMethodInvocation((node) {
    final target = node.realTarget;
    if (target == null) return;

    final name = switch (target) {
      Identifier() => target.name,
      _ => null,
    };
    if (name == null) return;

    final uri = _libraryUri(_resolveElement(target));
    if (!uri.startsWith(_dioUri)) return;

    // 静态方法 / 工厂构造(如 MultipartFile.fromFileSync)
    reporter.atNode(node, _code);
  });
}