convertAnalysisError method

AnalysisError convertAnalysisError(
  1. Diagnostic diagnostic, {
  2. LineInfo? lineInfo,
  3. DiagnosticSeverity? severity,
})

Converts the analysis diagnostic 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.Diagnostic diagnostic, {
  analyzer.LineInfo? lineInfo,
  analyzer.DiagnosticSeverity? severity,
}) {
  var diagnosticCode = diagnostic.diagnosticCode;
  severity ??= diagnosticCode.severity;
  var offset = diagnostic.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 + diagnostic.length);
    endLine = endLocation.lineNumber;
    endColumn = endLocation.columnNumber;
  }
  List<plugin.DiagnosticMessage>? contextMessages;
  if (diagnostic.contextMessages.isNotEmpty) {
    contextMessages = diagnostic.contextMessages
        .map((message) =>
            convertDiagnosticMessage(message, lineInfo: lineInfo))
        .toList();
  }
  return plugin.AnalysisError(
      convertErrorSeverity(severity),
      convertErrorType(diagnosticCode.type),
      plugin.Location(diagnostic.source.fullName, offset, diagnostic.length,
          startLine, startColumn,
          endLine: endLine, endColumn: endColumn),
      diagnostic.message,
      diagnosticCode.name.toLowerCase(),
      contextMessages: contextMessages,
      correction: diagnostic.correctionMessage,
      hasFix: true);
}