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;

  final root = config.root;
  final relative = p.posix.relative(
    filePath.replaceAll(r'\', '/'),
    from: root,
  );

  // Global analyzer excludes.
  if (config.analyzerExcludes.any((g) => g.matches(relative))) return false;

  // DCQ-level excludes.
  if (config.dcqExcludes.any((g) => g.matches(relative))) return false;

  // Per-rule include/exclude.
  final ruleConfig = config.ruleConfigs[ruleName];
  if (ruleConfig != null) {
    final include = _parseGlobs(ruleConfig['include']);
    final exclude = _parseGlobs(ruleConfig['exclude']);

    if (include.isNotEmpty) {
      return include.any((g) => g.matches(relative));
    }
    if (exclude.isNotEmpty) {
      if (exclude.any((g) => g.matches(relative))) return false;
    }
  }

  return true;
}