Skip to content

Commit d8ea620

Browse files
committed
rewrite negated assert/refute
1 parent 51d1cc7 commit d8ea620

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

CHANGELOG.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@ they can and will change without that change being reflected in Styler's semanti
55

66
## main
77

8+
### Improvements
9+
10+
- rewrite some negated asserts and refutes
11+
12+
| before | styled |
13+
|---------------------|-----------------|
14+
| `assert x != nil` | `assert x` |
15+
| `assert !!x` | `assert x` |
16+
| `assert !x` | `refute x` |
17+
| `assert not x` | `refute x` |
18+
| `assert !is_nil(x)` | `assert x` |
19+
| `assert x not in y` | `refute x in y` |
20+
| `refute !x` | `assert x` |
21+
| `refute not x` | `assert x` |
22+
| `refute x not in y` | `assert x in y` |
23+
| `assert x == nil` | _no change_ |
24+
| `assert is_nil(x)` | _no change_ |
25+
| `refute x` | _no change_ |
26+
27+
828
### Fixes
929

1030
- alias lifting: fix bug lifting in snippets with a single ast node at the root level (like a credo config file) (#240, h/t @defndaines)

docs/general_styles.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,22 @@ def save(
241241
# Params comment
242242
def save(%Socket{assigns: %{user: user, live_action: :new}} = initial_socket, params), do: :ok
243243
```
244+
245+
## Negated Assert/Refute
246+
247+
Notably there are three ways to write "assert the thing is nil", but Styler doesn't yet feel like coercing codebases to a single style there.
248+
249+
| before | styled |
250+
|---------------------|-----------------|
251+
| `assert x != nil` | `assert x` |
252+
| `assert !!x` | `assert x` |
253+
| `assert !x` | `refute x` |
254+
| `assert not x` | `refute x` |
255+
| `assert !is_nil(x)` | `assert x` |
256+
| `assert x not in y` | `refute x in y` |
257+
| `refute !x` | `assert x` |
258+
| `refute not x` | `assert x` |
259+
| `refute x not in y` | `assert x in y` |
260+
| `assert x == nil` | _no change_ |
261+
| `assert is_nil(x)` | _no change_ |
262+
| `refute x` | _no change_ |

lib/style/single_node.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ defmodule Styler.Style.SingleNode do
3737

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

40+
# reverse negated assert/refute
41+
defp style({:assert, meta, [{:!=, _, [x, {:__block__, _, [nil]}]}]}), do: {:assert, meta, [x]}
42+
defp style({:assert, meta, [{:!=, _, [{:__block__, _, [nil]}, y]}]}), do: {:assert, meta, [y]}
43+
defp style({:assert, meta, [{n, _, [x]}]}) when n in [:!, :not], do: style({:refute, meta, [x]})
44+
defp style({:refute, meta, [{n, _, [x]}]}) when n in [:!, :not], do: style({:assert, meta, [x]})
45+
defp style({:refute, meta, [{:is_nil, _, [x]}]}), do: style({:assert, meta, [x]})
46+
4047
# rewrite double-quote strings with >= 4 escaped double-quotes as sigils
4148
defp style({:__block__, [{:delimiter, ~s|"|} | meta], [string]} = node) when is_binary(string) do
4249
# running a regex against every double-quote delimited string literal in a codebase doesn't have too much impact

test/style/single_node_test.exs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@
1111
defmodule Styler.Style.SingleNodeTest do
1212
use Styler.StyleCase, async: true
1313

14+
test "assert/refute negation" do
15+
assert_style "assert x != nil", "assert x"
16+
assert_style "assert !!x", "assert x"
17+
assert_style "assert !x", "refute x"
18+
assert_style "assert not x", "refute x"
19+
assert_style "assert !is_nil(x)", "assert x"
20+
assert_style "assert x not in y", "refute x in y"
21+
22+
assert_style "refute !x", "assert x"
23+
assert_style "refute not x", "assert x"
24+
assert_style "refute x not in y", "assert x in y"
25+
end
26+
1427
test "string sigil rewrites" do
1528
assert_style ~s|""|
1629
assert_style ~s|"\\""|

0 commit comments

Comments
 (0)