AB.TypeParser (AB v0.2.1)

View Source

Functions for parsing and extracting type specifications from modules.

Summary

Functions

Creates a struct generator based on the module's @type definition rather than @spec.

Extracts struct fields from field types.

Extracts the typespec for a given function. Returns {:ok, {input_types, output_type}} or {:error, reason}.

Gets a function spec with all user and remote types fully resolved.

Parses a spec AST into input and output types.

Resolves all user and remote types in a type specification recursively. Returns the fully resolved type with all type aliases expanded.

Compares two type specifications for equivalence, ignoring metadata like line numbers.

Functions

create_struct_from_type_definition(module)

@spec create_struct_from_type_definition(module()) :: any() | nil

Creates a struct generator based on the module's @type definition rather than @spec.

extract_struct_fields(field_types)

@spec extract_struct_fields([any()]) :: %{required(atom()) => any()} | nil

Extracts struct fields from field types.

get_function_spec(module, function_name)

@spec get_function_spec(module(), atom()) ::
  {:ok, {[any()], any()}} | {:error, String.t()}

Extracts the typespec for a given function. Returns {:ok, {input_types, output_type}} or {:error, reason}.

get_resolved_function_spec(module, function_name)

@spec get_resolved_function_spec(module(), atom()) ::
  {:ok, {[any()], any()}} | {:error, String.t()}

Gets a function spec with all user and remote types fully resolved.

Examples

iex> get_resolved_function_spec(MyModule, :my_function)
{:ok, {[resolved_input_types], resolved_output_type}}

parse_spec(arg1)

@spec parse_spec(any()) :: {:ok, {[any()], any()}} | {:error, String.t()}
@spec parse_spec(any()) :: {:ok, {[any()], any()}} | {:error, String.t()}
@spec parse_spec(any()) :: {:ok, {[any()], any()}} | {:error, String.t()}

Parses a spec AST into input and output types.

resolve_all_types(type, module)

@spec resolve_all_types(any(), module()) :: any()

Resolves all user and remote types in a type specification recursively. Returns the fully resolved type with all type aliases expanded.

Examples

iex> resolve_all_types({:user_type, 0, :opts, []}, MyModule)
{:type, 0, :list, [...]}

iex> resolve_all_types({:remote_type, 0, [{:atom, 0, String}, {:atom, 0, :t}, []]}, MyModule)
{:type, 0, :binary, []}

types_equivalent?(type1, type2)

@spec types_equivalent?(any(), any()) :: boolean()
@spec types_equivalent?(any(), any()) :: boolean()
@spec types_equivalent?(any(), any()) :: boolean()
@spec types_equivalent?(any(), any()) :: boolean()
@spec types_equivalent?(any(), any()) :: boolean()
@spec types_equivalent?(any(), any()) :: boolean()
@spec types_equivalent?(any(), any()) :: boolean()
@spec types_equivalent?(any(), any()) :: boolean()

Compares two type specifications for equivalence, ignoring metadata like line numbers.