Skip to content

Commit

Permalink
outline docs cheatsheets
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Apr 22, 2024
1 parent 636f5db commit 367a9be
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/module_directives.cheatmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Module Directives (`use`, `import`, `alias`, `require`, ...)
32 changes: 32 additions & 0 deletions docs/pipe_chains.cheatmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Pipe Chain Styles


## Pipe Start

- raw value
- blocks are extracted to variables
- ecto's `from` is allowed

## Piped function rewrites

- add parens to function calls `|> fun |>` => `|> fun() |>`
- remove unnecessary `then/2`: `|> then(&f(&1, ...))` -> `|> f(...)`
- add `then` when defining anon funs in pipe `|> (& &1).() |>` => `|> |> then(& &1) |>`

## Piped function optimizations

- tries to fit everything on one line
- `lhs |> Enum.reverse() |> Enum.concat(enum)` => `lhs |> Enum.reverse(enum)` (also Kernel.++)
- `lhs |> Enum.filter(filterer) |> Enum.count()` => `lhs |> Enum.count(count)`
- `lhs |> Enum.map(mapper) |> Enum.join(joiner)` => `lhs |> Enum.map_join(joiner, mapper)`
- `lhs |> Enum.map(mapper) |> Enum.into(empty_map)` => `lhs |> Map.new(mapper)`
- `lhs |> Enum.map(mapper) |> Enum.into(collectable)` => `lhs |> Enum.into(collectable, mapper)`
- `lhs |> Enum.into(%{}, ...) => lhs |> Map.new(...)`
- `lhs |> Enum.map(mapper) |> Map.new()` => `lhs |> Map.new(mapper)` mapset & keyword also
- `lhs |> Map.merge(%{key: value}) => lhs |> Map.put(key, value)` (keyword literal also)

## Unpiping Single Pipes

- notably, optimizations might turn a 2 pipe into a single pipe
- doesn't unpipe when we're starting w/ quote
- pretty straight forward i daresay
52 changes: 52 additions & 0 deletions docs/single_node.cheatmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Single Node Styles

## Elixir Deprecation Rewrites

1.15+

- Logger.warn -> Logger.warning
- Path.safe_relative_to/2 => Path.safe_relative/2
- Enum/String.slice/2 w/ ranges -> explicit steps
- ~R/my_regex/ -> ~r/my_regex/
- Date.range/2 -> Date.range/3 when decreasing range
- IO.read/bin_read -> use `:eof` instead of `:all`
1.16+
- File.stream!(file, options, line_or_bytes) => File.stream!(file, line_or_bytes, options)

## Literal Rewrites

- string sigils
- large base 10 numbers

## Function Optimizations

These apply to the piped versions as well

- Enum.into(%{}/Map/Keyword/MapSet.new) -> X.new
- Map/Keyword.merge w/ single key literal -> X.put
- Enum.reverse(foo) ++ bar -> Enum.reverse(foo, bar)

## Function Readability

- Timex.now/0 -> DateTime.utc_now/0
- DateModule.compare(x, y) == :lt/:gt -> DateModule.before?/after?

## Code Readability
- put matches on right
- `Credo.Check.Readability.PreferImplicitTry`

## Consistency
- `def foo()` -> `def foo`

## Credo Rules This Hit

* Credo.Check.Consistency.ParameterPatternMatching
* Credo.Check.Readability.LargeNumbers
* Credo.Check.Readability.ParenthesesOnZeroArityDefs
* Credo.Check.Readability.PreferImplicitTry
* Credo.Check.Readability.StringSigils
* Credo.Check.Readability.WithSingleClause
* Credo.Check.Refactor.CaseTrivialMatches
* Credo.Check.Refactor.CondStatements
* Credo.Check.Refactor.RedundantWithClauseResult
* Credo.Check.Refactor.WithClauses
1 change: 0 additions & 1 deletion lib/style/deprecations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ defmodule Styler.Style.Deprecations do
do: {{:., dm, [{:__aliases__, am, [:Logger]}, :warning]}, funm, args}

# Path.safe_relative_to/2 => Path.safe_relative/2
# Path.safe_relative/2 is available since v1.14
# TODO: Remove after Elixir v1.19
defp style({{:., dm, [{_, _, [:Path]} = mod, :safe_relative_to]}, funm, args}),
do: {{:., dm, [mod, :safe_relative]}, funm, args}
Expand Down
4 changes: 2 additions & 2 deletions lib/style/pipes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ defmodule Styler.Style.Pipes do
Style.set_line({:|>, [], [lhs, rhs]}, dm[:line])
end

# lhs |> Enum.into(%{}, ...) => lhs |> Map.new(...)
# `lhs |> Enum.into(%{}, ...)`` => `lhs |> Map.new(...)``
defp fix_pipe({:|>, meta, [lhs, {{:., dm, [{_, _, [:Enum]}, :into]}, _, [collectable | rest]}]} = node) do
replacement =
case collectable do
Expand All @@ -277,7 +277,7 @@ defmodule Styler.Style.Pipes do
if replacement, do: {:|>, meta, [lhs, {replacement, dm, rest}]}, else: node
end

# `lhs |> Enum.map(mapper) |> Map.new()` => `lhs |> Map.new(mapper)
# `lhs |> Enum.map(mapper) |> Map.new()` => `lhs |> Map.new(mapper)``
defp fix_pipe(
pipe_chain(lhs, {{:., _, [{_, _, [enum]}, :map]}, _, [mapper]}, {{:., _, [{_, _, [mod]}, :new]} = new, nm, []})
)
Expand Down
22 changes: 22 additions & 0 deletions test/style/pipes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,9 @@ defmodule Styler.Style.PipesTest do
assert_style "a |> then(&fun/1)", "fun(a)"
assert_style "a |> then(&fun(&1)) |> c", "a |> fun() |> c()"
assert_style "a |> then(&fun(&1, d)) |> c", "a |> fun(d) |> c()"
assert_style "a |> then(&M.f(&1)) |> c", "a |> M.f() |> c()"

# Doens't rewrite multiple refs / non-starting argument
assert_style "a |> then(&fun(d, &1)) |> c()"
assert_style "a |> then(&fun(&1, d, %{foo: &1})) |> c()"

Expand Down Expand Up @@ -546,6 +549,25 @@ defmodule Styler.Style.PipesTest do
)
end

test "reverse/Kernel.++" do

Check failure on line 552 in test/style/pipes_test.exs

View workflow job for this annotation

GitHub Actions / Ex1.15.7/OTP25.1.2

test simple rewrites reverse/Kernel.++ (Styler.Style.PipesTest)

Check failure on line 552 in test/style/pipes_test.exs

View workflow job for this annotation

GitHub Actions / Ex1.16.0/OTP25.1.2

test simple rewrites reverse/Kernel.++ (Styler.Style.PipesTest)
assert_style("a |> Enum.reverse(bar) |> Kernel.++(foo)")
assert_style("a |> Enum.reverse() |> Kernel.++(foo)", "Enum.reverse(a, foo)")

assert_style(
"""
a
|> Enum.reverse()
|> Kernel.++([bar, baz])
|> Enum.sum()
""",
"""
a
|> Enum.reverse([bar, baz])
|> Enum.sum()
"""
)
end

test "filter/count" do
for enum <- ~w(Enum Stream) do
assert_style(
Expand Down

0 comments on commit 367a9be

Please sign in to comment.