Skip to content

Commit f84bba2

Browse files
pqcommit-bot@chromium.org
authored andcommitted
get libraries to fix from the context collection
Fixes: dart-lang/sdk#43694 Also implements recursive fix semantics consistent with `dart analyze`. Change-Id: I349650a0c1704c2e2be84287b46de17ecc132d56 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/166602 Commit-Queue: Phil Quitslund <pquitslund@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
1 parent 103465c commit f84bba2

File tree

5 files changed

+118
-35
lines changed

5 files changed

+118
-35
lines changed

pkg/analysis_server/lib/src/edit/edit_domain.dart

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ import 'package:analyzer/exception/exception.dart';
4242
import 'package:analyzer/file_system/file_system.dart';
4343
import 'package:analyzer/source/line_info.dart';
4444
import 'package:analyzer/src/analysis_options/analysis_options_provider.dart';
45+
import 'package:analyzer/src/dart/analysis/analysis_context_collection.dart';
4546
import 'package:analyzer/src/dart/analysis/results.dart' as engine;
4647
import 'package:analyzer/src/dart/ast/utilities.dart';
4748
import 'package:analyzer/src/dart/scanner/scanner.dart' as engine;
49+
import 'package:analyzer/src/dart/sdk/sdk.dart';
4850
import 'package:analyzer/src/error/codes.dart' as engine;
4951
import 'package:analyzer/src/exception/exception.dart';
5052
import 'package:analyzer/src/generated/engine.dart' as engine;
@@ -105,15 +107,21 @@ class EditDomainHandler extends AbstractRequestHandler {
105107
}
106108
}
107109

