convertAnalysisError method

AnalysisError convertAnalysisError(
  1. AnalysisError error, {
  2. LineInfo? lineInfo,
  3. ErrorSeverity? severity,
})

Converts the analysis error from the 'analyzer' package to an analysis error defined by the plugin API.

If a lineInfo is provided then the error's location will have a start line and start column. If a severity is provided, then it will override the severity defined by the error.

Implementation

plugin.AnalysisError convertAnalysisError(
  analyzer.AnalysisError error, {
  analyzer.LineInfo? lineInfo,
  analyzer.ErrorSeverity? severity,
}) {
  var errorCode = error.errorCode;
  severity ??= errorCode.errorSeverity;
  var offset = error.offset;
  var startLine = -1;
  var startColumn = -1;
  var endLine = -1;
  var endColumn = -1;
  if (lineInfo != null) {
    var startLocation = lineInfo.getLocation(offset);
    startLine = startLocation.lineNumber;
    startColumn = startLocation.columnNumber;
    var endLocation = lineInfo.getLocation(offset + error.length);
    endLine = endLocation.lineNumber;
    endColumn = endLocation.columnNumber;
  }
  List<plugin.DiagnosticMessage>? contextMessages;
  if (error.contextMessages.isNotEmpty) {
    contextMessages = error.contextMessages
        .map((message) =>
            convertDiagnosticMessage(message, lineInfo: lineInfo))
        .toList();
  }
  return plugin.AnalysisError(
      convertErrorSeverity(severity),
      convertErrorType(errorCode.type),
      plugin.Location(
          error.source.fullName, offset, error.length, startLine, startColumn,
          endLine: endLine, endColumn: endColumn),
      error.message,
      errorCode.name.toLowerCase(),
      contextMessages: contextMessages,
      correction: error.correction,
      hasFix: true);
}