-
Notifications
You must be signed in to change notification settings - Fork 23
Add PipeChainStart task #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
8d1ee1c
Changes for rewrite 1.0.0 (#96)
NickNeck 3d8ae5c
Read inputs from .formatter.exs (#98)
NickNeck f9f5027
Add recode.issues manifest (#99)
NickNeck 67a34b9
Update manifest handling
NickNeck 6c57bdd
Create issues for unformatted files in dry mode
NickNeck 591d9e5
Add minor refactorings
NickNeck 4e5f477
Add silent mode (#100)
NickNeck 39d596f
Fix LocalsWtihoutParens for multiline calls
NickNeck 8a3e6cf
Fix LocalsWtihoutParens in pipes
NickNeck e2828e4
Run mix format
NickNeck 9325dca
Update test
NickNeck 032566b
Changes for rewrite 1.0.0 (#96)
NickNeck 0ab693a
Add recode.issues manifest (#99)
NickNeck be51cdb
Create issues for unformatted files in dry mode
NickNeck 76d35fe
Add task PipeChainStart
NickNeck 313cfe8
Update .credo.exs
NickNeck bba5e28
Clean up
NickNeck 58e2e4c
Run mix format
NickNeck 2393c55
Set fail-fast to false
NickNeck 938ad79
Fix test
NickNeck df2edee
rm scripts
NickNeck 8fb1db8
update ci
NickNeck 5474d13
Fix filter_count
NickNeck 0771923
Add docs
NickNeck 3bf5df0
Fix typo
NickNeck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,261 @@ | ||
| defmodule Recode.Task.PipeChainStart do | ||
| @shortdoc "Checks if a pipe chain starts with a raw value." | ||
|
|
||
| @moduledoc """ | ||
| Pipes should start with a raw value to improve readability. | ||
|
|
||
| # preferred | ||
| user | ||
| |> User.changeset(params) | ||
| |> Repo.insert() | ||
|
|
||
| # not preferred | ||
| User.changeset(user, params) | ||
| |> Repo.insert() | ||
|
|
||
| Starting a pipe chain with a function call instead of a raw value can make | ||
| the code harder to follow, as the data flow is less clear. | ||
|
|
||
| This task rewrites the code when `mix recode` runs with `autocorrect: true`. | ||
| """ | ||
|
|
||
| use Recode.Task, corrector: true, category: :readability | ||
|
|
||
| alias Recode.AST | ||
| alias Rewrite.Source | ||
| alias Sourceror.Zipper | ||
|
|
||
| @sigils [ | ||
| :sigil_C, | ||
| :sigil_c, | ||
| :sigil_D, | ||
| :sigil_N, | ||
| :sigil_r, | ||
| :sigil_S, | ||
| :sigil_s, | ||
| :sigil_T, | ||
| :sigil_U, | ||
| :sigil_W, | ||
| :sigil_w | ||
| ] | ||
|
|
||
| @unary_ops [ | ||
| :!, | ||
| :"~~~", | ||
| :&, | ||
| :+, | ||
| :-, | ||
| :@, | ||
| :^, | ||
| :not | ||
| ] | ||
|
|
||
| @binary_ops [ | ||
| :!=, | ||
| :!==, | ||
| :"//", | ||
| :"::", | ||
| :"<|>", | ||
| :"^^^", | ||
| :&&&, | ||
| :&&, | ||
| :**, | ||
| :*, | ||
| :+++, | ||
| :++, | ||
| :+, | ||
| :-, | ||
| :--, | ||
| :---, | ||
| :., | ||
| :.., | ||
| :..//, | ||
| :/, | ||
| :<, | ||
| :<-, | ||
| :<<<, | ||
| :<<~, | ||
| :<=, | ||
| :<>, | ||
| :<~, | ||
| :<~>, | ||
| :=, | ||
| :==, | ||
| :===, | ||
| :=~, | ||
| :>, | ||
| :>=, | ||
| :>>>, | ||
| :\\, | ||
| :and, | ||
| :in, | ||
| :or, | ||
| :when, | ||
| :|, | ||
| :|>, | ||
| :||, | ||
| :|||, | ||
| :~>, | ||
| :~>> | ||
| ] | ||
|
|
||
| @special_forms [ | ||
| :<<>>, | ||
| :__block__, | ||
| :case, | ||
| :cond, | ||
| :fn, | ||
| :for, | ||
| :if, | ||
| :unquote, | ||
| :unquote_splicing, | ||
| :with | ||
| ] | ||
|
|
||
| @exclude_functions Enum.concat([@special_forms, @sigils, @unary_ops, @binary_ops]) | ||
|
|
||
| @impl Recode.Task | ||
| def run(source, opts) do | ||
| source | ||
| |> Source.get(:quoted) | ||
| |> Zipper.zip() | ||
| |> Zipper.traverse([], fn zipper, issues -> | ||
| pipe_chain_start(zipper, issues, opts[:autocorrect], opts) | ||
| end) | ||
| |> update(source, opts) | ||
| end | ||
|
|
||
| defp update({zipper, issues}, source, opts) do | ||
| update_source(source, opts, quoted: zipper, issues: issues) | ||
| end | ||
|
|
||
| @impl Recode.Task | ||
| def init(config) do | ||
| config = | ||
| config | ||
| |> Keyword.put_new(:exclude_functions, []) | ||
| |> Keyword.put_new(:exclude_arguments, []) | ||
|
|
||
| {:ok, config} | ||
| end | ||
|
|
||
| # inside pipe | ||
| defp pipe_chain_start( | ||
| %Zipper{node: {:|>, _, [{:|>, _, _} | _]}} = zipper, | ||
| issues, | ||
| _autocorrect, | ||
| _opts | ||
| ) do | ||
| {zipper, issues} | ||
| end | ||
|
|
||
| # pipe start | ||
| defp pipe_chain_start( | ||
| %Zipper{node: {:|>, meta, [lhs, rhs]}} = zipper, | ||
| issues, | ||
| autocorrect, | ||
| opts | ||
| ) do | ||
| case check(lhs, opts) do | ||
| :ok -> | ||
| {zipper, issues} | ||
|
|
||
| {:error, correction} when autocorrect -> | ||
| {correct(zipper, correction, rhs), issues} | ||
|
|
||
| {:error, _correction} when not autocorrect -> | ||
| {zipper, add_issue(issues, meta)} | ||
| end | ||
| end | ||
|
|
||
| defp pipe_chain_start(zipper, issues, _autocorrect, _opts) do | ||
| {zipper, issues} | ||
| end | ||
|
|
||
| defp check( | ||
| {{:., _meta1, [{:__aliases__, _meta2, _aliases2}, _fun]} = form, meta, [arg | rest]}, | ||
| opts | ||
| ) do | ||
| mf = AST.mf(form) | ||
|
|
||
| with :error <- check(mf, meta, arg, opts) do | ||
| correction(arg, form, rest) | ||
| end | ||
| end | ||
|
|
||
| defp check({{:., _meta1, [Access, :get]}, _meta, _args}, _opts) do | ||
| :ok | ||
| end | ||
|
|
||
| defp check({{:., _meta1, [_arg1 | _rest1]} = form, meta, [arg | rest]}, opts) do | ||
| with :error <- check(:., meta, arg, opts) do | ||
| correction(arg, form, rest) | ||
| end | ||
| end | ||
|
|
||
| defp check({form, meta, [arg | rest]}, opts) | ||
| when is_atom(form) and form not in @exclude_functions do | ||
| with :error <- check(form, meta, arg, opts) do | ||
| correction(arg, form, rest) | ||
| end | ||
| end | ||
|
|
||
| defp check(_ast, _opts), do: :ok | ||
|
|
||
| defp check(form, meta, arg, opts) do | ||
| with :error <- exclude_function(form, opts[:exclude_functions]), | ||
| :error <- exclude_argument(arg, opts[:exclude_arguments]) do | ||
| custom_sigil(form, meta[:delimiter]) | ||
| end | ||
| end | ||
|
|
||
| defp correction(arg, form, rest) do | ||
| {:error, {:|>, [], [arg, {form, [], rest}]}} | ||
| end | ||
|
|
||
| defp custom_sigil(_atom, nil), do: :error | ||
|
|
||
| defp custom_sigil(atom, _delimeter) do | ||
| sigil? = atom |> to_string() |> String.starts_with?("sigil_") | ||
|
|
||
| if sigil?, do: :ok, else: :error | ||
| end | ||
|
|
||
| defp correct(zipper, lhs, rhs) do | ||
| Zipper.replace(zipper, {:|>, [], [lhs, rhs]}) | ||
| end | ||
|
|
||
| defp add_issue(issues, meta) do | ||
| message = "Pipe chain should start with a raw value." | ||
| [new_issue(message, meta) | issues] | ||
| end | ||
|
|
||
| defp exclude_function(_fun, []), do: :error | ||
|
|
||
| defp exclude_function({module, fun}, exclude) do | ||
| if {module, fun} in exclude or {module, :*} in exclude, do: :ok, else: :error | ||
| end | ||
|
|
||
| defp exclude_function(fun, exclude) do | ||
| if fun in exclude, do: :ok, else: :error | ||
| end | ||
|
|
||
| defp exclude_argument({:__block__, _meta, [literal]}, _include) | ||
| when is_atom(literal) or is_number(literal) or is_binary(literal) or is_list(literal) do | ||
| :error | ||
| end | ||
|
|
||
| defp exclude_argument({form, _meta, nil}, _include) when is_atom(form) do | ||
| :error | ||
| end | ||
|
|
||
| defp exclude_argument({{:., _meta1, _args1}, _meta2, _args2}, _include) do | ||
| :error | ||
| end | ||
|
|
||
| defp exclude_argument({form, _meta, _}, exclude) do | ||
| if form in exclude, do: :ok, else: :error | ||
| end | ||
|
|
||
| defp exclude_argument(_arg, _include), do: :ok | ||
| end | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pass an AST to update_source/3, not a zipper.
Zipper.traverse/3 returns a zipper; update_source/3 typically expects the updated quoted AST. Convert the zipper back to the root node before updating.
Apply:
🤖 Prompt for AI Agents