Bylaw.Credo.Check.Elixir.SafeDateTimeComparison
(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
Avoid direct comparison operators on values that look like dates or times.
Examples
Avoid:
entry.inserted_at > cutoff_at
start_date <= end_datePrefer:
DateTime.after?(entry.inserted_at, cutoff_at)
Date.compare(start_date, end_date) in [:lt, :eq]Notes
Elixir's term ordering can compare structs even when the comparison is not the domain comparison you meant. Date and time types have comparison functions that encode the correct semantics.
Use compare/2, before?/2, or after?/2 from the relevant date/time
module. Ecto where clauses are ignored because query comparisons are
translated by Ecto instead of using Elixir term ordering.
This check uses static AST analysis, so it favors clear source-level patterns over runtime behavior.
Options
Configure options in .credo.exs with the check tuple:
%{
configs: [
%{
name: "default",
checks: [
{Bylaw.Credo.Check.Elixir.SafeDateTimeComparison,
[
datetime_suffixes: ~w(_datetime _at _date _time)
]}
]
}
]
}:datetime_suffixes- Variable and field suffixes that make a value look like a date or time. Defaults to_datetime,_at,_date, and_time.
Usage
Add this check to Credo's checks: list in .credo.exs:
%{
configs: [
%{
name: "default",
checks: [
{Bylaw.Credo.Check.Elixir.SafeDateTimeComparison, []}
]
}
]
}Check-Specific Parameters
Use the following parameters to configure this check:
:datetime_suffixes
Variable and field suffixes that make a value look like a date or time.
Defaults to _datetime, _at, _date, and _time.
This parameter defaults to ["_datetime", "_at", "_date", "_time"].
General Parameters
Like with all checks, general params can be applied.
Parameters can be configured via the .credo.exs config file.