Skip to content

Commit bddcddf

Browse files
srawlinsCommit Queue
authored andcommitted
analysis_server: use new Diagnostic name over AnalysisError
Work towards #60635 Change-Id: I5cc0cc66afd3531271eb8dc337556505cb21c074 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/425962 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent 3502a35 commit bddcddf

File tree

11 files changed

+51
-55
lines changed

11 files changed

+51
-55
lines changed

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

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -878,11 +878,10 @@ class BulkFixProcessor {
878878

879879
var analysisOptions = errorsResult.session.analysisContext
880880
.getAnalysisOptionsForFile(errorsResult.file);
881-
var filteredErrors = _filterDiagnostics(
881+
return _filterDiagnostics(
882882
analysisOptions,
883883
errorsResult.errors,
884-
);
885-
return filteredErrors.any(_isFixableError);
884+
).any((d) => d.isFixable);
886885
}
887886

888887
Future<BulkFixRequestResult> _organizeDirectives(
@@ -999,22 +998,6 @@ class BulkFixProcessor {
999998
BulkFixProcessor.nonLintMultiProducerMap.containsKey(diagnosticCode);
1000999
});
10011000
}
1002-
1003-
/// Returns whether [diagnostic] is something that might be fixable.
1004-
static bool _isFixableError(Diagnostic diagnostic) {
1005-
var errorCode = diagnostic.errorCode;
1006-
1007-
// Special cases that can be bulk fixed by this class but not by
1008-
// FixProcessor.
1009-
if (errorCode == WarningCode.DUPLICATE_IMPORT ||
1010-
errorCode == HintCode.UNNECESSARY_IMPORT ||
1011-
errorCode == WarningCode.UNUSED_IMPORT ||
1012-
(DirectivesOrdering.allCodes.contains(errorCode))) {
1013-
return true;
1014-
}
1015-
1016-
return _canBulkFix(errorCode);
1017-
}
10181001
}
10191002