108-
var paths = <String>[];
109-
for (var include in params.included) {
110-
var resource = server.resourceProvider.getResource(include);
111-
resource.collectDartFilePaths(paths);
112-
}
113-
114110
var workspace = DartChangeWorkspace(server.currentSessions);
115111
var processor = BulkFixProcessor(workspace);
116-
var changeBuilder = await processor.fixErrorsInLibraries(paths);
112+
113+
String sdkPath;
114+
var sdk = server.findSdk();
115+
if (sdk is FolderBasedDartSdk) {
116+
sdkPath = sdk.directory.path;
117+
}
118+
var collection = AnalysisContextCollectionImpl(
119+
includedPaths: params.included,
120+
resourceProvider: server.resourceProvider,
121+
sdkPath: sdkPath,
122+
);
123+
var changeBuilder = await processor.fixErrors(collection.contexts);
124+
117125
var response = EditBulkFixesResult(changeBuilder.sourceChange.edits)
118126
.toResponse(request.id);
119127
server.sendResponse(response);
@@ -1315,15 +1323,3 @@ class _RefactoringManager {
13151323
/// [_RefactoringManager] throws instances of this class internally to stop
13161324
/// processing in a manager that was reset.
13171325
class _ResetError {}
1318-
1319-
extension ResourceExtension on Resource {
1320-
void collectDartFilePaths(List<String> paths) {
1321-
if (this is File && AnalysisEngine.isDartFileName(path)) {
1322-
paths.add(path);
1323-
} else if (this is Folder) {
1324-
for (var child in (this as Folder).getChildren()) {
1325-
child.collectDartFilePaths(paths);
1326-
}
1327-
}
1328-
}
1329-
}

pkg/analysis_server/lib/src/services/correction/bulk_fix_processor.dart

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@ import 'package:analysis_server/src/services/correction/dart/use_rethrow.dart';
6161
import 'package:analysis_server/src/services/correction/fix.dart';
6262
import 'package:analysis_server/src/services/correction/fix_internal.dart';
6363
import 'package:analysis_server/src/services/linter/lint_names.dart';
64+
import 'package:analyzer/dart/analysis/analysis_context.dart';
6465
import 'package:analyzer/dart/analysis/results.dart';
6566
import 'package:analyzer/error/error.dart';
6667
import 'package:analyzer/source/error_processor.dart';
6768
import 'package:analyzer/src/error/codes.dart';
69+
import 'package:analyzer/src/generated/engine.dart' show AnalysisEngine;
6870
import 'package:analyzer/src/generated/source.dart';
6971
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
7072

@@ -341,16 +343,22 @@ class BulkFixProcessor {
341343
}
342344

343345
/// Return a change builder that has been used to create fixes for the
344-
/// diagnostics in the libraries at the given [libraryPaths].
345-
Future<ChangeBuilder> fixErrorsInLibraries(List<String> libraryPaths) async {
346-
for (var path in libraryPaths) {
347-
var session = workspace.getSession(path);
348-
var kind = await session.getSourceKind(path);
349-
if (kind == SourceKind.LIBRARY) {
350-
var libraryResult = await session.getResolvedLibrary(path);
351-
await _fixErrorsInLibrary(libraryResult);
346+
/// diagnostics in the libraries in the given [contexts].
347+
Future<ChangeBuilder> fixErrors(List<AnalysisContext> contexts) async {
348+
for (var context in contexts) {
349+
for (var path in context.contextRoot.analyzedFiles()) {
350+
if (!AnalysisEngine.isDartFileName(path)) {
351+
continue;
352+
}
353+
var kind = await context.currentSession.getSourceKind(path);
354+
if (kind != SourceKind.LIBRARY) {
355+
continue;
356+
}
357+
var library = await context.currentSession.getResolvedLibrary(path);
358+
await _fixErrorsInLibrary(library);
352359
}
353360
}
361+
354362
return builder;
355363
}
356364

pkg/analysis_server/test/edit/bulk_fixes_test.dart

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'package:analysis_server/protocol/protocol_generated.dart';
66
import 'package:analysis_server/src/edit/edit_domain.dart';
7+
import 'package:analyzer/src/generated/engine.dart';
78
import 'package:analyzer_plugin/protocol/protocol_common.dart';
89
import 'package:linter/src/rules.dart';
910
import 'package:test/test.dart';
@@ -38,10 +39,77 @@ class BulkFixesTest extends AbstractAnalysisTest {
3839
super.setUp();
3940
registerLintRules();
4041
handler = EditDomainHandler(server);
42+
createProject();
43+
}
44+
45+
Future<void> test_annotateOverrides_excludedSubProject() async {
46+
// Root project.
47+
addAnalysisOptionsFile('''
48+
analyzer:
49+
exclude:
50+
- test/data/**
51+
''');
52+
53+
// Sub-project.
54+
var subprojectRoot = '$projectPath/test/data/subproject';
55+
newFile('$subprojectRoot/${AnalysisEngine.ANALYSIS_OPTIONS_YAML_FILE}',
56+
content: '''
57+
linter:
58+
rules:
59+
- annotate_overrides
60+
''');
61+
62+
newFile('$subprojectRoot/${AnalysisEngine.PUBSPEC_YAML_FILE}', content: '''
63+
name: subproject
64+
''');
65+
66+
newFile('$subprojectRoot/test.dart', content: '''
67+
class A {
68+
void f() {}
69+
}
70+
class B extends A {
71+
void f() { }
72+
}
73+
''');
74+
75+
await assertNoEdits();
76+
}
77+
78+
Future<void> test_annotateOverrides_subProject() async {
79+
var subprojectRoot = '$projectPath/test/data/subproject';
80+
newFile('$subprojectRoot/${AnalysisEngine.ANALYSIS_OPTIONS_YAML_FILE}',
81+
content: '''
82+
linter:
83+
rules:
84+
- annotate_overrides
85+
''');
86+
87+
newFile('$subprojectRoot/${AnalysisEngine.PUBSPEC_YAML_FILE}', content: '''
88+
name: subproject
89+
''');
90+
91+
testFile = '$subprojectRoot/test.dart';
92+
addTestFile('''
93+
class A {
94+
void f() {}
95+
}
96+
class B extends A {
97+
void f() { }
98+
}
99+
''');
100+
101+
await assertEditEquals('''
102+
class A {
103+
void f() {}
104+
}
105+
class B extends A {
106+
@override
107+
void f() { }
108+
}
109+
''');
41110
}
42111

43112
Future<void> test_unnecessaryNew() async {
44-
createProject();
45113
addAnalysisOptionsFile('''
46114
linter:
47115
rules:
@@ -59,7 +127,6 @@ A f() => A();
59127
}
60128

61129
Future<void> test_unnecessaryNew_ignoredInOptions() async {
62-
createProject();
63130
addAnalysisOptionsFile('''
64131
analyzer:
65132
errors:
@@ -76,7 +143,6 @@ A f() => new A();
76143
}
77144

78145
Future<void> test_unnecessaryNew_ignoredInSource() async {
79-
createProject();
80146
addAnalysisOptionsFile('''
81147
linter:
82148
rules:
@@ -91,7 +157,7 @@ A f() => new A();
91157
}
92158

93159
Future<List<SourceFileEdit>> _getBulkEdits() async {
94-
var request = EditBulkFixesParams([testFile]).toRequest('0');
160+
var request = EditBulkFixesParams([projectPath]).toRequest('0');
95161
var response = await waitResponse(request);
96162
var result = EditBulkFixesResult.fromResponse(response);
97163
return result.edits;

pkg/analysis_server/test/integration/edit/bulk_fixes_test.dart

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'package:analyzer/src/generated/engine.dart';
56
import 'package:test/test.dart';
67
import 'package:test_reflective_loader/test_reflective_loader.dart';
78

@@ -27,11 +28,23 @@ class B extends A {
2728
standardAnalysisSetup();
2829
}
2930

30-
@failingTest
3131
Future<void> test_bulk_fix_override() async {
32-
setupTarget();
32+
writeFile(sourcePath(AnalysisEngine.ANALYSIS_OPTIONS_YAML_FILE), '''
33+
linter:
34+
rules:
35+
- annotate_overrides
36+
''');
37+
writeFile(sourcePath('test.dart'), '''
38+
class A {
39+
void f() {}
40+
}
41+
class B extends A {
42+
void f() { }
43+
}
44+
''');
45+
standardAnalysisSetup();
3346

3447
var result = await sendEditBulkFixes([sourceDirectory.path]);
35-
expect(result.edits.length, 1);
48+
expect(result.edits, hasLength(1));
3649
}
3750
}

pkg/analysis_server/test/src/services/correction/fix/bulk/bulk_fix_processor.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ abstract class BulkFixProcessorTest extends AbstractSingleUnitTest {
5858
var tracker = DeclarationsTracker(MemoryByteStore(), resourceProvider);
5959
tracker.addContext(driver.analysisContext);
6060
var changeBuilder =
61-
await BulkFixProcessor(workspace).fixErrorsInLibraries([testFile]);
61+
await BulkFixProcessor(workspace).fixErrors([driver.analysisContext]);
6262
return changeBuilder.sourceChange;
6363
}
6464

0 commit comments

Comments
 (0)