Rbtz.CredoChecks.Readability.TopLevelAliasImportRequire (rbtz_credo_checks v0.1.0)

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 normal and works with any version of Elixir.

Explanation

Ensures that alias, import, and require statements appear at the top level of a module rather than inside individual functions or test blocks.

Module-level directives are easier to discover when scanning a file: a reader can see every external dependency at a glance, and the cost of an import is paid once for the whole module rather than re-stated in each function.

Bad

defmodule MyModule do
  def query do
    import Ecto.Query

    from u in User, where: u.active == true
  end
end

Good

defmodule MyModule do
  import Ecto.Query

  def query do
    from u in User, where: u.active == true
  end
end

The check looks inside def, defp, defmacro, describe, test, setup, and setup_all blocks. quote blocks are skipped because alias/import/require are routinely emitted by macros.

Check-Specific Parameters

There are no specific parameters for this check.

General Parameters

Like with all checks, general params can be applied.

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