10201003
class BulkFixRequestResult {
@@ -1149,6 +1132,22 @@ class _PubspecDeps {
11491132
final Set<String> devPackages = <String>{};
11501133
}
11511134

1135+
extension on Diagnostic {
1136+
/// Returns whether this diagnostic is something that might be fixable.
1137+
bool get isFixable {
1138+
// Special cases that can be bulk fixed by this class but not by
1139+
// FixProcessor.
1140+
if (errorCode == WarningCode.DUPLICATE_IMPORT ||
1141+
errorCode == HintCode.UNNECESSARY_IMPORT ||
1142+
errorCode == WarningCode.UNUSED_IMPORT ||
1143+
(DirectivesOrdering.allCodes.contains(errorCode))) {
1144+
return true;
1145+
}
1146+
1147+
return BulkFixProcessor._canBulkFix(errorCode);
1148+
}
1149+
}
1150+
11521151
extension on int {
11531152
String get isAre => this == 1 ? 'is' : 'are';
11541153
}

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

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

55
import 'package:analysis_server/src/services/correction/fix.dart';
66
import 'package:analysis_server_plugin/edit/dart/correction_producer.dart';
7-
import 'package:analyzer/diagnostic/diagnostic.dart';
87
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
98
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
109
import 'package:analyzer_plugin/utilities/range_factory.dart';
@@ -24,13 +23,10 @@ class ReplaceBooleanWithBool extends ResolvedCorrectionProducer {
2423

2524
@override
2625
Future<void> compute(ChangeBuilder builder) async {
27-
var analysisError = diagnostic;
28-
if (analysisError is! Diagnostic) {
29-
return;
26+
if (diagnostic case var diagnostic?) {
27+
await builder.addDartFileEdit(file, (builder) {
28+
builder.addSimpleReplacement(range.error(diagnostic), 'bool');
29+
});
3030
}
31-
32-
await builder.addDartFileEdit(file, (builder) {
33-
builder.addSimpleReplacement(range.error(analysisError), 'bool');
34-
});
3531
}
3632
}

pkg/analysis_server/lib/src/services/correction/fix/pubspec/fix_generator.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class PubspecFixGenerator {
3131
/// The resource provider used to access the file system.
3232
final ResourceProvider resourceProvider;
3333

34-
/// The error for which fixes are being generated.
34+
/// The diagnostic for which fixes are being generated.
3535
final Diagnostic error;
3636

3737
/// The offset of the [error] for which fixes are being generated.

pkg/analysis_server/test/services/correction/organize_directives_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void main() {
1919

2020
@reflectiveTest
2121
class OrganizeDirectivesTest extends AbstractSingleUnitTest {
22-
late List<Diagnostic> testErrors;
22+
late List<Diagnostic> testDiagnostics;
2323

2424
@override
2525
void setUp() {
@@ -996,7 +996,7 @@ class NonLibraryAnnotation {
996996
var organizer = ImportOrganizer(
997997
testCode,
998998
testUnit,
999-
testErrors,
999+
testDiagnostics,
10001000
removeUnused: removeUnused,
10011001
);
10021002
var edits = organizer.organize();
@@ -1007,12 +1007,12 @@ class NonLibraryAnnotation {
10071007
Future<void> _computeUnitAndErrors(String code) async {
10081008
verifyNoTestUnitErrors = false;
10091009
await resolveTestCode(code);
1010-
testErrors = testAnalysisResult.errors;
1010+
testDiagnostics = testAnalysisResult.errors;
10111011
}
10121012

10131013
Future<void> _parseUnitAndErrors(String code) async {
10141014
verifyNoTestUnitErrors = false;
10151015
await parseTestCode(code);
1016-
testErrors = testParsedResult.errors;
1016+
testDiagnostics = testParsedResult.errors;
10171017
}
10181018
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class AddMissingEnumLikeCaseClausesTest extends FixProcessorLintTest {
2727

2828
bool Function(Diagnostic) get _filter {
2929
var hasError = false;
30-
return (error) {
31-
var errorCode = error.errorCode;
30+
return (diagnostic) {
31+
var errorCode = diagnostic.errorCode;
3232
if (!hasError && errorCode is LintCode && errorCode.name == lintCode) {
3333
hasError = true;
3434
return true;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,9 @@ class AddMissingSwitchCasesTest_SwitchStatement extends FixProcessorTest {
366366

367367
bool Function(Diagnostic) get _filter {
368368
var hasError = false;
369-
return (error) {
369+
return (diagnostic) {
370370
if (!hasError &&
371-
error.errorCode ==
371+
diagnostic.errorCode ==
372372
CompileTimeErrorCode.NON_EXHAUSTIVE_SWITCH_STATEMENT) {
373373
hasError = true;
374374
return true;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -596,8 +596,8 @@ abstract class FixProcessorTest extends BaseFixProcessorTest {
596596
}
597597

598598
/// Computes fixes and verifies that there are [expectedNumberOfFixesForKind]
599-
/// fixes for the given [diagnostic] of the appropriate kind, and that the messages
600-
/// of the fixes are equal to [matchFixMessages].
599+
/// fixes for the given [diagnostic] of the appropriate kind, and that the
600+
/// messages of the fixes are equal to [matchFixMessages].
601601
Future<void> _assertHasFixes(
602602
Diagnostic diagnostic, {
603603
required int expectedNumberOfFixesForKind,

pkg/analysis_server/test/src/services/correction/fix/pubspec/test_support.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ abstract class PubspecFixTest with ResourceProviderMixin {
2121
/// The result of parsing the [content].
2222
late YamlNode node;
2323

24-
/// The error to be fixed.
24+
/// The diagnostic to be fixed.
2525
late Diagnostic diagnostic;
2626

2727
/// Return the kind of fixes being tested by this test class.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ enum _MyEnum {A, B, C}
179179
await assertHasFix(
180180
r'''
181181
''',
182-
errorFilter: (error) {
183-
return error.errorCode == WarningCode.UNUSED_ELEMENT;
182+
errorFilter: (diagnostic) {
183+
return diagnostic.errorCode == WarningCode.UNUSED_ELEMENT;
184184
},
185185
);
186186
}

pkg/analysis_server/test/utils/test_support.dart

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -269,21 +269,19 @@ class GatheringDiagnosticListener implements DiagnosticListener {
269269
}
270270
expectedCounts[code] = count;
271271
}
272-
//
273-
// Compute the actual number of each type of error.
274-
//
275-
var errorsByCode = <DiagnosticCode, List<Diagnostic>>{};
272+
273+
// Compute the actual number of each type of diagnostic.
274+
var diagnosticsByCode = <DiagnosticCode, List<Diagnostic>>{};
276275
for (var diagnostic in _diagnostics) {
277-
errorsByCode
276+
diagnosticsByCode
278277
.putIfAbsent(diagnostic.errorCode, () => <Diagnostic>[])
279278
.add(diagnostic);
280279
}
281-
//
282-
// Compare the expected and actual number of each type of error.
283-
//
280+
281+
// Compare the expected and actual number of each type of diagnostic.
284282
expectedCounts.forEach((DiagnosticCode code, int expectedCount) {
285283
int actualCount;
286-
var list = errorsByCode.remove(code);
284+
var list = diagnosticsByCode.remove(code);
287285
if (list == null) {
288286
actualCount = 0;
289287
} else {
@@ -306,8 +304,11 @@ class GatheringDiagnosticListener implements DiagnosticListener {
306304
// Check that there are no more errors in the actual-errors map,
307305
// otherwise record message.
308306
//
309-
errorsByCode.forEach((code, actualErrors) {
310-
var actualCount = actualErrors.length;
307+
diagnosticsByCode.forEach((
308+
DiagnosticCode code,
309+
List<Diagnostic> actualDiagnostics,
310+
) {
311+
var actualCount = actualDiagnostics.length;
311312
if (buffer.length == 0) {
312313
buffer.write('Expected ');
313314
} else {
@@ -318,8 +319,8 @@ class GatheringDiagnosticListener implements DiagnosticListener {
318319
buffer.write(', found ');
319320
buffer.write(actualCount);
320321
buffer.write(' (');
321-
for (var i = 0; i < actualErrors.length; i++) {
322-
var error = actualErrors[i];
322+
for (var i = 0; i < actualDiagnostics.length; i++) {
323+
var error = actualDiagnostics[i];
323324
if (i > 0) {
324325
buffer.write(', ');
325326
}

0 commit comments

Comments
 (0)