Skip to content

Commit 958ae6a

Browse files
srawlinsCommit Queue
authored andcommitted
analyzer: Simplify AnalysisContextCollectionImpl with new updateAnalysisOptions4 parameter
The new updateAnalysisOptions4 parameter is a callback function, similar to updateAnalysisOptions3, but with one removed parameter itself, the `DartSdk sdk` parameter. The one place where this parameter was used in the callback passed by the caller, was in DAS's ContextManager, and it was just used in order to compute a FeatureSet from (a) the computed DartSdk and (b) the command-line `--enable-experiment` options. We can instead provide a `enabledExperiments` parameter on AnalysisContextCollectionImpl, leading to a simpler API and still a simple implementation in ContextBuilder. The reason for the incremental migration is that dartdoc, and possibly some internal clients, use updateAnalysisOptions3. Change-Id: Ie990ab8f7b850aed1ce3ef39ab71c01835c080da Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/447400 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 0952026 commit 958ae6a

File tree

8 files changed

+43
-40
lines changed

8 files changed

+43
-40
lines changed

pkg/analysis_server/lib/src/context_manager.dart

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import 'package:analysis_server/src/lsp/handlers/handlers.dart';
1010
import 'package:analysis_server/src/scheduler/scheduled_message.dart';
1111
import 'package:analysis_server/src/services/correction/fix/data_driven/transform_set_parser.dart';
1212
import 'package:analyzer/dart/analysis/analysis_context.dart';
13-
import 'package:analyzer/dart/analysis/features.dart';
1413
import 'package:analyzer/dart/analysis/results.dart';
1514
import 'package:analyzer/error/listener.dart';
1615
import 'package:analyzer/file_system/file_system.dart';
@@ -597,17 +596,7 @@ class ContextManagerImpl implements ContextManager {
597596
packagesFile: packagesFile,
598597
fileContentCache: _fileContentCache,
599598
unlinkedUnitStore: _unlinkedUnitStore,
600-
updateAnalysisOptions3: ({
601-
required analysisOptions,
602-
required sdk,
603-
}) {
604-
if (_enabledExperiments.isNotEmpty) {
605-
analysisOptions.contextFeatures = FeatureSet.fromEnableFlags2(
606-
sdkLanguageVersion: sdk.languageVersion,
607-
flags: _enabledExperiments,
608-
);
609-
}
610-
},
599+
enabledExperiments: _enabledExperiments,
611600
);
612601

