Skip to content

Commit defc6c0

Browse files
remotecomnovaugustktekinaytfiedlerdejanzehalfdan
authored
v1.4.2: Upstream merge (#8)
* ignore no-paren, no-body function heads. closes adobe#185 * minor optimization * Wrap up 1.0.0 docs overhaul * 1.0.0 * Add missing documentation. Closes adobe#188 * update readme link * add more verbiage re styler can add bugs. Closes adobe#186 * reword warning to list examples of breakages * Update styles.md Fixed example for "drop superfluous _..." * Add Stream.run optimizations, fix optimizations shrinking pipes to one line (Closes adobe#180) * fix infinite loop rewriting negated if with empty do body. closes adobe#196 * Remove unless from codebases (adobe#194) * use `not` over `!` when rewriting `unless a (> >= < <= in) b` * actually, lets only do `not` for `in` * v1.1.0 * update version in readme * Don't pipe into `Kernel.!` when rewriting unless with pipes * configs: improve comment handling when moving a small number of nodes. Closes adobe#187 * v1.1.2 * pipes: improve comment behaviour in optimizations (Closes adobe#176) * One-line unpiped assignments (adobe#197) Closes adobe#181 * Pipify: `d(a |> b |> c)` => `a |> b() |> c() |> d()`(adobe#198) Closes adobe#133 * 1.18 hard deprecations (adobe#203) * `List.zip` => `Enum.zip` * `first..last = range` => `first..last//_ = range` * docs and changelog * docs prep for 1.2 * v1.2.0 * correct docs on Enum.into * Fix pipifying pipes-in-pipes (Closes adobe#204) * v1.2.1 * introduce `# styler:sort` comment directive (adobe#205) closes adobe#167 * styler:sort - handle tuples * mix format :X * style:sort - dont dedupe * docs and fix a typo * fiddle with private apis to ease iex playing * remove Zipper.reduce/3 - bugged, but more importantly unused * Maintain comment-node relations when applying `#styler:sort` directive (adobe#207) Co-authored-by: Greg Mefford <gmefford@adobe.com> Closes adobe#167 * v1.3.0 * correct changelog * fix twople bug in sort directive, add map sorting * v1.3.1 * defstruct with list literal * ci: update elixir and erlang versions * sort directive: sort values of keys. Closes adobe#208 * v1.3.2 * fix comments bug in styler:sort directive * styler:sort arbitrary ast within do end blocks * improve `with` statement replacements * v1.3.3 * alias lifting: shrink when alias already exists. closes adobe#201 * alias lifting: be better about conflicts. Closes adobe#193 * improve alias lift collision case * remove vestigial with rewriting head * pipes: handle pipifying functions whose first arg is itself a pipe. closes adobe#193 * cleanup the messes left in the previous commit 🙈 * correct issue number in change log * test against 1.18 * 1.18 warnings + formatting * no one saw that right? * ex1.17+: replace `:timer.units(x)` with the new `to_timeout(unit: x)` for `hours|minutes|seconds` Closes adobe#218 * 1.18+: change struct updates to map updates. Closes adobe#199 * ensure test works across versions * fix `with` rewrites when keyword with an else stab (adobe#220) Closes adobe#219 fixes for both <1.17 and >=1.17 * pipify nested function calls with pipe as the first argument. closes adobe#216 * change struct update deprecation to ex1.19+ * docs docs docs docs docs! * ship struct update to map update changes after all * v1.4.0 * Add OTP26/27 but only run for 1.17/1.18 * fix `with` redundant body + non-arrow behind redundant clause. Closes adobe#221 * link to quokka * rewrite `to_timeout(unit: x * m)` to use larger units in some cases * defs test describe formatting * dont crash on invalid defs * fix CI for older elixir * v1.4.1 * if: drop empty do bodies. Closes adobe#227 * Fix large comment block mangling bug when ordering sibling AST (adobe#232) Closes adobe#230 * if: treat is_nil as a negator * Revert "if: treat is_nil as a negator" This reverts commit 50ae386. * Revert "if: drop empty do bodies. Closes adobe#227" This reverts commit 6b42462. * v1.4.2 * changelog: fix GH md formatting issues * optimize zipper performance * We still don't want moduledoc false in modules without it * Remove missed conflict --------- Co-authored-by: Matt Enlow <enlow@adobe.com> Co-authored-by: Kem Tekinay <ktekinay@mactechnologies.com> Co-authored-by: Matt Enlow <matt@novaugust.net> Co-authored-by: Theodor Fiedler <theo.fie@gmail.com> Co-authored-by: Fabian Becker <fbecker@adobe.com> Co-authored-by: Jesse Herrick <jesse@remote.com>
1 parent 1375747 commit defc6c0

32 files changed

Lines changed: 2554 additions & 744 deletions

.github/workflows/ci.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ jobs:
99
name: Ex${{matrix.elixir}}/OTP${{matrix.otp}}
1010
strategy:
1111
matrix:
12-
elixir: ['1.15.7', '1.16.0', '1.17.0-rc.0']
13-
otp: ['25.1.2']
12+
elixir: ["1.15.8", "1.16.3", "1.17.3", "1.18.2"]
13+
otp: ["25.3.2", "26.2.5", "27.2.4"]
14+
exclude:
15+
- elixir: "1.15.8"
16+
otp: "27.2.4"
17+
- elixir: "1.16.3"
18+
otp: "27.2.4"
1419
steps:
1520
- uses: actions/checkout@v4
1621
- uses: erlef/setup-beam@v1

.tool-versions

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
erlang 26.1.2
2-
elixir 1.16.0-otp-26
1+
elixir 1.18.2-otp-27
2+
erlang 27.2.3

CHANGELOG.md

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,231 @@
44
they can and will change without that change being reflected in Styler's semantic version.
55
## main
66

7+
## 1.4.2
8+
9+
### Fixes
10+
11+
- fix comment misplacement for large comment blocks in config files and `# styler:sort` (#230, h/t @cschmatzler)
12+
13+
## 1.4.1
14+
15+
### Improvements
16+
17+
- `to_timeout/1` rewrites to use the next largest unit in some simple instances
18+
19+
```elixir
20+
# before
21+
to_timeout(second: 60 * m)
22+
to_timeout(day: 7)
23+
# after
24+
to_timeout(minute: m)
25+
to_timeout(week: 1)
26+
```
27+
28+
### Fixes
29+
30+
- fixed styler raising when encountering invalid function definition ast
31+
32+
## 1.4.0
33+
34+
- A very nice change in alias lifting means Styler will make sure that your code is _using_ the aliases that it's specified.
35+
- Shoutout to the smartrent folks for finding pipifying recursion issues
36+
- Elixir 1.17 improvements and fixes
37+
- Elixir 1.19-dev: delete struct updates
38+
39+
Read on for details.
40+
41+
### Improvements
42+
43+
#### Alias Lifting
44+
45+
This release taught Styler to try just that little bit harder when doing alias lifting.
46+
47+
- general improvements around conflict detection, lifting in more correct places and fewer incorrect places (#193, h/t @jsw800)
48+
- use knowledge of existing aliases to shorten invocations (#201, h/t me)
49+
50+
example:
51+
52+
alias A.B.C
53+
54+
A.B.C.foo()
55+
A.B.C.bar()
56+
A.B.C.baz()
57+
58+
becomes:
59+
60+
alias A.B.C
61+
62+
C.foo()
63+
C.bar()
64+
C.baz()
65+
66+
#### Struct Updates => Map Updates
67+
68+
1.19 deprecates struct update syntax in favor of map update syntax.
69+
70+
```elixir
71+
# This
72+
%Struct{x | y}
73+
# Styles to this
74+
%{x | y}
75+
```
76+
77+
**WARNING** Double check your diffs to make sure your variable is pattern matching against the same struct if you want to harness 1.19's type checking features. Apologies to folks who hoped Styler would do this step for you <3 (#199, h/t @SteffenDE)
78+
79+
#### Ex1.17+
80+
81+
- Replace `:timer.units(x)` with the new `to_timeout(unit: x)` for `hours|minutes|seconds` (This style is only applied if you're on 1.17+)
82+
83+
### Fixes
84+
85+
- `pipes`: handle pipifying when the first arg is itself a pipe: `c(a |> b, d)` => `a |> b() |> c(d)` (#214, h/t @kybishop)
86+
- `pipes`: handle pipifying nested functions `d(c(a |> b))` => `a |> b |> c() |> d` (#216, h/t @emkguts)
87+
- `with`: fix a stabby `with` `, else: (_ -> :ok)` being rewritten to a case (#219, h/t @iamhassangm)
88+
89+
## 1.3.3
90+
91+
### Improvements
92+
93+
- `with do: body` and variations with no arrows in the head will be rewritten to just `body`
94+
- `# styler:sort` will sort arbitrary ast nodes within a `do end` block:
95+
96+
Given:
97+
98+
# styler:sort
99+
my_macro "some arg" do
100+
another_macro :q
101+
another_macro :w
102+
another_macro :e
103+
another_macro :r
104+
another_macro :t
105+
another_macro :y
106+
end
107+
108+
We get
109+
110+
# styler:sort
111+
my_macro "some arg" do
112+
another_macro :e
113+
another_macro :q
114+
another_macro :r
115+
another_macro :t
116+
another_macro :w
117+
another_macro :y
118+
end
119+
120+
### Fixes
121+
122+
- fix a bug in comment-movement when multiple `# styler:sort` directives are added to a file at the same time
123+
124+
## 1.3.2
125+
126+
### Improvements
127+
128+
- `# styler:sort` can be used to sort values of key-value pairs. eg, sort the value of a single key in a map (Closes #208, h/t @ypconstante)
129+
130+
## 1.3.1
131+
132+
### Improvements
133+
134+
- `# styler:sort` now works with maps and the `defstruct` macro
135+
136+
### Fixes
137+
138+
- `# styler:sort` no longer blows up on keyword lists :X
139+
140+
## 1.3.0
141+
142+
### Improvements
143+
144+
#### `# styler:sort` Styler's first comment directive
145+
146+
Styler will now keep a user-designated list or wordlist (`~w` sigil) sorted as part of formatting via the use of comments. Elements of the list are sorted by their string representation.
147+
148+
The intention is to remove comments to humans, like `# Please keep this list sorted!`, in favor of comments to robots: `# styler:sort`. Personally speaking, Styler is much better at alphabetical-order than I ever will be.
149+
150+
To use the new directive, put it on the line before a list or wordlist.
151+
152+
This example:
153+
154+
```elixir
155+
# styler:sort
156+
[:c, :a, :b]
157+
158+
# styler:sort
159+
~w(a list of words)
160+
161+
# styler:sort
162+
@country_codes ~w(
163+
en_US
164+
po_PO
165+
fr_CA
166+
ja_JP
167+
)
168+
169+
# styler:sort
170+
a_var =
171+
[
172+
Modules,
173+
In,
174+
A,
175+
List
176+
]
177+
```
178+
179+
Would yield:
180+
181+
```elixir
182+
# styler:sort
183+
[:a, :b, :c]
184+
185+
# styler:sort
186+
~w(a list of words)
187+
188+
# styler:sort
189+
@country_codes ~w(
190+
en_US
191+
fr_CA
192+
ja_JP
193+
po_PO
194+
)
195+
196+
# styler:sort
197+
a_var =
198+
[
199+
A,
200+
In,
201+
List,
202+
Modules
203+
]
204+
```
205+
206+
## 1.2.1
207+
208+
### Fixes
209+
210+
* `|>` don't pipify when the call is itself in a pipe (aka don't touch `a |> b(c |> d() |>e()) |> f()`) (Closes #204, h/t @paulswartz)
211+
212+
## 1.2.0
213+
214+
### Improvements
215+
216+
* `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)
217+
* `pipes`: unpiping assignments will make the assignment one-line when possible (Closes #181)
218+
* `deprecations`: 1.18 deprecations
219+
* `List.zip` => `Enum.zip`
220+
* `first..last = range` => `first..last//_ = range`
221+
222+
### Fixes
223+
224+
* `pipes`: optimizations are less likely to move comments (Closes #176)
225+
226+
## 1.1.2
227+
228+
### Improvements
229+
230+
* Config Sorting: improve comment handling when only sorting a few nodes (Closes #187)
231+
7232
## 1.1.1
8233

9234
### Improvements

README.md

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@ You can learn more about the history, purpose and implementation of Styler from
1111

1212
## Features
1313

14-
- auto-fixes [many credo rules](docs/credo.md), meaning you can turn them off to speed credo up
15-
- [keeps a strict module layout](docs/module_directives.md#directive-organization)
16-
- alphabetizes module directives
17-
- [extracts repeated aliases](docs/module_directives.md#alias-lifting)
18-
- [makes your pipe chains pretty as can be](docs/pipes.md)
19-
- pipes and unpipes function calls based on the number of calls
20-
- optimizes standard library calls (`a |> Enum.map(m) |> Enum.into(Map.new)` => `Map.new(a, m)`)
21-
- replaces strings with sigils when the string has many escaped quotes
22-
- ... and so much more
23-
24-
[See our Rewrites documentation on hexdocs](https://hexdocs.pm/styler/styles.html)
14+
Styler fixes a plethora of elixir style and optimization issues automatically as part of mix format.
15+
16+
[See Styler's documentation on Hex](https://hexdocs.pm/styler/styles.html) for the comprehensive list of its features.
17+
18+
The fastest way to see what all it can do you for you is to just try it out in your codebase... but here's a list of a few features to help you decide if you're interested in Styler:
19+
20+
- sorts and organizes `import`/`alias`/`require` and other [module directives](docs/module_directives.md)
21+
- keeps lists, sigils, and even arbitrary code sorted with the `# styler:sort` [comment directive](docs/comment_directives.md)
22+
- automatically creates aliases for repeatedly referenced modules names ([_"alias lifting"_](docs/module_directives.md#alias-lifting))
23+
- optimizes pipe chains for [readability and performance](docs/pipes.md)
24+
- rewrites strings as sigils when it results in fewer escapes
25+
- auto-fixes [many credo rules](docs/credo.md), meaning you can spend less time fighting with CI
2526

2627
## Who is Styler for?
2728

@@ -41,7 +42,7 @@ Add `:styler` as a dependency to your project's `mix.exs`:
4142
```elixir
4243
def deps do
4344
[
44-
{:styler, "~> 1.1", only: [:dev, :test], runtime: false},
45+
{:styler, "~> 1.4", only: [:dev, :test], runtime: false},
4546
]
4647
end
4748
```
@@ -75,10 +76,54 @@ Styler's only current configuration option is `:alias_lifting_exclude`, which ac
7576

7677
#### No Credo-Style Enable/Disable
7778

78-
Styler [will not add configuration](https://github.com/adobe/elixir-styler/pull/127#issuecomment-1912242143) for ad-hoc enabling/disabling of rewrites. Sorry! Its implementation simply does not support that approach. There are however many forks out there that have attempted this; please explore the [Github forks tab](https://github.com/adobe/elixir-styler/forks) to see if there's a project that suits your needs or that you can draw inspiration from.
79+
Styler [will not add configuration](https://github.com/adobe/elixir-styler/pull/127#issuecomment-1912242143) for ad-hoc enabling/disabling of rewrites. Sorry!
80+
81+
However, Smartrent has a fork of Styler named [Quokka](https://github.com/smartrent/quokka) that allows for finegrained control of Styler. Maybe it's what you're looking for. If not, you can always fork it or Styler as a starting point for your own tool!
7982

8083
Ultimately Styler is @adobe's internal tool that we're happy to share with the world. We're delighted if you like it as is, and just as excited if it's a starting point for you to make something even better for yourself.
8184

85+
## WARNING: Styler can change the behaviour of your program!
86+
87+
In some cases, this can introduce bugs. It goes without saying, but look over your changes before committing to main :)
88+
89+
A simple example of a way Styler changes the behaviour of code is the following rewrite:
90+
91+
```elixir
92+
# Before: this case statement...
93+
case foo do
94+
true -> :ok
95+
false -> :error
96+
end
97+
98+
# After: ... is rewritten by Styler to be an if statement!.
99+
if foo do
100+
:ok
101+
else
102+
:error
103+
end
104+
```
105+
106+
These programs are not semantically equivalent. The former would raise if `foo` returned any value other than `true` or `false`, while the latter blissfully completes.
107+
108+
However, Styler is about _style_, and the `if` statement is (in our opinion) of much better style. If the exception behaviour was intentional on the code author's part, they should have written the program like this:
109+
110+
```elixir
111+
case foo do
112+
true -> :ok
113+
false -> :error
114+
other -> raise "expected `true` or `false`, got: #{inspect other}"
115+
end
116+
```
117+
118+
Also good style! But Styler assumes that most of the time people just meant the `if` equivalent of the code, and so makes that change. If issues like this bother you, Styler probably isn't the tool you're looking for.
119+
120+
Other ways Styler can change your program:
121+
122+
- [`with` statement rewrites](https://github.com/adobe/elixir-styler/issues/186)
123+
- [config file sorting](https://hexdocs.pm/styler/mix_configs.html#this-can-break-your-program)
124+
- and likely other ways. stay safe out there!
125+
126+
>>>>>>> upstream/main
82127
## Thanks & Inspiration
83128

84129
### [Sourceror](https://github.com/doorgan/sourceror/)

0 commit comments

Comments
 (0)