Skip to content

Commit 126def2

Browse files
srawlinsCommit Queue
authored andcommitted
analyzer_plugin: Use Diagnostic instead of deprecated AnalysisError
Work towards #60635 Change-Id: Icf62987d6de7cafb6f6e92076a5fcc2bd48772c5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/426522 Auto-Submit: Samuel Rawlins <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent d07c0cd commit 126def2

File tree

10 files changed

+73
-68
lines changed

10 files changed

+73
-68
lines changed

pkg/analyzer_plugin/lib/plugin/fix_mixin.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/dart/analysis/results.dart';
6-
import 'package:analyzer/error/error.dart';
6+
import 'package:analyzer/diagnostic/diagnostic.dart';
77
import 'package:analyzer/src/dart/analysis/driver.dart';
88
import 'package:analyzer_plugin/plugin/plugin.dart';
99
import 'package:analyzer_plugin/protocol/protocol.dart';
@@ -25,13 +25,13 @@ mixin DartFixesMixin implements FixesMixin {
2525
var offset = parameters.offset;
2626
var result = await getResolvedUnitResult(path);
2727
return DartFixesRequestImpl(
28-
resourceProvider, offset, _getErrors(offset, result), result);
28+
resourceProvider, offset, _getDiagnostics(offset, result), result);
2929
}
3030

