-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathparse.dart
77 lines (64 loc) · 2.2 KB
/
parse.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import 'package:over_react_analyzer_plugin/src/analysis_options/plugin_analysis_options.dart';
import 'package:yaml/yaml.dart';
/// Parses the given yaml and returns over react analyzer plugin configuration options.
PluginAnalysisOptions? processAnalysisOptionsFile(String fileContents) {
// TODO catch parse errors, add test coverage
final yaml = loadYamlNode(fileContents);
if (yaml is YamlMap) {
return _parseAnalysisOptions(yaml);
}
// If the file isn't a map, return null
return null;
}
PluginAnalysisOptions? _parseAnalysisOptions(YamlMap yaml) {
final dynamic overReact = yaml['over_react'];
if (overReact is YamlMap) {
return PluginAnalysisOptions(
errors: _parseErrors(overReact),
exhaustiveDepsAdditionalHooksPattern: _parseExhaustiveDepsAdditionalHooksPattern(overReact),
);
}
// If there is no `over_react` key in the yaml file, return null.
return null;
}
RegExp? _parseExhaustiveDepsAdditionalHooksPattern(YamlMap overReact) {
final dynamic exhaustiveDeps = overReact['exhaustive_deps'];
if (exhaustiveDeps is! YamlMap) return null;
final dynamic additionalHooks = exhaustiveDeps['additional_hooks'];
if (additionalHooks is! String) return null;
try {
// This will throw if the regex is invalid.
return RegExp(additionalHooks);
} catch (_) {
return null;
}
}
Map<String, AnalysisOptionsSeverity> _parseErrors(YamlMap overReact) {
final dynamic errors = overReact['errors'];
if (errors is YamlMap) {
return _yamlMapToDartMap(errors);
}
// If there is no `errors` key under the `over_react` key, an empty error map.
return {};
}
Map<String, AnalysisOptionsSeverity> _yamlMapToDartMap(YamlMap errors) {
final map = <String, AnalysisOptionsSeverity>{};
for (final key in errors.keys) {
if (key is String) {
final dynamic value = errors[key];
if (value is String) {
final enumValue = _severityMap[value];
if (enumValue != null) {
map[key] = enumValue;
}
}
}
}
return map;
}
const _severityMap = {
'ignore': AnalysisOptionsSeverity.ignore,
'info': AnalysisOptionsSeverity.info,
'error': AnalysisOptionsSeverity.error,
'warning': AnalysisOptionsSeverity.warning,
};