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>
This commit is contained in:
Sam Rawlins 2021-12-04 01:38:59 +00:00 committed by Commit Bot
parent edf7b0587a
commit 0b73860956
9 changed files with 31 additions and 27 deletions

View file

@ -431,7 +431,7 @@ abstract class AbstractAnalysisServer {
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 @@ abstract class AbstractAnalysisServer {
/// Sends an error notification to the user.
void sendServerErrorNotification(
String message,
dynamic exception,
/*StackTrace*/ stackTrace, {
Object exception,
StackTrace? stackTrace, {
bool fatal = false,
});

View file

@ -221,7 +221,7 @@ class ContextManagerImpl implements ContextManager {
this._performanceLog,
this._scheduler,
this._instrumentationService,
{required enableBazelWatcher})
{required bool enableBazelWatcher})
: pathContext = resourceProvider.pathContext {
if (enableBazelWatcher) {
bazelWatcherService = BazelFileWatcherService(_instrumentationService);

View file

@ -80,7 +80,7 @@ class LspByteStreamServerChannel implements LspServerCommunicationChannel {
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)) {

View file

@ -120,7 +120,7 @@ class PerformRefactorCommandHandler extends SimpleEditCommandHandler {
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 @@ class PerformRefactorCommandHandler extends SimpleEditCommandHandler {
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 @@ class PerformRefactorCommandHandler extends SimpleEditCommandHandler {
// 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:

View file

@ -134,7 +134,8 @@ abstract class MessageHandler<P, R>
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 @@ abstract class MessageHandler<P, R>
);
}
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);
}
}

View file

@ -374,17 +374,18 @@ class LspAnalysisServer extends AbstractAnalysisServer {
/// 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 @@ class LspAnalysisServer extends AbstractAnalysisServer {
exceptions.add(ServerException(
message,
exception,
stackTrace is StackTrace ? stackTrace : StackTrace.current,
stackTrace,
false,
));
@ -550,9 +551,10 @@ class LspAnalysisServer extends AbstractAnalysisServer {
}
@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 @@ class LspAnalysisServer extends AbstractAnalysisServer {
/// 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 @@ class LspAnalysisServer extends AbstractAnalysisServer {
_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}),

View file

@ -1225,12 +1225,12 @@ lsp.Location toLocation(server.Location location, server.LineInfo lineInfo) =>
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',

View file

@ -31,7 +31,7 @@ ErrorOr<Pair<String, List<plugin.SourceEdit>>> applyAndConvertEditsToServer(
Either2<TextDocumentContentChangeEvent1,
TextDocumentContentChangeEvent2>>
changes, {
failureIsCritical = false,
bool failureIsCritical = false,
}) {
var newContent = oldContent;
final serverEdits = <server.SourceEdit>[];
@ -45,9 +45,9 @@ ErrorOr<Pair<String, List<plugin.SourceEdit>>> applyAndConvertEditsToServer(
(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);
}

View file

@ -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 @@ class SearchDomainHandler implements protocol.RequestHandler {
}
/// 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);
}