Skip to content

Commit 1c4d7e1

Browse files
FMorschelCommit Queue
authored andcommitted
[DAS] Fixes multiple fixes for insertions in multi-file library
Merges the test in https://dart-review.googlesource.com/c/sdk/+/401840 that is related to https://dart-review.googlesource.com/c/sdk/+/401180, but in the LSP handler where it can be more easily debugged. [email protected] Fixes #59572 Change-Id: I03874be3671dfbfa65a2f0b9f3916ecafcad9400 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/401865 Auto-Submit: Felipe Morschel <[email protected]> Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Phil Quitslund <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 2d2901b commit 1c4d7e1

File tree

5 files changed

+136
-32
lines changed

5 files changed

+136
-32
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,9 @@ class BulkFixProcessor {
534534
}
535535
}
536536
if (resolvedLibrary is ResolvedLibraryResult) {
537-
await _fixErrorsInLibrary(
537+
await _fixErrorsInLibraryAt(
538538
resolvedLibrary,
539+
path: path,
539540
stopAfterFirst: stopAfterFirst,
540541
);
541542
if (isCancelled || (stopAfterFirst && changeMap.hasFixes)) {
@@ -591,12 +592,14 @@ class BulkFixProcessor {
591592

592593
/// Uses the change [builder] to create fixes for the diagnostics in the
593594
/// library associated with the analysis [libraryResult].
594-
Future<void> _fixErrorsInLibrary(
595+
Future<void> _fixErrorsInLibraryAt(
595596
ResolvedLibraryResult libraryResult, {
597+
required String path,
596598
bool stopAfterFirst = false,
597599
bool autoTriggered = false,
598600
}) async {
599-
for (var unitResult in libraryResult.units) {
601+
var unitResult = libraryResult.unitWithPath(path);
602+
if (unitResult != null) {
600603
await _fixErrorsInLibraryUnit(
601604
libraryResult,
602605
unitResult,

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ class B extends A {
3737
expect(result.edits, hasLength(1));
3838
}
3939

40-
@FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/59572')
4140
Future<void> test_bulk_fix_with_parts() async {
4241
writeFile(sourcePath(file_paths.analysisOptionsYaml), '''
4342
linter:

pkg/analysis_server/test/lsp/commands/fix_all_in_workspace_test.dart

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,62 @@ void f() {
133133
}
134134
}
135135

136+
Future<void> test_partFile_issue59572() async {
137+
newFile(analysisOptionsPath, '''
138+
linter:
139+
rules:
140+
- empty_statements
141+
- prefer_const_constructors
142+
''');
143+
144+
newFile(join(projectFolderPath, 'lib', 'part.dart'), '''
145+
part of 'main.dart';
146+
147+
class C {
148+
const C();
149+
}
150+
151+
C b() {
152+
// dart fix should only add a single const
153+
return C();
154+
}
155+
''');
156+
157+
newFile(join(projectFolderPath, 'lib', 'main.dart'), '''
158+
part 'part.dart';
159+
160+
void a() {
161+
// need to trigger a lint in main.dart for the bug to happen
162+
;
163+
b();
164+
}
165+
''');
166+
167+
await initialize();
168+
await verifyCommandEdits(Command(command: commandId, title: 'UNUSED'), '''
169+
>>>>>>>>>> lib/main.dart
170+
>>>>>>>>>> Remove empty statement: lines 5-6
171+
part 'part.dart';
172+
173+
void a() {
174+
// need to trigger a lint in main.dart for the bug to happen
175+
b();
176+
}
177+
>>>>>>>>>> lib/part.dart
178+
>>>>>>>>>> Add 'const' modifier: line 9
179+
part of 'main.dart';
180+
181+
class C {
182+
const C();
183+
}
184+
185+
C b() {
186+
// dart fix should only add a single const
187+
return const C();
188+
}
189+
''');
190+
}
191+
136192
Future<void> test_serverAdvertisesCommand() async {
137193
await initialize();
138194
expect(

pkg/analysis_server/test/src/services/correction/fix/bulk_fix_processor_test.dart

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,31 +119,32 @@ part 'a.dart';
119119
);
120120

121121
newFile('$testPackageLibPath/a.dart', '''
122-
part of 'test.dart';
122+
part 'test.dart';
123+
part 'b.dart';
123124
124-
class A { }
125+
class C{}
125126
126-
var a = new A();
127+
var c = C();
127128
''');
128129

129130
newFile('$testPackageLibPath/b.dart', '''
130-
part of 'test.dart';
131+
part of 'a.dart';
131132
132133
class B { }
133134
134135
var b = new B();
135136
''');
136137

137138
await resolveTestCode('''
138-
part 'a.dart';
139-
part 'b.dart';
139+
part of 'a.dart';
140140
141-
class C{}
142-
var c = new C();
141+
class A { }
142+
143+
var a = new A();
143144
''');
144145

145146
expect(await computeHasFixes(), isTrue);
146-
expect(processor.changeMap.libraryMap.length, 3);
147+
expect(processor.changeMap.libraryMap.length, 1);
147148
}
148149

149150
/// https://github.com/dart-lang/sdk/issues/59572
@@ -177,8 +178,10 @@ void a() {
177178
''');
178179

179180
expect(await computeHasFixes(), isTrue);
180-
expect(processor.changeMap.libraryMap.length, 2);
181-
expect(processor.fixDetails.length, 2);
181+
expect(processor.changeMap.libraryMap.length, 1);
182+
expect(processor.fixDetails.length, 1);
183+
var details = processor.fixDetails;
184+
expect(details.first.fixes, hasLength(1));
182185
}
183186

184187
Future<void> test_hasFixes_stoppedAfterFirst() async {
@@ -284,10 +287,12 @@ import 'package:b/b.dart';
284287
import 'package:c/c.dart';
285288
import 'package:d/d.dart';
286289
import 'package:test/lib.dart';
290+
287291
void f() {
288292
print(C());
289293
}
290294
''');
295+
291296
await getResolvedUnit(testFile);
292297
await assertFixPubspec(content, expected);
293298
}
@@ -385,6 +390,7 @@ import 'package:b/b.dart';
385390
import 'package:c/c.dart';
386391
import 'package:d/d.dart';
387392
import 'package:test/lib.dart';
393+
388394
void f() {
389395
print(C());
390396
}

pkg/dartdev/test/commands/fix_test.dart

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,6 @@ void main() {
1616
final bullet = '•';
1717
final nonAnsiBullet = '-';
1818

19-
/// Allow for different bullets; depending on how the test harness is run,
20-
/// subprocesses may decide to give us ansi bullets or normal bullets.
21-
/// TODO(jcollins): find a way to detect which one we should be expecting.
22-
Matcher stringContainsInOrderWithVariableBullets(List<String> substrings) {
23-
var substitutedSubstrings = substrings;
24-
if (substrings.any((s) => s.contains(bullet))) {
25-
substitutedSubstrings =
26-
substrings.map((s) => s.replaceAll(bullet, nonAnsiBullet)).toList();
27-
}
28-
return anyOf(stringContainsInOrder(substrings),
29-
stringContainsInOrder(substitutedSubstrings));
30-
}
31-
3219
void defineFix() {
3320
TestProject? p;
3421
late ProcessResult result;
@@ -404,12 +391,54 @@ linter:
404391
});
405392

406393
test(
407-
'--apply --code=(multiple) [part file]',
394+
'--apply part.dart',
408395
() async {
409396
p = project(
410397
mainSrc: '''
411398
part 'part.dart';
399+
void a() {
400+
b();
401+
}
402+
''',
403+
analysisOptions: '''
404+
linter:
405+
rules:
406+
- prefer_const_constructors
407+
''',
408+
);
409+
p!.file('lib/part.dart', '''
410+
part of 'main.dart';
411+
Stream<String> b() {
412+
return Stream.empty();
413+
}
414+
''');
415+
var result = await p!.runFix([
416+
'--apply',
417+
'--code',
418+
'empty_statements',
419+
'--code',
420+
'prefer_const_constructors',
421+
'./lib/part.dart'
422+
], workingDir: p!.dirPath);
423+
expect(result.exitCode, 0);
424+
expect(result.stderr, isEmpty);
425+
expect(
426+
result.stdout,
427+
stringContainsInOrderWithVariableBullets([
428+
'Applying fixes...',
429+
'part.dart',
430+
' prefer_const_constructors $bullet 1 fix',
431+
'1 fix made in 1 file.',
432+
]));
433+
},
434+
);
412435

436+
test(
437+
'--apply --code=(multiple) [part file]',
438+
() async {
439+
p = project(
440+
mainSrc: '''
441+
part 'part.dart';
413442
void a() {
414443
// need to trigger a lint in main.dart for the bug to happen
415444
;
@@ -425,7 +454,6 @@ linter:
425454
);
426455
p!.file('lib/part.dart', '''
427456
part of 'main.dart';
428-
429457
Stream<String> b() {
430458
// dart fix should only add a single const
431459
return Stream.empty();
@@ -439,7 +467,6 @@ Stream<String> b() {
439467
'prefer_const_constructors',
440468
'.'
441469
], workingDir: p!.dirPath);
442-
443470
expect(result.exitCode, 0);
444471
expect(result.stderr, isEmpty);
445472
expect(
@@ -453,7 +480,6 @@ Stream<String> b() {
453480
'2 fixes made in 2 files.',
454481
]));
455482
},
456-
skip: 'Failing: https://github.com/dart-lang/sdk/issues/59572',
457483
);
458484

459485
test('--apply --code=(multiple: comma-delimited)', () async {
@@ -766,3 +792,17 @@ class A {
766792
});
767793
});
768794
}
795+
796+
/// Allow for different bullets; depending on how the test harness is run,
797+
/// subprocesses may decide to give us ansi bullets or normal bullets.
798+
/// TODO(jcollins): find a way to detect which one we should be expecting.
799+
Matcher stringContainsInOrderWithVariableBullets(List<String> substrings) {
800+
var substringMatcher = stringContainsInOrder(substrings);
801+
if (substrings.any((s) => s.contains(bullet))) {
802+
var alternatives = [
803+
for (var s in substrings) s.replaceAll(bullet, nonAnsiBullet)
804+
];
805+
return anyOf(substringMatcher, stringContainsInOrder(alternatives));
806+
}
807+
return substringMatcher;
808+
}

0 commit comments

Comments
 (0)