Skip to content

Commit e29aa0c

Browse files
srawlinsCommit Queue
authored andcommitted
analyzer: Convert applyOptions extension method into AnalysisOptionsBuilder builder.
Previously, the fields in an AnalysisOptionsImpl were primarily arranged by an extension method, declared externally, called `applyOptions`. It seemed very odd that this wasn't just a method on AnalysisOptionsImpl; the "Impl" class is already private. But it turns out that an AnalysisOptionsImpl is _mostly_ not mutated in practice; the class wants to be immutable, with final fields. In this change, I make AnalysisOptionsImpl mostly immutable, and convert the `applyOptions` extension method, and all of its helper code, into a builder class, AnalysisOptionsBuilder. While we don't use a lot of builders in analyzer packages, this is a much more common and idiomatic pattern for setting up complicated data, multiple values, and then finalizing it all into an object that will not be changed again during it's lifetime. One big benefit of this refactoring is that most fields in AnalysisOptionsImpl are now final: * sourceLanguageConstraint * errorProcessors * excludePatterns * file * strictCasts * strictInference * strictRawTypes * chromeOsManifestChecks * codeStyleOptions * formatterOptions * unignorableNames Whereas before, they _all_ had to be mutable, as `applyOptions`, the primary mechanism for setting up an AnalysisOptionsImpl, had to able to write any field. Other changes: * Flip the instantiation of AnalysisOptionsImpl and CodeStyleOptions; now CodeStyleOptions is instatiated first, and AnalysisOptionsImpl sets itself as `codeStyleOptions.options`. * Remove the private, deprecated, `applyToAnalysisOptions` function. Change-Id: I6595d88aa5721e4f9a8b2b987482f9f43d27efd1 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/390660 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 640ad14 commit e29aa0c

File tree

18 files changed

+614
-542
lines changed

18 files changed

+614
-542
lines changed

pkg/analyzer/lib/src/analysis_options/apply_options.dart

Lines changed: 0 additions & 255 deletions
This file was deleted.

