OXC.Diagnostic (OXC v0.19.0)

Copy Markdown View Source

Errors and lint findings as Elixir Code.diagnostic/1 maps.

Every OXC error and lint finding uses this shape:

%{
  file: "app.ts",
  severity: :error,
  message: "Unexpected token",
  position: {2, 11},
  span: {2, 12},
  source: nil,
  stacktrace: []
}

position is the 1-based {line, column} where the problem starts and span is where it ends, with columns counted in characters. Diagnostics without a source location use position: 0 and span: nil, as Elixir does.

Some diagnostics carry extra keys:

  • :details — help text suggesting a fix
  • :rule — the lint rule that reported the finding
  • :labels — secondary locations, each with :position, :span, and :message
  • :fixes — lint fixes as OXC.patch/0 maps, ready for OXC.patch_string/2
  • :suggestions — alternative type-aware fixes, each with a :message and :fixes

Summary

Types

A native error: message, help, and byte-range labels with the primary label first.

t()

Functions

Format a diagnostic as file:line:column: message, like Elixir compiler output.

Build diagnostics from native errors reported for source in file.

Types

label()

@type label() :: %{position: position(), span: position(), message: String.t() | nil}

position()

@type position() :: {line :: pos_integer(), column :: pos_integer()}

raw()

@type raw() :: %{
  :message => String.t(),
  optional(:help) => String.t() | nil,
  optional(:labels) => [
    {non_neg_integer(), non_neg_integer(), String.t() | nil}
  ],
  optional(:severity) => :error | :warning,
  optional(:rule) => String.t(),
  optional(:fixes) => [{non_neg_integer(), non_neg_integer(), String.t()}],
  optional(:suggestions) => [
    %{
      message: String.t(),
      fixes: [{non_neg_integer(), non_neg_integer(), String.t()}]
    }
  ],
  optional(atom()) => term()
}

A native error: message, help, and byte-range labels with the primary label first.

t()

@type t() :: Code.diagnostic(:error | :warning)

Functions

format(map)

@spec format(t()) :: String.t()

Format a diagnostic as file:line:column: message, like Elixir compiler output.

from_raw(errors, file, source)

@spec from_raw([raw() | String.t()], String.t() | nil, iodata() | nil) :: [t()]

Build diagnostics from native errors reported for source in file.

Pass nil for source when the errors do not refer to a single source text.