Skip to content

Commit

Permalink
docs and changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Nov 19, 2024
1 parent 5016027 commit 929d217
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 82 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ they can and will change without that change being reflected in Styler's semanti

* `pipes`: pipe-ifies when first arg to a function is a pipe. reach out if this happens in unstylish places in your code (Closes #133)
* `pipes`: unpiping assignments will make the assignment one-line when possible (Closes #181)
* `deprecations`: 1.18 deprecations
* `List.zip` => `Enum.zip`
* `first..last = range` => `first..last//_ = range`

### Fixes

Expand Down
48 changes: 48 additions & 0 deletions docs/deprecations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
## Elixir Deprecation Rewrites

| Before | After |
|--------|-------|
| `Logger.warn` | `Logger.warning`|
| `Path.safe_relative_to/2` | `Path.safe_relative/2`|
| `~R/my_regex/` | `~r/my_regex/`|
| `Enum/String.slice/2` with decreasing ranges | add explicit steps to the range * |
| `Date.range/2` with decreasing range | `Date.range/3` *|
| `IO.read/bin_read` with `:all` option | replace `:all` with `:eof`|

\* For both of the "decreasing range" changes, the rewrite can only be applied if the range is being passed as an argument to the function.

### 1.18 Deprecations

#### `List.zip/1`

```
# Before
List.zip(list)
# Styled
Enum.zip(list)
```

#### Range Matching Without Step

```elixir
# Before
first..last = range
# Styled
first..last//_ = range

# Before
def foo(x..y), do: :ok
# Styled
def foo(x..y//_), do: :ok
```

### 1.16+

`File.stream!/3` `:line` and `:bytes` deprecation

```elixir
# Before
File.stream!(path, [encoding: :utf8, trim_bom: true], :line)
# Styled
File.stream!(path, :line, encoding: :utf8, trim_bom: true)
```
101 changes: 19 additions & 82 deletions docs/styles.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ baz |> Enum.reverse() |> Enum.concat(bop)
Enum.reverse(baz, bop)
```

## `Timex.now/0` ->` DateTime.utc_now/0`
## `Timex.now/0` -> `DateTime.utc_now/0`

Timex certainly has its uses, but knowing what stdlib date/time struct is returned by `now/0` is a bit difficult!

Expand Down Expand Up @@ -137,43 +137,25 @@ DateTime.compare(start, end_date) == :lt
DateTime.before?(start, end_date)
```

## Implicit Try
## Implicit `try`

Styler will rewrite functions whose entire body is a try/do to instead use the implicit try syntax, per Credo's `Credo.Check.Readability.PreferImplicitTry`

The following example illustrates the most complex case, but Styler happily handles just basic try do/rescue bodies just as easily.

### Before

```elixir
def foo() do
# before
def foo do
try do
uh_oh()
rescue
exception -> {:error, exception}
throw_ball()
catch
:a_throw -> {:error, :threw!}
else
try_has_an_else_clause? -> {:did_you_know, try_has_an_else_clause?}
after
:done
:ball -> :caught
end
end
```

### After

```elixir
def foo() do
uh_oh()
rescue
exception -> {:error, exception}
# Styled:
def foo do
throw_ball()
catch
:a_throw -> {:error, :threw!}
else
try_has_an_else_clause? -> {:did_you_know, try_has_an_else_clause?}
after
:done
:ball -> :caught
end
```

Expand All @@ -183,44 +165,19 @@ The author of the library disagrees with this style convention :) BUT, the wonde

```elixir
# Before
def foo()
defp foo()
defmacro foo()
defmacrop foo()

# Styled
def foo
defp foo
defmacro foo
defmacrop foo
```

## Elixir Deprecation Rewrites

### 1.15+

| Before | After |
|--------|-------|
| `Logger.warn` | `Logger.warning`|
| `Path.safe_relative_to/2` | `Path.safe_relative/2`|
| `~R/my_regex/` | `~r/my_regex/`|
| `Enum/String.slice/2` with decreasing ranges | add explicit steps to the range * |
| `Date.range/2` with decreasing range | `Date.range/3` *|
| `IO.read/bin_read` with `:all` option | replace `:all` with `:eof`|

\* For both of the "decreasing range" changes, the rewrite can only be applied if the range is being passed as an argument to the function.

### 1.16+
File.stream! `:line` and `:bytes` deprecation
def foo() do
defp foo() do
defmacro foo() do
defmacrop foo() do

```elixir
# Before
File.stream!(path, [encoding: :utf8, trim_bom: true], :line)
# Styled
File.stream!(path, :line, encoding: :utf8, trim_bom: true)
def foo do
defp foo do
defmacro foo do
defmacrop foo do
```

## Putting variable matching on the right
## Variable matching on the right

```elixir
# Before
Expand Down Expand Up @@ -263,26 +220,6 @@ case foo do
end
```

## Use Implicit Try

```elixir
# before
def foo d
try do
throw_ball()
catch
:ball -> :caught
end
end

# Styled:
def foo d
throw_ball()
catch
:ball -> :caught
end
```

## Shrink Function Definitions to One Line When Possible

```elixir
Expand Down

0 comments on commit 929d217

Please sign in to comment.