shouldAnalyze static method

bool shouldAnalyze(
  1. String filePath,
  2. String ruleName,
  3. RuleContext context
)

Returns whether filePath should be analyzed by ruleName.

Checks global analyzer: exclude:, DCQ-level exclude:, and per-rule include:/exclude: patterns.

Implementation

static bool shouldAnalyze(
  String filePath,
  String ruleName,
  RuleContext context,
) {
  final config = _forContext(context);
  if (config == null) return true;

  // Cache file-level exclude result across rules for the same file.
  if (_lastExcludeFile != filePath || _lastExcludeConfig != config) {
    _lastExcludeFile = filePath;
    _lastExcludeConfig = config;
    final relative = p.posix.relative(
      filePath.replaceAll(r'\', '/'),
      from: config.root,
    );
    _lastRelativePath = relative;
    _lastExcludeResult =
        config.analyzerExcludes.any((g) => g.matches(relative)) ||
        config.dcqExcludes.any((g) => g.matches(relative));
  }

  if (_lastExcludeResult!) return false;

  // Per-rule include/exclude (cached glob objects).
  final globs = config.ruleGlobs(ruleName);
  if (globs.include.isNotEmpty) {
    return globs.include.any((g) => g.matches(_lastRelativePath!));
  }
  if (globs.exclude.isNotEmpty) {
    if (globs.exclude.any((g) => g.matches(_lastRelativePath!))) return false;
  }

  return true;
}