loadScanConfig function

ScanConfig? loadScanConfig(
  1. String projectRoot, {
  2. bool includeDisabledBlock = true,
})

Attempts to load rule config from the project at projectRoot.

Returns null if no analysis_options.yaml exists or it has no diagnostics: section. Uses the same regex patterns as the native plugin's config loader.

A plugins: block the extension commented out still holds the project's rule list, so it is read by default: the language server (on by default) replaces the plugin by commenting the block out, and without this every CLI scan lost the user's rules. Pass includeDisabledBlock false to ask only about the live configuration.

Implementation

ScanConfig? loadScanConfig(
  String projectRoot, {
  bool includeDisabledBlock = true,
}) {
  final enabled = <String>{};
  final disabled = <String>{};

  // Read severity overrides first (analysis_options_custom.yaml)
  _loadSeverityOverrides(projectRoot, enabled, disabled);

  // Read diagnostics section (analysis_options.yaml)
  var configContent = _readFile(projectRoot, 'analysis_options.yaml');
  if (configContent == null) return null;
  if (includeDisabledBlock) {
    configContent = _uncommentDisabledBlock(configContent);
  }

  final sectionMatch = RegExp(
    r'^\s+diagnostics:\s*$',
    multiLine: true,
  ).firstMatch(configContent);
  if (sectionMatch == null) return null;

  final lines = configContent.afterIndex(sectionMatch.end).split('\n');
  for (final line in lines) {
    if (line.trim().isEmpty || line.trimLeft().startsWith('#')) continue;
    if (!line.startsWith('      ')) break;

    final match = RegExp(r'^\s+([\w_]+):\s*(true|false)').firstMatch(line);
    if (match == null) continue;

    final ruleName = match.group(1);
    if (ruleName == null || ruleName.isEmpty) continue;

    if (match.group(2) == 'true') {
      enabled.add(ruleName);
      disabled.remove(ruleName);
    } else {
      disabled.add(ruleName);
      enabled.remove(ruleName);
    }
  }

  if (enabled.isEmpty && disabled.isEmpty) return null;
  return ScanConfig(enabledRules: enabled, disabledRules: disabled);
}