Bylaw.Credo.Check.Ecto.NamedBinding (bylaw_credo v0.1.0-alpha.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 higher and works with any version of Elixir.

Explanation

Prefer named Ecto bindings over positional bindings in composed queries.

Examples

Avoid:

  User
  |> join(:inner, [u], p in assoc(u, :profile))
  |> where([u, p], p.active)
  |> select([u, p], {u.id, p.display_name})

Prefer:

  User
  |> from(as: :user)
  |> join(:inner, [user: u], p in assoc(u, :profile), as: :profile)
  |> where([profile: p], p.active)
  |> select([user: u, profile: p], {u.id, p.display_name})

Notes

Positional bindings make every later query clause depend on the order of earlier joins. Adding, removing, or reordering a join can silently change what [u, p] means in the rest of the pipeline.

Named bindings make each clause say which relationship it is using, so query changes are easier to review and less sensitive to join order.

Path exclusions are matched against the source filename and are intended for generated files or temporary migration areas.

The check uses static AST analysis, so dynamic code generation and macro-expanded code may fall outside its signal.

Options

Configure options in .credo.exs with the check tuple:

%{
  configs: [
    %{
      name: "default",
      checks: [
        {Bylaw.Credo.Check.Ecto.NamedBinding,
         [
           excluded_paths: ["test/support/"]
         ]}
      ]
    }
  ]
}
  • :excluded_paths - Paths containing any configured string are skipped. Use this for generated files or transitional areas that cannot yet follow named binding conventions.

Usage

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

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

Check-Specific Parameters

Use the following parameters to configure this check:

:excluded_paths

Paths containing any configured string are skipped. Use this for generated files or transitional areas that cannot yet follow named binding conventions.

This parameter defaults to [].

General Parameters

Like with all checks, general params can be applied.

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