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;
    if (!_bannedTypes.contains(element.name)) return;

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

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

  // `WebSocketChannel.connect(...)` / `WebSocket.connect(...)` 等静态调用
  context.registry.addMethodInvocation((node) {
    final target = node.realTarget;
    if (target == null) return;

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

    final uri = _libraryUri(_resolveElement(target));
    if (!_isBannedLib(uri)) return;

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