Skip to content

Commit

Permalink
Merge branch 'main' into me/config-ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Apr 4, 2024
2 parents 3edad64 + 6b0fa79 commit 7c0cab5
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
name: Ex${{matrix.elixir}}/OTP${{matrix.otp}}
strategy:
matrix:
elixir: ['1.14.2', '1.15.7', '1.16.0']
elixir: ['1.15.7', '1.16.0']
otp: ['25.1.2']
steps:
- uses: actions/checkout@v4
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
* module directives: various fixes for unreported obscure crashes
* pipes: fix a comment-shifting scenario when unpiping

### Breaking Changes

* drop support for elixir `1.14`

## v0.11.9

### Improvements
Expand Down
21 changes: 6 additions & 15 deletions lib/style/single_node.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ defmodule Styler.Style.SingleNode do

def run({node, meta}, ctx), do: {:cont, {style(node), meta}, ctx}

# as of 1.15, elixir's formatter takes care of this for us.
if Version.match?(System.version(), "< 1.15.0-dev") do
# 'charlist' => ~c"charlist"
defp style({:__block__, [{:delimiter, ~s|'|} | meta], [chars]}) when is_list(chars),
do: {:sigil_c, [{:delimiter, ~s|"|} | meta], [{:<<>>, [line: meta[:line]], [List.to_string(chars)]}, []]}
end

# rewrite double-quote strings with >= 4 escaped double-quotes as sigils
defp style({:__block__, [{:delimiter, ~s|"|} | meta], [string]} = node) when is_binary(string) do
# running a regex against every double-quote delimited string literal in a codebase doesn't have too much impact
Expand Down Expand Up @@ -150,14 +143,12 @@ defmodule Styler.Style.SingleNode do
defp style({{:., dm, [{:__aliases__, am, [:Timex]}, :now]}, funm, [tz]}),
do: {{:., dm, [{:__aliases__, am, [:DateTime]}, :now!]}, funm, [tz]}

if Version.match?(System.version(), ">= 1.15.0-dev") do
# {DateTime,NaiveDateTime,Time,Date}.compare(a, b) == :lt => {DateTime,NaiveDateTime,Time,Date}.before?(a, b)
# {DateTime,NaiveDateTime,Time,Date}.compare(a, b) == :gt => {DateTime,NaiveDateTime,Time,Date}.after?(a, b)
defp style({:==, _, [{{:., dm, [{:__aliases__, am, [mod]}, :compare]}, funm, args}, {:__block__, _, [result]}]})
when mod in ~w[DateTime NaiveDateTime Time Date]a and result in [:lt, :gt] do
fun = if result == :lt, do: :before?, else: :after?
{{:., dm, [{:__aliases__, am, [mod]}, fun]}, funm, args}
end
# {DateTime,NaiveDateTime,Time,Date}.compare(a, b) == :lt => {DateTime,NaiveDateTime,Time,Date}.before?(a, b)
# {DateTime,NaiveDateTime,Time,Date}.compare(a, b) == :gt => {DateTime,NaiveDateTime,Time,Date}.after?(a, b)
defp style({:==, _, [{{:., dm, [{:__aliases__, am, [mod]}, :compare]}, funm, args}, {:__block__, _, [result]}]})
when mod in ~w[DateTime NaiveDateTime Time Date]a and result in [:lt, :gt] do
fun = if result == :lt, do: :before?, else: :after?
{{:., dm, [{:__aliases__, am, [mod]}, fun]}, funm, args}
end

# Remove parens from 0 arity funs (Credo.Check.Readability.ParenthesesOnZeroArityDefs)
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule Styler.MixProject do
[
app: :styler,
version: @version,
elixir: "~> 1.14",
elixir: "~> 1.15",
start_permanent: Mix.env() == :prod,
elixirc_paths: elixirc_paths(Mix.env()),
deps: deps(),
Expand Down
32 changes: 11 additions & 21 deletions test/style/single_node_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@
defmodule Styler.Style.SingleNodeTest do
use Styler.StyleCase, async: true

if Version.match?(System.version(), "< 1.15.0-dev") do
test "charlist literals: rewrites single quote charlists to ~c" do
assert_style("'foo'", ~s|~c"foo"|)
assert_style(~s|'"'|, ~s|~c"\\""|)
assert_style("''", ~s|~c""|)
end
end

test "string sigil rewrites" do
assert_style ~s|""|
assert_style ~s|"\\""|
Expand Down Expand Up @@ -70,20 +62,18 @@ defmodule Styler.Style.SingleNodeTest do
end
end

if Version.match?(System.version(), ">= 1.15.0-dev") do
test "{DateTime,NaiveDateTime,Time,Date}.compare to {DateTime,NaiveDateTime,Time,Date}.before?" do
assert_style("DateTime.compare(foo, bar) == :lt", "DateTime.before?(foo, bar)")
assert_style("NaiveDateTime.compare(foo, bar) == :lt", "NaiveDateTime.before?(foo, bar)")
assert_style("Time.compare(foo, bar) == :lt", "Time.before?(foo, bar)")
assert_style("Date.compare(foo, bar) == :lt", "Date.before?(foo, bar)")
end
test "{DateTime,NaiveDateTime,Time,Date}.compare to {DateTime,NaiveDateTime,Time,Date}.before?" do
assert_style("DateTime.compare(foo, bar) == :lt", "DateTime.before?(foo, bar)")
assert_style("NaiveDateTime.compare(foo, bar) == :lt", "NaiveDateTime.before?(foo, bar)")
assert_style("Time.compare(foo, bar) == :lt", "Time.before?(foo, bar)")
assert_style("Date.compare(foo, bar) == :lt", "Date.before?(foo, bar)")
end

test "{DateTime,NaiveDateTime,Time,Date}.compare to {DateTime,NaiveDateTime,Time,Date}.after?" do
assert_style("DateTime.compare(foo, bar) == :gt", "DateTime.after?(foo, bar)")
assert_style("NaiveDateTime.compare(foo, bar) == :gt", "NaiveDateTime.after?(foo, bar)")
assert_style("Time.compare(foo, bar) == :gt", "Time.after?(foo, bar)")
assert_style("Time.compare(foo, bar) == :gt", "Time.after?(foo, bar)")
end
test "{DateTime,NaiveDateTime,Time,Date}.compare to {DateTime,NaiveDateTime,Time,Date}.after?" do
assert_style("DateTime.compare(foo, bar) == :gt", "DateTime.after?(foo, bar)")
assert_style("NaiveDateTime.compare(foo, bar) == :gt", "NaiveDateTime.after?(foo, bar)")
assert_style("Time.compare(foo, bar) == :gt", "Time.after?(foo, bar)")
assert_style("Time.compare(foo, bar) == :gt", "Time.after?(foo, bar)")
end

describe "def / defp" do
Expand Down

0 comments on commit 7c0cab5

Please sign in to comment.