613602
for (var analysisContext in collection.contexts) {

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,29 @@ class AnalysisContextCollectionImpl implements AnalysisContextCollection {
5656
AnalysisDriverScheduler? scheduler,
5757
FileContentCache? fileContentCache,
5858
UnlinkedUnitStore? unlinkedUnitStore,
59+
List<String> enabledExperiments = const [],
60+
@Deprecated('Use updateAnalysisOptions4 instead')
5961
void Function({
6062
required AnalysisOptionsImpl analysisOptions,
6163
required DartSdk sdk,
6264
})?
6365
updateAnalysisOptions3,
66+
void Function({required AnalysisOptionsImpl analysisOptions})?
67+
updateAnalysisOptions4,
6468
bool enableLintRuleTiming = false,
6569
}) : resourceProvider =
6670
resourceProvider ?? PhysicalResourceProvider.INSTANCE {
6771
sdkPath ??= getSdkPath();
6872

6973
performanceLog ??= PerformanceLog(null);
7074

75+
if (updateAnalysisOptions3 != null && updateAnalysisOptions4 != null) {
76+
throw ArgumentError(
77+
'Only one of updateAnalysisOptions3 and updateAnalysisOptions4 may be '
78+
'given',
79+
);
80+
}
81+
7182
if (scheduler == null) {
7283
scheduler = AnalysisDriverScheduler(performanceLog);
7384
if (drainStreams) {
@@ -102,6 +113,16 @@ class AnalysisContextCollectionImpl implements AnalysisContextCollection {
102113
resourceProvider: this.resourceProvider,
103114
);
104115

116+
// While users can use the deprecated `updateAnalysisOptions3` and the new
117+
// `updateAnalysisOptions4` parameter, prefer `updateAnalysisOptions4`, but
118+
// create a new closure with the signature of the old.
119+
var updateAnalysisOptions = updateAnalysisOptions4 != null
120+
? ({
121+
required AnalysisOptionsImpl analysisOptions,
122+
required DartSdk sdk,
123+
}) => updateAnalysisOptions4(analysisOptions: analysisOptions)
124+
: updateAnalysisOptions3;
125+
105126
for (var root in roots) {
106127
var context = contextBuilder.createContext(
107128
byteStore: byteStore,
@@ -117,12 +138,13 @@ class AnalysisContextCollectionImpl implements AnalysisContextCollection {
117138
sdkPath: sdkPath,
118139
sdkSummaryPath: sdkSummaryPath,
119140
scheduler: scheduler,
120-
updateAnalysisOptions3: updateAnalysisOptions3,
141+
updateAnalysisOptions3: updateAnalysisOptions,
121142
fileContentCache: fileContentCache,
122143
unlinkedUnitStore: unlinkedUnitStore ?? UnlinkedUnitStoreImpl(),
123144
ownedFiles: ownedFiles,
124145
enableLintRuleTiming: enableLintRuleTiming,
125146
withFineDependencies: withFineDependencies,
147+
enabledExperiments: enabledExperiments,
126148
);
127149
contexts.add(context);
128150
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'dart:async';
66

77
import 'package:analyzer/dart/analysis/context_root.dart';
88
import 'package:analyzer/dart/analysis/declared_variables.dart';
9+
import 'package:analyzer/dart/analysis/features.dart';
910
import 'package:analyzer/file_system/file_system.dart';
1011
import 'package:analyzer/file_system/physical_file_system.dart';
1112
import 'package:analyzer/src/analysis_options/analysis_options_provider.dart';
@@ -91,6 +92,7 @@ class ContextBuilderImpl {
9192
bool enableLintRuleTiming = false,
9293
LinkedBundleProvider? linkedBundleProvider,
9394
required bool withFineDependencies,
95+
List<String> enabledExperiments = const [],
9496
}) {
9597
byteStore ??= MemoryByteStore();
9698
performanceLog ??= PerformanceLog(null);
@@ -141,6 +143,7 @@ class ContextBuilderImpl {
141143
sourceFactory,
142144
sdk,
143145
updateAnalysisOptions3,
146+
enabledExperiments,
144147
),
145148
);
146149
} else {
@@ -292,6 +295,7 @@ class ContextBuilderImpl {
292295
required DartSdk sdk,
293296
})?
294297
updateAnalysisOptions,
298+
List<String> enabledExperiments,
295299
) {
296300
AnalysisOptionsImpl? options;
297301

@@ -308,6 +312,10 @@ class ContextBuilderImpl {
308312
}
309313
}
310314
options ??= AnalysisOptionsImpl(file: optionsFile);
315+
options.contextFeatures = FeatureSet.fromEnableFlags2(
316+
sdkLanguageVersion: sdk.languageVersion,
317+
flags: enabledExperiments,
318+
);
311319

312320
if (updateAnalysisOptions != null) {
313321
updateAnalysisOptions(analysisOptions: options, sdk: sdk);

pkg/analyzer/test/src/dart/analysis/analysis_context_collection_test.dart

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import 'package:analyzer/src/dart/analysis/analysis_options.dart';
99
import 'package:analyzer/src/dart/analysis/driver_based_analysis_context.dart';
1010
import 'package:analyzer/src/dart/analysis/experiments.dart';
1111
import 'package:analyzer/src/dart/analysis/file_state.dart';
12-
import 'package:analyzer/src/generated/sdk.dart';
1312
import 'package:analyzer/src/test_utilities/mock_sdk.dart';
1413
import 'package:analyzer/src/util/file_paths.dart' as file_paths;
1514
import 'package:analyzer/src/utilities/extensions/file_system.dart';
@@ -379,7 +378,7 @@ name: test
379378

380379
_assertWorkspaceCollectionText(
381380
workspaceRootPath,
382-
updateAnalysisOptions: ({required analysisOptions, required sdk}) {
381+
updateAnalysisOptions: ({required analysisOptions}) {
383382
analysisOptions.contextFeatures = FeatureSet.fromEnableFlags2(
384383
sdkLanguageVersion: ExperimentStatus.currentVersion,
385384
flags: ['digit-separators', 'variance'],
@@ -461,7 +460,7 @@ name: test
461460

462461
_assertWorkspaceCollectionText(
463462
workspaceRootPath,
464-
updateAnalysisOptions: ({required analysisOptions, required sdk}) {
463+
updateAnalysisOptions: ({required analysisOptions}) {
465464
analysisOptions.contextFeatures = FeatureSet.fromEnableFlags2(
466465
sdkLanguageVersion: ExperimentStatus.currentVersion,
467466
flags: ['variance'],
@@ -1243,10 +1242,7 @@ workspaces
12431242
String workspaceRootPath,
12441243
String expected, {
12451244
File? optionsFile,
1246-
void Function({
1247-
required AnalysisOptionsImpl analysisOptions,
1248-
required DartSdk sdk,
1249-
})?
1245+
void Function({required AnalysisOptionsImpl analysisOptions})?
12501246
updateAnalysisOptions,
12511247
}) {
12521248
if (optionsFile != null) {
@@ -1257,7 +1253,7 @@ workspaces
12571253
sdkPath: sdkRoot.path,
12581254
includedPaths: [getFolder(workspaceRootPath).path],
12591255
optionsFile: optionsFile?.path,
1260-
updateAnalysisOptions3: updateAnalysisOptions,
1256+
updateAnalysisOptions4: updateAnalysisOptions,
12611257
);
12621258

12631259
_assertCollectionText(collection, expected);

pkg/analyzer/test/src/dart/resolution/context_collection_resolution.dart

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import 'package:analyzer/src/dart/analysis/driver_based_analysis_context.dart';
1414
import 'package:analyzer/src/dart/analysis/results.dart';
1515
import 'package:analyzer/src/dart/analysis/unlinked_unit_store.dart';
1616
import 'package:analyzer/src/dart/element/element.dart';
17-
import 'package:analyzer/src/generated/sdk.dart';
1817
import 'package:analyzer/src/test_utilities/mock_sdk.dart';
1918
import 'package:analyzer/src/util/file_paths.dart' as file_paths;
2019
import 'package:analyzer/src/workspace/basic.dart';
@@ -125,7 +124,6 @@ abstract class ContextResolutionTest
125124
sdkPath: sdkRoot.path,
126125
sdkSummaryPath: sdkSummaryFile?.path,
127126
librarySummaryPaths: librarySummaryFiles?.map((e) => e.path).toList(),
128-
updateAnalysisOptions3: updateAnalysisOptions,
129127
drainStreams: false,
130128
withFineDependencies: withFineDependencies,
131129
);
@@ -255,13 +253,6 @@ abstract class ContextResolutionTest
255253
await disposeAnalysisContextCollection();
256254
}
257255

258-
/// Override this method to update [analysisOptions] for every context root,
259-
/// the default or already updated with `analysis_options.yaml` file.
260-
void updateAnalysisOptions({
261-
required AnalysisOptionsImpl analysisOptions,
262-
required DartSdk sdk,
263-
}) {}
264-
265256
/// Call this method if the test needs to use the empty byte store, without
266257
/// any information cached.
267258
void useEmptyByteStore() {

pkg/analyzer/test/util/id_testing_helper.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ Future<TestResult<T>> runTestForConfig<T>(
144144
resourceProvider: resourceProvider,
145145
retainDataForTesting: true,
146146
sdkPath: sdkRoot.path,
147-
updateAnalysisOptions3: ({required analysisOptions, required sdk}) {
147+
updateAnalysisOptions4: ({required analysisOptions}) {
148148
analysisOptions.contextFeatures = config.featureSet;
149149
},
150150
);

pkg/analyzer_cli/lib/src/driver.dart

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import 'package:analyzer/src/dart/analysis/driver_based_analysis_context.dart';
2121
import 'package:analyzer/src/dart/analysis/file_content_cache.dart';
2222
import 'package:analyzer/src/dart/analysis/file_state.dart';
2323
import 'package:analyzer/src/dart/analysis/results.dart';
24-
import 'package:analyzer/src/generated/sdk.dart';
2524
import 'package:analyzer/src/manifest/manifest_validator.dart';
2625
import 'package:analyzer/src/pubspec/pubspec_validator.dart';
2726
import 'package:analyzer/src/source/path_filter.dart';
@@ -617,7 +616,7 @@ class _AnalysisContextProvider {
617616
packagesFile: _commandLineOptions!.defaultPackagesPath,
618617
resourceProvider: _resourceProvider,
619618
sdkPath: _commandLineOptions!.dartSdkPath,
620-
updateAnalysisOptions3: _updateAnalysisOptions,
619+
updateAnalysisOptions4: _updateAnalysisOptions,
621620
fileContentCache: _fileContentCache,
622621
);
623622
_toDispose.add(_collection!);
@@ -652,10 +651,7 @@ class _AnalysisContextProvider {
652651
_analysisContext = _collection!.contextFor(path);
653652
}
654653

655-
void _updateAnalysisOptions({
656-
required AnalysisOptionsImpl analysisOptions,
657-
required DartSdk sdk,
658-
}) {
654+
void _updateAnalysisOptions({required AnalysisOptionsImpl analysisOptions}) {
659655
_commandLineOptions!.updateAnalysisOptions(analysisOptions);
660656
}
661657
}

pkg/linter/tool/test_linter.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,15 @@ class TestLinter implements DiagnosticListener {
5252
Future<List<Diagnostic>> _analyze(Iterable<io.File> files) async {
5353
AnalysisEngine.instance.instrumentationService = _StdInstrumentation();
5454

55-
var filePaths =
56-
files.map((file) => _absoluteNormalizedPath(file.path)).toList();
55+
var filePaths = files
56+
.map((file) => _absoluteNormalizedPath(file.path))
57+
.toList();
5758

5859
var contextCollection = AnalysisContextCollectionImpl(
5960
resourceProvider: _resourceProvider,
6061
sdkPath: _dartSdkPath,
6162
includedPaths: filePaths,
62-
updateAnalysisOptions3: ({required analysisOptions, required sdk}) {
63+
updateAnalysisOptions4: ({required analysisOptions}) {
6364
analysisOptions.lint = true;
6465
analysisOptions.warning = false;
6566
analysisOptions.lintRules = _rules;

0 commit comments

Comments
 (0)