Bylaw.Credo.Check.Elixir.NoRemoteCallsInModuleAttributes (bylaw_credo v0.3.1)

Copy Markdown View Source

Basics

This check is disabled by default.

Learn how to enable it via .credo.exs.

This check has a base priority of high and works with any version of Elixir.

Explanation

Avoid calls into application and dependency modules from module attributes.

Module attributes are evaluated at compile time. Calling another module from an attribute creates a compile-time dependency and embeds the returned value in the compiled consumer.

Examples

Avoid:

@some_values MyApp.SomeModule.some_function()

Prefer:

def some_values do
  MyApp.SomeModule.some_function()
end

Literal values are accepted:

@some_values [:one, :two]

Calls into Elixir and OTP standard-library modules are accepted by default because they do not add dependencies between project files:

@some_values Enum.uniq([:one, :one])

When compile-time evaluation is intentional, keep the call and disable this check locally:

# credo:disable-for-next-line Bylaw.Credo.Check.Elixir.NoRemoteCallsInModuleAttributes
@some_values MyApp.SomeModule.some_function()

Notes

Typespec attributes are ignored because calls such as String.t() describe types and do not execute the named function.

External function captures stored in attributes are reported because the compiler tracks them as compile-time dependencies too.

This check uses static AST analysis. It recognizes statically named Elixir and Erlang modules, including nested calls inside an attribute value.

Options

  • :allow_standard_library - When true, accept calls into modules shipped by the Elixir, Kernel, and standard-library OTP applications. Defaults to true.

Usage

Add this check to Credo's checks: list in .credo.exs:

%{
  configs: [
    %{
      name: "default",
      checks: [
        {Bylaw.Credo.Check.Elixir.NoRemoteCallsInModuleAttributes, []}
      ]
    }
  ]
}

To report standard-library calls as well:

{Bylaw.Credo.Check.Elixir.NoRemoteCallsInModuleAttributes,
 [allow_standard_library: false]}

Check-Specific Parameters

Use the following parameters to configure this check:

:allow_standard_library

When true, accept calls into Elixir and OTP standard-library modules

This parameter defaults to true.

General Parameters

Like with all checks, general params can be applied.

Parameters can be configured via the .credo.exs config file.