Assay.Formatter.Warning (assay v0.6.0)

Copy Markdown View Source

Shared helpers for turning Dialyzer warning payloads into friendly, Elixir-style text sections. A handler can take an entry (as delivered by the formatter), produce a %Result{headline, details}, and lean on these utilities for consistent indentation, diff highlighting, and term rendering.

Example: using the diff helpers

# Given two one-line specs, render a +/- diff with balanced parens and color.
opts = [color?: true]
expected = ["([integer()]) :: integer() | nil"]
actual   = ["(maybe_improper_list()) :: any()"]

Assay.Formatter.Helpers.diff_lines(expected, actual, opts)
# => [
#      IO.ANSI.red()   <> "-  ([integer()]) :: integer() | nil" <>
#      IO.ANSI.reset(),
#      IO.ANSI.green() <> "+  (maybe_improper_list()) :: any()" <>
#      IO.ANSI.reset()
#    ]

Example: rendering Erlang terms as Elixir-friendly text

Assay.Formatter.Helpers.format_term_lines("%{title => <<116,105,116,108,101>>}")
# => ["%{:title => "title"}"]

Handlers that need more control (maps, specs, nested structs) can combine diff_segments/3, diff_map_entries/3, and format_term_lines/1 to build domain-specific sections while preserving the same visual language used by other warnings.

Summary

Functions

Trims Dialyzer prefixes from a reason line.

Renders a diff section header plus indented lines.

Extracts the will never return reason line from a Dialyzer entry, if present.

Formats a module/function into Module.fun.

Builds a simple reason block for warning output.

Builds a Result for a raw Dialyzer entry, delegating to handlers when available.

Renders a labeled value block with optional color.

Functions

clean_reason(line)

Trims Dialyzer prefixes from a reason line.

Examples

iex> Assay.Formatter.Warning.clean_reason("-> will never return since types differ")
"will never return since types differ"

diff_lines(expected_lines, actual_lines, opts)

Convenience wrapper around Assay.Formatter.Helpers.diff_lines/3.

Examples

iex> result = Assay.Formatter.Warning.diff_lines(["(atom())"], ["(binary())"], color?: false)
iex> List.flatten(result)
["-  (atom())", "+  (binary())"]

diff_section(lines, opts)

Renders a diff section header plus indented lines.

Examples

iex> Assay.Formatter.Warning.diff_section(["-  (atom())", "+  (binary())"], color?: false)
["", "Diff (expected -, actual +):", "    -  (atom())", "    +  (binary())"]

extract_reason_line(entry)

Extracts the will never return reason line from a Dialyzer entry, if present.

Examples

iex> entry = %{text: "lib/foo.ex:1: -> will never return since types differ"}
iex> Assay.Formatter.Warning.extract_reason_line(entry)
"will never return since types differ"

format_call(module, fun)

Formats a module/function into Module.fun.

Examples

iex> Assay.Formatter.Warning.format_call(Foo.Bar, :run)
"Foo.Bar.run"

format_term_lines(value)

Convenience wrapper around Assay.Formatter.Helpers.format_term_lines/1.

Examples

iex> Assay.Formatter.Warning.format_term_lines("%{title => <<116,105,116,108,101>>}")
["%{title => \"title\"}"]

iex> Assay.Formatter.Warning.format_term_lines("(atom())")
["(atom())"]

reason_block(line)

Builds a simple reason block for warning output.

Examples

iex> Assay.Formatter.Warning.reason_block("will never return")
["", "Reason:", "  will never return"]

render(entry, opts \\ [])

@spec render(
  map(),
  keyword()
) :: Assay.Formatter.Warning.Result.t()

Builds a Result for a raw Dialyzer entry, delegating to handlers when available.

Examples

iex> entry = %{text: "warning text", match_text: "warning text", path: nil, line: nil, column: nil, code: nil}
iex> Assay.Formatter.Warning.render(entry).headline
"warning text"

value_block(label, lines, opts)

Renders a labeled value block with optional color.

Examples

iex> Assay.Formatter.Warning.value_block("Expected", ["(atom())"], color?: false)
["Expected:", "  (atom())"]