loadAnalyzerExcludes function
Reads analyzer: exclude: for a scan of scanRoot (#403).
Matches dart analyze: nearest analysis_options.yaml at or above the target;
patterns are relative to its folder. Each pattern is returned as an
absolute glob (that folder + pattern) so it matches however the scan
target was given. Returns an empty list when no options file, no section
or invalid YAML is found. Does not follow include: chains.
Implementation
List<String> loadAnalyzerExcludes(String scanRoot) {
var dir = p.normalize(p.absolute(scanRoot));
while (!File(p.join(dir, 'analysis_options.yaml')).existsSync()) {
final parent = p.dirname(dir);
if (parent == dir) return const [];
dir = parent;
}
final content = _readFile(dir, 'analysis_options.yaml');
if (content == null) return const [];
try {
final doc = loadYaml(content);
final analyzer = doc is YamlMap ? doc['analyzer'] : null;
final exclude = analyzer is YamlMap ? analyzer['exclude'] : null;
if (exclude is! YamlList) return const [];
final base = dir.replaceAll('\\', '/');
final globs = <String>[];
for (final item in exclude) {
if (item is String) {
final glob = p.posix.normalize('$base/$item');
globs.add(glob);
// Like dart analyze, a folder pattern also excludes its contents.
if (!glob.endsWith('**')) globs.add('$glob/**');
}
}
return globs;
} on Object {
// Malformed YAML: rule loading has its own handling; add no excludes.
return const [];
}
}