@@ -15,11 +15,11 @@ import 'package:yaml/yaml.dart';
1515
1616/// Provide the options found in the analysis options file.
1717class AnalysisOptionsProvider {
18- /// The source factory used to resolve include declarations
19- /// in analysis options files or `null` if include is not supported .
20- final SourceFactory ? _sourceFactory;
18+ /// The source factory used to resolve include declarations in analysis
19+ /// options files.
20+ final SourceFactory _sourceFactory;
2121
22- AnalysisOptionsProvider ([ this ._sourceFactory] );
22+ AnalysisOptionsProvider (this ._sourceFactory);
2323
2424 /// Provides the analysis options that apply to [root] .
2525 ///
@@ -74,63 +74,58 @@ class AnalysisOptionsProvider {
7474 Set <Source >? handled,
7575 }) {
7676 handled ?? = {};
77+
78+ YamlMap options;
7779 try {
78- var options = getOptionsFromString (_readAnalysisOptions (source));
79- if (_sourceFactory == null ) {
80- return options;
81- }
82- var includeValue = options.valueAt (AnalysisOptionsFile .include);
83- var includes = switch (includeValue) {
84- YamlScalar (: String value) => [value],
85- YamlList () =>
86- includeValue.nodes
87- .whereType <YamlScalar >()
88- .map ((e) => e.value)
89- .whereType <String >()
90- .toList (),
91- _ => < String > [],
92- };
93- var includeOptions = includes.fold (YamlMap (), (currentOptions, path) {
94- var includeSource = _sourceFactory.resolveUri (source, path);
95- if (includeSource == null || ! handled! .add (includeSource)) {
96- // Return the existing options, unchanged.
97- return currentOptions;
98- }
99- var includedOptions = getOptionsFromSource (
100- includeSource,
101- pathContext,
102- handled: handled,
103- );
104- includedOptions = _rewriteRelativePaths (
105- includedOptions,
106- pathContext.dirname (includeSource.fullName),
107- pathContext,
108- );
109- return merge (currentOptions, includedOptions);
110- });
111- options = merge (includeOptions, options);
112- return options;
113- } on OptionsFormatException {
80+ options = getOptionsFromString (source.contents.data);
81+ } on Exception {
11482 return YamlMap ();
11583 }
84+
85+ var includeValue = options.valueAt (AnalysisOptionsFile .include);
86+ var includes = switch (includeValue) {
87+ YamlScalar (: String value) => [value],
88+ YamlList () =>
89+ includeValue.nodes
90+ .whereType <YamlScalar >()
91+ .map ((e) => e.value)
92+ .whereType <String >()
93+ .toList (),
94+ _ => < String > [],
95+ };
96+ var includeOptions = includes.fold (YamlMap (), (currentOptions, path) {
97+ var includeSource = _sourceFactory.resolveUri (source, path);
98+ if (includeSource == null || ! handled! .add (includeSource)) {
99+ // Return the existing options, unchanged.
100+ return currentOptions;
101+ }
102+ var includedOptions = getOptionsFromSource (
103+ includeSource,
104+ pathContext,
105+ handled: handled,
106+ );
107+ includedOptions = _rewriteRelativePaths (
108+ includedOptions,
109+ pathContext.dirname (includeSource.fullName),
110+ pathContext,
111+ );
112+ return merge (currentOptions, includedOptions);
113+ });
114+ options = merge (includeOptions, options);
115+ return options;
116116 }
117117
118118 /// Provide the options found in [content] .
119119 ///
120120 /// An 'include' directive, if present, will be left as-is, and the referenced
121121 /// options will NOT be merged into the result. Returns an empty options map
122122 /// if the content is null, or not a YAML map.
123- YamlMap getOptionsFromString (String ? content, {Uri ? sourceUrl}) {
124- if (content == null ) {
125- return YamlMap ();
126- }
123+ YamlMap getOptionsFromString (String content, {Uri ? sourceUrl}) {
127124 try {
128125 var doc = loadYamlNode (content, sourceUrl: sourceUrl);
129126 return doc is YamlMap ? doc : YamlMap ();
130127 } on YamlException catch (e) {
131128 throw OptionsFormatException (e.message, e.span);
132- } catch (e) {
133- throw OptionsFormatException ('Unable to parse YAML document.' );
134129 }
135130 }
136131
@@ -149,17 +144,6 @@ class AnalysisOptionsProvider {
149144 YamlMap merge (YamlMap defaults, YamlMap overrides) =>
150145 Merger ().mergeMap (defaults, overrides);
151146
152- /// Read the contents of [source] as a string.
153- /// Returns null if source is null or does not exist.
154- String ? _readAnalysisOptions (Source source) {
155- try {
156- return source.contents.data;
157- } catch (e) {
158- // Source can't be read.
159- return null ;
160- }
161- }
162-
163147 /// Walks [options] with semantic knowledge about where paths may appear in an
164148 /// analysis options file, rewriting relative paths (relative to [directory] )
165149 /// as absolute paths.
0 commit comments