analysis_server: more strict-casts improvements
Mostly this CL involves adding types to parameters which were implicitly
dynamic, and casting a few JSON results. It should be a no-op,
behaviorally.
Bug: https://github.com/dart-lang/sdk/issues/41651
Change-Id: I230ae4f9eef3a6c9e4dbfe60b95907402aefda05
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/222027
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
diff --git a/pkg/analysis_server/lib/src/analysis_server_abstract.dart b/pkg/analysis_server/lib/src/analysis_server_abstract.dart
index 4280b0d..7724107 100644
--- a/pkg/analysis_server/lib/src/analysis_server_abstract.dart
+++ b/pkg/analysis_server/lib/src/analysis_server_abstract.dart
@@ -431,7 +431,7 @@
return driver
.getResult(path, sendCachedToStream: sendCachedToStream)
.then((value) => value is ResolvedUnitResult ? value : null)
- .catchError((e, st) {
+ .catchError((Object e, StackTrace st) {
instrumentationService.logException(e, st);
return null;
});
@@ -505,8 +505,8 @@
/// Sends an error notification to the user.
void sendServerErrorNotification(
String message,
- dynamic exception,
- /*StackTrace*/ stackTrace, {
+ Object exception,
+ StackTrace? stackTrace, {
bool fatal = false,
});
diff --git a/pkg/analysis_server/lib/src/context_manager.dart b/pkg/analysis_server/lib/src/context_manager.dart
index 7f494be..b6df480 100644
--- a/pkg/analysis_server/lib/src/context_manager.dart
+++ b/pkg/analysis_server/lib/src/context_manager.dart
@@ -221,7 +221,7 @@
this._performanceLog,
this._scheduler,
this._instrumentationService,
- {required enableBazelWatcher})
+ {required bool enableBazelWatcher})
: pathContext = resourceProvider.pathContext {
if (enableBazelWatcher) {
bazelWatcherService = BazelFileWatcherService(_instrumentationService);
diff --git a/pkg/analysis_server/lib/src/lsp/channel/lsp_byte_stream_channel.dart b/pkg/analysis_server/lib/src/lsp/channel/lsp_byte_stream_channel.dart
index ed1ea21..a86e794 100644
--- a/pkg/analysis_server/lib/src/lsp/channel/lsp_byte_stream_channel.dart
+++ b/pkg/analysis_server/lib/src/lsp/channel/lsp_byte_stream_channel.dart
@@ -80,7 +80,7 @@
return;
}
_instrumentationService.logRequest(data);
- final Map<String, Object?> json = jsonDecode(data);
+ final json = jsonDecode(data) as Map<String, Object?>;
if (RequestMessage.canParse(json, nullLspJsonReporter)) {
onMessage(RequestMessage.fromJson(json));
} else if (NotificationMessage.canParse(json, nullLspJsonReporter)) {
diff --git a/pkg/analysis_server/lib/src/lsp/handlers/commands/perform_refactor.dart b/pkg/analysis_server/lib/src/lsp/handlers/commands/perform_refactor.dart
index 20c4d27..4de8bfe 100644
--- a/pkg/analysis_server/lib/src/lsp/handlers/commands/perform_refactor.dart
+++ b/pkg/analysis_server/lib/src/lsp/handlers/commands/perform_refactor.dart
@@ -120,7 +120,7 @@
final refactor = ExtractMethodRefactoring(
server.searchEngine, result, offset, length);
- var preferredName = options != null ? options['name'] : null;
+ var preferredName = options != null ? options['name'] as String : null;
// checkInitialConditions will populate names with suggestions.
if (preferredName == null) {
await refactor.checkInitialConditions();
@@ -138,7 +138,7 @@
case RefactoringKind.EXTRACT_LOCAL_VARIABLE:
final refactor = ExtractLocalRefactoring(result, offset, length);
- var preferredName = options != null ? options['name'] : null;
+ var preferredName = options != null ? options['name'] as String : null;
// checkInitialConditions will populate names with suggestions.
if (preferredName == null) {
await refactor.checkInitialConditions();
@@ -162,7 +162,7 @@
// to handle this better.
// https://github.com/microsoft/language-server-protocol/issues/764
refactor.name =
- (options != null ? options['name'] : null) ?? 'NewWidget';
+ options != null ? options['name'] as String : 'NewWidget';
return success(refactor);
case RefactoringKind.INLINE_LOCAL_VARIABLE:
diff --git a/pkg/analysis_server/lib/src/lsp/handlers/handlers.dart b/pkg/analysis_server/lib/src/lsp/handlers/handlers.dart
index 6b87d05..6b2ed52 100644
--- a/pkg/analysis_server/lib/src/lsp/handlers/handlers.dart
+++ b/pkg/analysis_server/lib/src/lsp/handlers/handlers.dart
@@ -134,7 +134,8 @@
FutureOr<ErrorOr<R>> handleMessage(
IncomingMessage message, CancellationToken token) {
final reporter = LspJsonReporter('params');
- if (!jsonHandler.validateParams(message.params, reporter)) {
+ final paramsJson = message.params as Map<String, Object?>?;
+ if (!jsonHandler.validateParams(paramsJson, reporter)) {
return error(
ErrorCodes.InvalidParams,
'Invalid params for ${message.method}:\n'
@@ -144,9 +145,8 @@
);
}
- final params = message.params != null
- ? jsonHandler.convertParams(message.params)
- : null as P;
+ final params =
+ paramsJson != null ? jsonHandler.convertParams(paramsJson) : null as P;
return handle(params, token);
}
}
diff --git a/pkg/analysis_server/lib/src/lsp/lsp_analysis_server.dart b/pkg/analysis_server/lib/src/lsp/lsp_analysis_server.dart
index d33a96e..cf22460 100644
--- a/pkg/analysis_server/lib/src/lsp/lsp_analysis_server.dart
+++ b/pkg/analysis_server/lib/src/lsp/lsp_analysis_server.dart
@@ -374,17 +374,18 @@
/// Logs an exception by sending it to the client (window/logMessage) and
/// recording it in a buffer on the server for diagnostics.
- void logException(String message, exception, stackTrace) {
+ void logException(String message, Object exception, StackTrace? stackTrace) {
var fullMessage = message;
if (exception is CaughtException) {
stackTrace ??= exception.stackTrace;
fullMessage = '$fullMessage: ${exception.exception}';
- } else if (exception != null) {
+ } else {
fullMessage = '$fullMessage: $exception';
}
final fullError =
stackTrace == null ? fullMessage : '$fullMessage\n$stackTrace';
+ stackTrace ??= StackTrace.current;
// Log the full message since showMessage above may be truncated or
// formatted badly (eg. VS Code takes the newlines out).
@@ -394,7 +395,7 @@
exceptions.add(ServerException(
message,
exception,
- stackTrace is StackTrace ? stackTrace : StackTrace.current,
+ stackTrace,
false,
));
@@ -550,9 +551,10 @@
}
@override
- void sendServerErrorNotification(String message, exception, stackTrace,
+ void sendServerErrorNotification(
+ String message, Object exception, StackTrace? stackTrace,
{bool fatal = false}) {
- message = exception == null ? message : '$message: $exception';
+ message = '$message: $exception';
// Show message (without stack) to the user.
showErrorMessageToUser(message);
@@ -652,9 +654,9 @@
/// There was an error related to the socket from which messages are being
/// read.
- void socketError(error, stack) {
+ void socketError(Object error, StackTrace? stackTrace) {
// Don't send to instrumentation service; not an internal error.
- sendServerErrorNotification('Socket error', error, stack);
+ sendServerErrorNotification('Socket error', error, stackTrace);
}
Future<void> updateWorkspaceFolders(
@@ -670,7 +672,7 @@
_refreshAnalysisRoots();
}
- void _afterOverlayChanged(String path, dynamic changeForPlugins) {
+ void _afterOverlayChanged(String path, plugin.HasToJson changeForPlugins) {
driverMap.values.forEach((driver) => driver.changeFile(path));
pluginManager.setAnalysisUpdateContentParams(
plugin.AnalysisUpdateContentParams({path: changeForPlugins}),
diff --git a/pkg/analysis_server/lib/src/lsp/mapping.dart b/pkg/analysis_server/lib/src/lsp/mapping.dart
index 36d72cc..ffabd19 100644
--- a/pkg/analysis_server/lib/src/lsp/mapping.dart
+++ b/pkg/analysis_server/lib/src/lsp/mapping.dart
@@ -1225,12 +1225,12 @@
ErrorOr<int> toOffset(
server.LineInfo lineInfo,
lsp.Position pos, {
- failureIsCritial = false,
+ bool failureIsCritical = false,
}) {
// line is zero-based so cannot equal lineCount
if (pos.line >= lineInfo.lineCount) {
return ErrorOr<int>.error(lsp.ResponseError(
- code: failureIsCritial
+ code: failureIsCritical
? lsp.ServerErrorCodes.ClientServerInconsistentState
: lsp.ServerErrorCodes.InvalidFileLineCol,
message: 'Invalid line number',
diff --git a/pkg/analysis_server/lib/src/lsp/source_edits.dart b/pkg/analysis_server/lib/src/lsp/source_edits.dart
index 49b1b09..1e2b464 100644
--- a/pkg/analysis_server/lib/src/lsp/source_edits.dart
+++ b/pkg/analysis_server/lib/src/lsp/source_edits.dart
@@ -31,7 +31,7 @@
Either2<TextDocumentContentChangeEvent1,
TextDocumentContentChangeEvent2>>
changes, {
- failureIsCritical = false,
+ bool failureIsCritical = false,
}) {
var newContent = oldContent;
final serverEdits = <server.SourceEdit>[];
@@ -45,9 +45,9 @@
(change) {
final lines = LineInfo.fromContent(newContent);
final offsetStart = toOffset(lines, change.range.start,
- failureIsCritial: failureIsCritical);
+ failureIsCritical: failureIsCritical);
final offsetEnd = toOffset(lines, change.range.end,
- failureIsCritial: failureIsCritical);
+ failureIsCritical: failureIsCritical);
if (offsetStart.isError) {
return ErrorOr.error(offsetStart.error);
}
diff --git a/pkg/analysis_server/lib/src/search/search_domain.dart b/pkg/analysis_server/lib/src/search/search_domain.dart
index 3336e28..04542a8 100644
--- a/pkg/analysis_server/lib/src/search/search_domain.dart
+++ b/pkg/analysis_server/lib/src/search/search_domain.dart
@@ -5,6 +5,8 @@
import 'package:analysis_server/protocol/protocol.dart';
import 'package:analysis_server/protocol/protocol_constants.dart';
import 'package:analysis_server/src/analysis_server.dart';
+import 'package:analysis_server/src/protocol/protocol_internal.dart'
+ show ResponseResult;
import 'package:analysis_server/src/protocol_server.dart' as protocol;
import 'package:analysis_server/src/search/element_references.dart';
import 'package:analysis_server/src/search/type_hierarchy.dart';
@@ -262,8 +264,8 @@
}
/// Send a search response with the given [result] to the given [request].
- void _sendSearchResult(protocol.Request request, result) {
- protocol.Response response = result.toResponse(request.id);
+ void _sendSearchResult(protocol.Request request, ResponseResult result) {
+ var response = result.toResponse(request.id);
server.sendResponse(response);
}