Skip to content

Commit d05ef6e

Browse files
srawlinsCommit Queue
authored andcommitted
analyzer: Make all ContextRootImpl fields final
This includes the `excludedGlobs`, `optionsFile`, and `packagesFile` fields. In each case of setting the field's value after instantiation, the code happens _right_ after instantiation, so they can just be set in the constructor and be final. Change-Id: If39e8ebd8a52f4bfc81a016bff2f40d00db16a3f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/455862 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent 05bcfd2 commit d05ef6e

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

pkg/analysis_server/test/src/plugin/plugin_isolate_test.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:io' as io;
66

77
import 'package:analysis_server/src/plugin/plugin_isolate.dart';
8+
import 'package:analyzer/file_system/file_system.dart';
89
import 'package:analyzer/instrumentation/instrumentation.dart';
910
import 'package:analyzer/src/context/packages.dart';
1011
import 'package:analyzer/src/dart/analysis/context_root.dart';
@@ -52,9 +53,8 @@ class PluginIsolateTest with ResourceProviderMixin, _ContextRoot {
5253
}
5354

5455
void test_addContextRoot() {
55-
var contextRoot1 = _newContextRoot('/pkg1');
5656
var optionsFile = getFile('/pkg1/analysis_options.yaml');
57-
contextRoot1.optionsFile = optionsFile;
57+
var contextRoot1 = _newContextRoot('/pkg1', optionsFile: optionsFile);
5858
var session = PluginSession(pluginIsolate);
5959
var channel = TestServerCommunicationChannel(session);
6060
pluginIsolate.currentSession = session;
@@ -325,12 +325,13 @@ class TestServerCommunicationChannel implements ServerCommunicationChannel {
325325
}
326326

327327
mixin _ContextRoot on ResourceProviderMixin {
328-
ContextRootImpl _newContextRoot(String rootPath) {
328+
ContextRootImpl _newContextRoot(String rootPath, {File? optionsFile}) {
329329
rootPath = convertPath(rootPath);
330330
return ContextRootImpl(
331331
resourceProvider,
332332
resourceProvider.getFolder(rootPath),
333333
BasicWorkspace.find(resourceProvider, Packages.empty, rootPath),
334+
optionsFile: optionsFile,
334335
);
335336
}
336337
}

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -364,14 +364,18 @@ class ContextLocatorImpl {
364364
}) {
365365
optionsFile ??= _findDefaultOptionsFile(workspace);
366366

367-
var root = ContextRootImpl(resourceProvider, rootFolder, workspace);
368-
root.packagesFile = packagesFile;
369-
root.optionsFile = optionsFile;
367+
var root = ContextRootImpl(
368+
resourceProvider,
369+
rootFolder,
370+
workspace,
371+
optionsFile: optionsFile,
372+
packagesFile: packagesFile,
373+
);
370374
if (optionsFile != null) {
371375
root.optionsFileMap[rootFolder] = optionsFile;
372376
}
373377

374-
root.excludedGlobs = _getExcludedGlobs(optionsFile, workspace);
378+
root.excludedGlobs.addAll(_getExcludedGlobs(optionsFile, workspace));
375379
roots.add(root);
376380
return root;
377381
}
@@ -446,13 +450,11 @@ class ContextLocatorImpl {
446450
packagesFile: rootPackagesFile,
447451
buildGnFile: buildGnFile,
448452
);
449-
var root = ContextRootImpl(resourceProvider, folder, workspace);
450-
root.packagesFile = rootPackagesFile;
451453
// Check for analysis options file in the parent directories, from
452454
// root folder to the containing root folder. Pick the one closest
453455
// to the root.
454456
if (localOptionsFile == null) {
455-
var parentFolder = root.root.parent;
457+
var parentFolder = folder.parent;
456458
while (parentFolder != containingRoot.root) {
457459
localOptionsFile = parentFolder.existingAnalysisOptionsYamlFile;
458460
if (localOptionsFile != null) {
@@ -461,14 +463,20 @@ class ContextLocatorImpl {
461463
parentFolder = parentFolder.parent;
462464
}
463465
}
464-
root.optionsFile = localOptionsFile ?? containingRoot.optionsFile;
466+
var root = ContextRootImpl(
467+
resourceProvider,
468+
folder,
469+
workspace,
470+
optionsFile: localOptionsFile ?? containingRoot.optionsFile,
471+
packagesFile: rootPackagesFile,
472+
);
465473
root.included.add(folder);
466474
containingRoot.excluded.add(folder);
467475
roots.add(root);
468476
containingRoot = root;
469477
containingRootEnabledLegacyPlugins = localEnabledPlugins;
470478
excludedGlobs = _getExcludedGlobs(root.optionsFile, workspace);
471-
root.excludedGlobs = excludedGlobs;
479+
root.excludedGlobs.addAll(excludedGlobs);
472480
usedThisRoot = false;
473481
}
474482

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,25 @@ class ContextRootImpl implements ContextRoot {
2727

2828
/// A list of the globs for excluded files that were read from the analysis
2929
/// options file.
30-
List<LocatedGlob> excludedGlobs = [];
30+
final List<LocatedGlob> excludedGlobs = [];
3131

3232
@override
33-
File? optionsFile;
33+
final File? optionsFile;
3434

3535
/// Maintains a mapping of folders to associated analysis options files.
3636
final Map<Folder, File> optionsFileMap = {};
3737

3838
@override
39-
File? packagesFile;
39+
final File? packagesFile;
4040

4141
/// Initialize a newly created context root.
42-
ContextRootImpl(this.resourceProvider, this.root, this.workspace);
42+
ContextRootImpl(
43+
this.resourceProvider,
44+
this.root,
45+
this.workspace, {
46+
this.optionsFile,
47+
this.packagesFile,
48+
});
4349

4450
@override
4551
Iterable<String> get excludedPaths =>

0 commit comments

Comments
 (0)