pkg/analyzer/lib/src/analysis_options/code_style_options.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import 'package:analyzer/dart/ast/ast.dart';
99
/// The concrete implementation of [CodeStyleOptions].
1010
class CodeStyleOptionsImpl implements CodeStyleOptions {
1111
/// The analysis options that owns this instance.
12-
final AnalysisOptions _options;
12+
late final AnalysisOptions options;
1313

1414
@override
1515
final bool useFormatter;
1616

17-
CodeStyleOptionsImpl(this._options, {required this.useFormatter});
17+
CodeStyleOptionsImpl({required this.useFormatter});
1818

1919
@override
2020
bool get addTrailingCommas => _isLintEnabled('require_trailing_commas');
@@ -77,7 +77,7 @@ class CodeStyleOptionsImpl implements CodeStyleOptions {
7777
}
7878

7979
/// Returns whether the lint rule with the given [name] is enabled.
80-
bool _isLintEnabled(String name) => _options.isLintEnabled(name);
80+
bool _isLintEnabled(String name) => options.isLintEnabled(name);
8181

8282
/// Returns the preferred lint quote, otherwise `null`.
8383
String? _lintQuote() => _isLintEnabled('prefer_single_quotes')

pkg/analyzer/lib/src/dart/analysis/context_builder.dart

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import 'package:analyzer/dart/analysis/declared_variables.dart';
88
import 'package:analyzer/file_system/file_system.dart';
99
import 'package:analyzer/file_system/physical_file_system.dart';
1010
import 'package:analyzer/src/analysis_options/analysis_options_provider.dart';
11-
import 'package:analyzer/src/analysis_options/apply_options.dart';
1211
import 'package:analyzer/src/context/builder.dart' show EmbedderYamlLocator;
1312
import 'package:analyzer/src/context/packages.dart';
1413
import 'package:analyzer/src/dart/analysis/analysis_options_map.dart';
@@ -119,20 +118,19 @@ class ContextBuilderImpl implements ContextBuilder {
119118
var optionsFile = contextRoot.optionsFile;
120119
var sourceFactory = workspace.createSourceFactory(sdk, summaryData);
121120

122-
var analysisOptionsMap =
123-
// If there's an options file defined (as, e.g. passed into the
124-
// AnalysisContextCollection), use a shared options map based on it.
125-
(definedOptionsFile && optionsFile != null)
126-
? AnalysisOptionsMap.forSharedOptions(_getAnalysisOptions(
127-
contextRoot,
128-
optionsFile,
129-
sourceFactory,
130-
sdk,
131-
updateAnalysisOptions2))
132-
// Else, create one from the options file mappings stored in the
133-
// context root.
134-
: _createOptionsMap(
135-
contextRoot, sourceFactory, updateAnalysisOptions2, sdk);
121+
AnalysisOptionsMap analysisOptionsMap;
122+
// If there's an options file defined (as, e.g. passed into the
123+
// AnalysisContextCollection), use a shared options map based on it.
124+
if (definedOptionsFile && optionsFile != null) {
125+
analysisOptionsMap = AnalysisOptionsMap.forSharedOptions(
126+
_getAnalysisOptions(contextRoot, optionsFile, sourceFactory, sdk,
127+
updateAnalysisOptions2));
128+
} else {
129+
// Otherwise, create one from the options file mappings stored in the
130+
// context root.
131+
analysisOptionsMap = _createOptionsMap(
132+
contextRoot, sourceFactory, updateAnalysisOptions2, sdk);
133+
}
136134

137135
var analysisContext =
138136
DriverBasedAnalysisContext(resourceProvider, contextRoot);
@@ -204,9 +202,9 @@ class ContextBuilderImpl implements ContextBuilder {
204202
(contextRoot as ContextRootImpl).optionsFileMap.entries;
205203
for (var entry in optionsMappings) {
206204
var file = entry.value;
207-
var options = AnalysisOptionsImpl(file: file);
208-
var optionsYaml = provider.getOptionsFromFile(file);
209-
options.applyOptions(optionsYaml);
205+
var options = AnalysisOptionsImpl.fromYaml(
206+
file: file, optionsMap: provider.getOptionsFromFile(file));
207+
210208
_optionsMap.add(entry.key, options);
211209
}
212210

@@ -300,17 +298,20 @@ class ContextBuilderImpl implements ContextBuilder {
300298
required DartSdk sdk})?
301299
updateAnalysisOptions,
302300
) {
303-
var options = AnalysisOptionsImpl(file: optionsFile);
301+
AnalysisOptionsImpl? options;
304302

305303
if (optionsFile != null) {
306304
try {
307305
var provider = AnalysisOptionsProvider(sourceFactory);
308-
var optionsMap = provider.getOptionsFromFile(optionsFile);
309-
options.applyOptions(optionsMap);
306+
options = AnalysisOptionsImpl.fromYaml(
307+
optionsMap: provider.getOptionsFromFile(optionsFile),
308+
file: optionsFile,
309+
);
310310
} catch (e) {
311-
// ignore
311+
// Ignore exception.
312312
}
313313
}
314+
options ??= AnalysisOptionsImpl(file: optionsFile);
314315

315316
var pubspecFile = _findPubspecFile(contextRoot);
316317
if (pubspecFile != null) {

pkg/analyzer/lib/src/dart/analysis/context_locator.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import 'package:analyzer/file_system/file_system.dart';
1010
import 'package:analyzer/file_system/physical_file_system.dart'
1111
show PhysicalResourceProvider;
1212
import 'package:analyzer/src/analysis_options/analysis_options_provider.dart';
13-
import 'package:analyzer/src/analysis_options/apply_options.dart';
1413
import 'package:analyzer/src/context/packages.dart';
1514
import 'package:analyzer/src/dart/analysis/context_root.dart';
1615
import 'package:analyzer/src/generated/engine.dart';
@@ -543,9 +542,10 @@ class ContextLocatorImpl implements ContextLocator {
543542
var provider =
544543
AnalysisOptionsProvider(workspace.createSourceFactory(null, null));
545544

546-
var options = AnalysisOptionsImpl(file: optionsFile);
547-
var optionsYaml = provider.getOptionsFromFile(optionsFile);
548-
options.applyOptions(optionsYaml);
545+
var options = AnalysisOptionsImpl.fromYaml(
546+
file: optionsFile,
547+
optionsMap: provider.getOptionsFromFile(optionsFile),
548+
);
549549

550550
return options.enabledLegacyPluginNames.toSet();
551551
} catch (_) {

0 commit comments

Comments
 (0)