31-
List<AnalysisError> _getErrors(int offset, ResolvedUnitResult result) {
31+
List<Diagnostic> _getDiagnostics(int offset, ResolvedUnitResult result) {
3232
var lineInfo = result.lineInfo;
3333
var offsetLine = lineInfo.getLocation(offset).lineNumber;
34-
return result.errors.where((AnalysisError error) {
34+
return result.errors.where((error) {
3535
var errorLine = lineInfo.getLocation(error.offset).lineNumber;
3636
return errorLine == offsetLine;
3737
}).toList();

pkg/analyzer_plugin/lib/src/utilities/fixes/fixes.dart

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/dart/analysis/results.dart';
6-
import 'package:analyzer/error/error.dart';
6+
import 'package:analyzer/diagnostic/diagnostic.dart';
77
import 'package:analyzer/file_system/file_system.dart';
88
import 'package:analyzer_plugin/protocol/protocol_generated.dart';
99
import 'package:analyzer_plugin/utilities/analyzer_converter.dart';
@@ -18,7 +18,7 @@ class DartFixesRequestImpl implements DartFixesRequest {
1818
final int offset;
1919

2020
@override
21-
final List<AnalysisError> errorsToFix;
21+
final List<Diagnostic> errorsToFix;
2222

2323
@override
2424
final ResolvedUnitResult result;
@@ -31,8 +31,8 @@ class DartFixesRequestImpl implements DartFixesRequest {
3131
/// A concrete implementation of [FixCollector].
3232
class FixCollectorImpl implements FixCollector {
3333
/// The list of fixes that have been collected.
34-
final Map<AnalysisError, List<PrioritizedSourceChange>> fixMap =
35-
<AnalysisError, List<PrioritizedSourceChange>>{};
34+
final Map<Diagnostic, List<PrioritizedSourceChange>> fixMap =
35+
<Diagnostic, List<PrioritizedSourceChange>>{};
3636

3737
/// Return the fixes that have been collected up to this point.
3838
List<AnalysisErrorFixes> get fixes {
@@ -46,7 +46,9 @@ class FixCollectorImpl implements FixCollector {
4646
}
4747

4848
@override
49-
void addFix(AnalysisError error, PrioritizedSourceChange change) {
50-
fixMap.putIfAbsent(error, () => <PrioritizedSourceChange>[]).add(change);
49+
void addFix(Diagnostic diagnostic, PrioritizedSourceChange change) {
50+
fixMap
51+
.putIfAbsent(diagnostic, () => <PrioritizedSourceChange>[])
52+
.add(change);
5153
}
5254
}

pkg/analyzer_plugin/lib/utilities/analyzer_converter.dart

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ import 'package:path/path.dart' as path;
1919
///
2020
/// Clients may not extend, implement or mix-in this class.
2121
class AnalyzerConverter {
22-
/// Converts the analysis [error] from the 'analyzer' package to an analysis
23-
/// error defined by the plugin API.
22+
/// Converts the analysis [diagnostic] from the 'analyzer' package to an
23+
/// analysis error defined by the plugin API.
2424
///
2525
/// If a [lineInfo] is provided then the error's location will have a start
2626
/// line and start column. If a [severity] is provided, then it will override
2727
/// the severity defined by the error.
2828
plugin.AnalysisError convertAnalysisError(
29-
analyzer.AnalysisError error, {
29+
analyzer.Diagnostic diagnostic, {
3030
analyzer.LineInfo? lineInfo,
3131
analyzer.DiagnosticSeverity? severity,
3232
}) {
33-
var errorCode = error.errorCode;
33+
var errorCode = diagnostic.errorCode;
3434
severity ??= errorCode.errorSeverity;
35-
var offset = error.offset;
35+
var offset = diagnostic.offset;
3636
var startLine = -1;
3737
var startColumn = -1;
3838
var endLine = -1;
@@ -41,36 +41,36 @@ class AnalyzerConverter {
4141
var startLocation = lineInfo.getLocation(offset);
4242
startLine = startLocation.lineNumber;
4343
startColumn = startLocation.columnNumber;
44-
var endLocation = lineInfo.getLocation(offset + error.length);
44+
var endLocation = lineInfo.getLocation(offset + diagnostic.length);
4545
endLine = endLocation.lineNumber;
4646
endColumn = endLocation.columnNumber;
4747
}
4848
List<plugin.DiagnosticMessage>? contextMessages;
49-
if (error.contextMessages.isNotEmpty) {
50-
contextMessages = error.contextMessages
49+
if (diagnostic.contextMessages.isNotEmpty) {
50+
contextMessages = diagnostic.contextMessages
5151
.map((message) =>
5252
convertDiagnosticMessage(message, lineInfo: lineInfo))
5353
.toList();
5454
}
5555
return plugin.AnalysisError(
5656
convertErrorSeverity(severity),
5757
convertErrorType(errorCode.type),
58-
plugin.Location(
59-
error.source.fullName, offset, error.length, startLine, startColumn,
58+
plugin.Location(diagnostic.source.fullName, offset, diagnostic.length,
59+
startLine, startColumn,
6060
endLine: endLine, endColumn: endColumn),
61-
error.message,
61+
diagnostic.message,
6262
errorCode.name.toLowerCase(),
6363
contextMessages: contextMessages,
64-
correction: error.correctionMessage,
64+
correction: diagnostic.correctionMessage,
6565
hasFix: true);
6666
}
6767

68-
/// Converts the list of analysis [errors] from the 'analyzer' package to a
69-
/// list of analysis errors defined by the plugin API.
68+
/// Converts the list of analysis [diagnostics] from the 'analyzer' package to
69+
/// a list of analysis errors defined by the plugin API.
7070
///
7171
/// The severities of the errors are altered based on [options].
7272
List<plugin.AnalysisError> convertAnalysisErrors(
73-
List<analyzer.AnalysisError> errors, {
73+
List<analyzer.Diagnostic> diagnostics, {
7474
// TODO(srawlins): Make `lineInfo` required and non-nullable, in a breaking
7575
// change release.
7676
analyzer.LineInfo? lineInfo,
@@ -79,18 +79,18 @@ class AnalyzerConverter {
7979
analyzer.AnalysisOptions? options,
8080
}) {
8181
var serverErrors = <plugin.AnalysisError>[];
82-
for (var error in errors) {
83-
var processor = analyzer.ErrorProcessor.getProcessor(options, error);
82+
for (var diagnostic in diagnostics) {
83+
var processor = analyzer.ErrorProcessor.getProcessor(options, diagnostic);
8484
if (processor != null) {
8585
var severity = processor.severity;
8686
// Errors with null severity are filtered out.
8787
if (severity != null) {
8888
// Specified severities override.
89-
serverErrors.add(convertAnalysisError(error,
89+
serverErrors.add(convertAnalysisError(diagnostic,
9090
lineInfo: lineInfo, severity: severity));
9191
}
9292
} else {
93-
serverErrors.add(convertAnalysisError(error, lineInfo: lineInfo));
93+
serverErrors.add(convertAnalysisError(diagnostic, lineInfo: lineInfo));
9494
}
9595
}
9696
return serverErrors;

pkg/analyzer_plugin/lib/utilities/fixes/fix_contributor_mixin.dart

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +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/error/error.dart';
5+
import 'package:analyzer/diagnostic/diagnostic.dart';
66
import 'package:analyzer/src/generated/java_core.dart';
77
import 'package:analyzer_plugin/protocol/protocol_generated.dart';
88
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
@@ -20,19 +20,20 @@ abstract class FixContributorMixin implements FixContributor {
2020
/// The collector to which fixes should be added.
2121
FixCollector? collector;
2222

23-
/// Add a fix for the given [error]. Use the [kind] of the fix to get the
24-
/// message and priority, and use the change [builder] to get the edits that
25-
/// comprise the fix. If the message has parameters, then use the list of
23+
/// Adds a fix for the given [diagnostic]. Use the [kind] of the fix to get
24+
/// the message and priority, and use the change [builder] to get the edits
25+
/// that comprise the fix. If the message has parameters, then use the list of
2626
/// [args] to populate the message.
27-
void addFix(AnalysisError error, FixKind kind, ChangeBuilder builder,
27+
void addFix(Diagnostic diagnostic, FixKind kind, ChangeBuilder builder,
2828
{List<Object>? args}) {
2929
var change = builder.sourceChange;
3030
if (change.edits.isEmpty) {
3131
return;
3232
}
3333
change.id = kind.id;
3434
change.message = formatList(kind.message, args);
35-
collector?.addFix(error, PrioritizedSourceChange(kind.priority, change));
35+
collector?.addFix(
36+
diagnostic, PrioritizedSourceChange(kind.priority, change));
3637
}
3738

3839
@override
@@ -50,7 +51,7 @@ abstract class FixContributorMixin implements FixContributor {
5051
}
5152
}
5253

53-
/// Compute the fixes that are appropriate for the given [error] and add them
54-
/// to the fix [collector].
55-
Future<void> computeFixesForError(AnalysisError error);
54+
/// Computes the fixes that are appropriate for the given [diagnostic] and add
55+
/// them to the fix [collector].
56+
Future<void> computeFixesForError(Diagnostic diagnostic);
5657
}

pkg/analyzer_plugin/lib/utilities/fixes/fixes.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/dart/analysis/results.dart';
6-
import 'package:analyzer/error/error.dart';
6+
import 'package:analyzer/diagnostic/diagnostic.dart';
77
import 'package:analyzer/file_system/file_system.dart';
88
import 'package:analyzer_plugin/protocol/protocol.dart';
99
import 'package:analyzer_plugin/protocol/protocol_generated.dart';
@@ -23,8 +23,8 @@ abstract class DartFixesRequest implements FixesRequest {
2323
///
2424
/// Clients may not extend, implement or mix-in this class.
2525
abstract class FixCollector {
26-
/// Record a new [change] (fix) associated with the given [error].
27-
void addFix(AnalysisError error, PrioritizedSourceChange change);
26+
/// Records a new [change] (fix) associated with the given [diagnostic].
27+
void addFix(Diagnostic diagnostic, PrioritizedSourceChange change);
2828
}
2929

3030
/// An object used to produce fixes.
@@ -43,7 +43,7 @@ abstract class FixContributor {
4343
abstract class FixesRequest {
4444
/// The analysis error to be fixed, or `null` if the error has not been
4545
/// determined.
46-
List<AnalysisError> get errorsToFix;
46+
List<Diagnostic> get errorsToFix;
4747

4848
/// Return the offset within the source for which fixes are being requested.
4949
int get offset;

pkg/analyzer_plugin/lib/utilities/range_factory.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import 'package:analyzer/dart/ast/ast.dart';
66
import 'package:analyzer/dart/ast/syntactic_entity.dart';
77
import 'package:analyzer/dart/ast/token.dart';
88
import 'package:analyzer/dart/element/element.dart';
9-
import 'package:analyzer/error/error.dart';
9+
import 'package:analyzer/diagnostic/diagnostic.dart';
1010
import 'package:analyzer/source/source_range.dart';
1111

1212
/// An instance of [RangeFactory] made available for convenience.
@@ -123,9 +123,10 @@ class RangeFactory {
123123
return SourceRange(node.offset, node.length);
124124
}
125125

126-
/// Return a source range that covers the same range as the given [error].
127-
SourceRange error(AnalysisError error) {
128-
return SourceRange(error.offset, error.length);
126+
/// Returns a source range that covers the same range as the given
127+
/// [diagnostic].
128+
SourceRange error(Diagnostic diagnostic) {
129+
return SourceRange(diagnostic.offset, diagnostic.length);
129130
}
130131

131132
/// Returns a source range that covers the name of the given [fragment].

pkg/analyzer_plugin/test/plugin/fix_mixin_test.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +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/error/error.dart';
5+
import 'package:analyzer/diagnostic/diagnostic.dart';
66
import 'package:analyzer/source/line_info.dart';
77
import 'package:analyzer/src/error/codes.dart';
88
import 'package:analyzer_plugin/plugin/fix_mixin.dart';
@@ -88,14 +88,14 @@ class _TestServerPlugin extends MockServerPlugin with FixesMixin {
8888
@override
8989
Future<FixesRequest> getFixesRequest(EditGetFixesParams parameters) async {
9090
var offset = parameters.offset;
91-
var error = AnalysisError.tmp(
91+
var diagnostic = Diagnostic.tmp(
9292
source: MockSource(),
9393
offset: 0,
9494
length: 0,
9595
errorCode: CompileTimeErrorCode.AWAIT_IN_WRONG_CONTEXT,
9696
);
97-
var result =
98-
MockResolvedUnitResult(lineInfo: LineInfo([0, 20]), errors: [error]);
99-
return DartFixesRequestImpl(resourceProvider, offset, [error], result);
97+
var result = MockResolvedUnitResult(
98+
lineInfo: LineInfo([0, 20]), errors: [diagnostic]);
99+
return DartFixesRequestImpl(resourceProvider, offset, [diagnostic], result);
100100
}
101101
}

pkg/analyzer_plugin/test/plugin/mocks.dart

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

77
import 'package:analyzer/dart/analysis/analysis_context.dart';
88
import 'package:analyzer/dart/analysis/results.dart';
9-
import 'package:analyzer/error/error.dart';
9+
import 'package:analyzer/diagnostic/diagnostic.dart';
1010
import 'package:analyzer/file_system/file_system.dart';
1111
import 'package:analyzer/source/line_info.dart';
1212
import 'package:analyzer/source/source.dart';
@@ -113,7 +113,7 @@ class MockChannel implements PluginCommunicationChannel {
113113

114114
class MockResolvedUnitResult implements ResolvedUnitResult {
115115
@override
116-
final List<AnalysisError> errors;
116+
final List<Diagnostic> errors;
117117

118118
@override
119119
final LineInfo lineInfo;
@@ -122,7 +122,7 @@ class MockResolvedUnitResult implements ResolvedUnitResult {
122122
final String path;
123123

124124
MockResolvedUnitResult(
125-
{List<AnalysisError>? errors, LineInfo? lineInfo, String? path})
125+
{List<Diagnostic>? errors, LineInfo? lineInfo, String? path})
126126
: errors = errors ?? [],
127127
lineInfo = lineInfo ?? LineInfo([0]),
128128
path = path ?? '';

pkg/analyzer_plugin/test/support/abstract_single_unit.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import 'package:analyzer/dart/analysis/results.dart';
66
import 'package:analyzer/dart/ast/ast.dart';
77
import 'package:analyzer/dart/element/element.dart';
8-
import 'package:analyzer/error/error.dart';
98
import 'package:analyzer/src/dart/ast/element_locator.dart';
109
import 'package:analyzer/src/error/codes.dart';
1110
import 'package:analyzer/src/test_utilities/find_element2.dart';
@@ -107,7 +106,7 @@ class AbstractSingleUnitTest extends AbstractContextTest {
107106
testCode = result.content;
108107
testUnit = result.unit;
109108
if (verifyNoTestUnitErrors) {
110-
expect(result.errors.where((AnalysisError error) {
109+
expect(result.errors.where((error) {
111110
return error.errorCode != WarningCode.DEAD_CODE &&
112111
error.errorCode != WarningCode.UNUSED_CATCH_CLAUSE &&
113112
error.errorCode != WarningCode.UNUSED_CATCH_STACK &&

0 commit comments

Comments
 (0)