Skip to content

Commit

Permalink
change autocorrect to autofix
Browse files Browse the repository at this point in the history
  • Loading branch information
devonremote committed Jul 29, 2022
1 parent acd96bb commit 4bb4095
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 60 deletions.
4 changes: 2 additions & 2 deletions lib/credo/check.ex
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ defmodule Credo.Check do
Credo.Issue.t()

@doc false
@callback autocorrect(source_file :: String.t(), issue :: Issue.t()) :: String.t()
@callback autofix(source_file :: String.t(), issue :: Issue.t()) :: String.t()

@base_category_exit_status_map %{
consistency: 1,
Expand Down Expand Up @@ -400,7 +400,7 @@ defmodule Credo.Check do

@doc false
@impl true
def autocorrect(source_file, _issue) do
def autofix(source_file, _issue) do
source_file
end

Expand Down
10 changes: 5 additions & 5 deletions lib/credo/check/consistency/line_endings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ defmodule Credo.Check.Consistency.LineEndings do
"File is using unix line endings while most of the files use windows line endings."
end

def autocorrect(file, issue) do
def autofix(file, issue) do
if issue.message == message_for(:windows) do
do_autocorrect(file, :unix_to_windows)
do_autofix(file, :unix_to_windows)
else
do_autocorrect(file, :windows_to_unix)
do_autofix(file, :windows_to_unix)
end
end

defp do_autocorrect(file, :windows_to_unix), do: String.replace(file, "\r\n", "\n")
defp do_autocorrect(file, :unix_to_windows), do: String.replace(file, "\n", "\r\n")
defp do_autofix(file, :windows_to_unix), do: String.replace(file, "\r\n", "\n")
defp do_autofix(file, :unix_to_windows), do: String.replace(file, "\n", "\r\n")
end
8 changes: 4 additions & 4 deletions lib/credo/check/readability/alias_order.ex
Original file line number Diff line number Diff line change
Expand Up @@ -224,20 +224,20 @@ defmodule Credo.Check.Readability.AliasOrder do
)
end

def autocorrect(file, _issue) do
def autofix(file, _issue) do
{:ok, quoted} = :"Elixir.Code".string_to_quoted(file)

modified =
quoted
|> Macro.prewalk(&do_autocorrect/1)
|> Macro.prewalk(&do_autofix/1)
|> Macro.to_string()
|> :"Elixir.Code".format_string!()
|> to_string()

"#{modified}\n"
end

defp do_autocorrect({:__block__ = op, meta, [{:alias, _, _} | _] = aliases}) do
defp do_autofix({:__block__ = op, meta, [{:alias, _, _} | _] = aliases}) do
modified =
aliases
|> group_aliases()
Expand All @@ -253,7 +253,7 @@ defmodule Credo.Check.Readability.AliasOrder do
{op, Keyword.delete(meta, :line), modified}
end

defp do_autocorrect(ast), do: ast
defp do_autofix(ast), do: ast

defp put_line_number([{op, meta, args} | tail], line) do
modified = {op, Keyword.put(meta, :line, line), args}
Expand Down
2 changes: 1 addition & 1 deletion lib/credo/check/readability/trailing_blank_line.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ defmodule Credo.Check.Readability.TrailingBlankLine do
)
end

def autocorrect(file, _issue) do
def autofix(file, _issue) do
"#{file}\n"
end
end
8 changes: 4 additions & 4 deletions lib/credo/check/refactor/unless_with_else.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,22 @@ defmodule Credo.Check.Refactor.UnlessWithElse do
)
end

def autocorrect(file, _issue) do
def autofix(file, _issue) do
{:ok, quoted} = :"Elixir.Code".string_to_quoted(file)

modified =
quoted
|> Macro.prewalk(&do_autocorrect/1)
|> Macro.prewalk(&do_autofix/1)
|> Macro.to_string()
|> :"Elixir.Code".format_string!()
|> to_string()

"#{modified}\n"
end

defp do_autocorrect({:unless, meta, [clause, [do: falsy, else: truthy]]}) do
defp do_autofix({:unless, meta, [clause, [do: falsy, else: truthy]]}) do
{:if, meta, [clause, [do: truthy, else: falsy]]}
end

defp do_autocorrect(ast), do: ast
defp do_autofix(ast), do: ast
end
14 changes: 7 additions & 7 deletions lib/credo/check/warning/unused_string_operation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ defmodule Credo.Check.Warning.UnusedStringOperation do
)
end

def autocorrect(file, _issue) do
def autofix(file, _issue) do
{_, quoted} = Credo.Code.ast(file)
source_file = SourceFile.parse(file, "nofile")

Expand All @@ -60,15 +60,15 @@ defmodule Credo.Check.Warning.UnusedStringOperation do

modified =
quoted
|> Macro.prewalk(&do_autocorrect(&1, unused_calls))
|> Macro.prewalk(&do_autofix(&1, unused_calls))
|> Macro.to_string()
|> :"Elixir.Code".format_string!()
|> to_string()

"#{modified}\n"
end

defp do_autocorrect({:__block__, meta, [{:|>, _pipe_meta, pipe_args} | tail] = args}, unused_calls) do
defp do_autofix({:__block__, meta, [{:|>, _pipe_meta, pipe_args} | tail] = args}, unused_calls) do
args =
if List.last(pipe_args) in unused_calls do
[hd(pipe_args) | tail]
Expand All @@ -79,13 +79,13 @@ defmodule Credo.Check.Warning.UnusedStringOperation do
{:__block__, meta, args}
end

defp do_autocorrect({:__block__, meta, args}, unused_calls) do
defp do_autofix({:__block__, meta, args}, unused_calls) do
{:__block__, meta, Enum.reject(args, & &1 in unused_calls)}
end

defp do_autocorrect({:do, node}, unused_calls) do
{:do, do_autocorrect(node, unused_calls)}
defp do_autofix({:do, node}, unused_calls) do
{:do, do_autofix(node, unused_calls)}
end

defp do_autocorrect(ast, _), do: ast
defp do_autofix(ast, _), do: ast
end
4 changes: 2 additions & 2 deletions lib/credo/cli/command/suggest/suggest_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defmodule Credo.CLI.Command.Suggest.SuggestCommand do
Switch.boolean("read_from_stdin"),
Switch.boolean("strict"),
Switch.boolean("verbose"),
Switch.boolean("autocorrect"),
Switch.boolean("autofix"),
Switch.boolean("watch")
]

Expand All @@ -42,7 +42,7 @@ defmodule Credo.CLI.Command.Suggest.SuggestCommand do
print_before_analysis: [__MODULE__.PrintBeforeInfo],
run_analysis: [Task.RunChecks],
filter_issues: [Task.SetRelevantIssues],
run_autocorrect: [Task.RunAutocorrect],
run_autofix: [Task.RunAutofix],
print_after_analysis: [__MODULE__.PrintResultsAndSummary]
)
end
Expand Down
10 changes: 5 additions & 5 deletions lib/credo/cli/task/run_autocorrect.ex
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
defmodule Credo.CLI.Task.RunAutocorrect do
defmodule Credo.CLI.Task.RunAutofix do
@moduledoc false

use Credo.Execution.Task

def call(exec, opts, read_fun \\ &File.read!/1, write_fun \\ &File.write!/2) do
if exec.autocorrect do
if exec.autofix do
issues = Keyword.get_lazy(opts, :issues, fn -> Execution.get_issues(exec) end)

issues
|> group_by_file
|> Enum.each(fn {file_path, issues} ->
file = read_fun.(file_path)

corrected = Enum.reduce(issues, file, &run_autocorrect(&1, &2, exec))
corrected = Enum.reduce(issues, file, &run_autofix(&1, &2, exec))

write_fun.(file_path, corrected)
end)
Expand All @@ -27,8 +27,8 @@ defmodule Credo.CLI.Task.RunAutocorrect do
end)
end

defp run_autocorrect(issue, file, exec) do
case issue.check.autocorrect(file, issue) do
defp run_autofix(issue, file, exec) do
case issue.check.autofix(file, issue) do
^file ->
file

Expand Down
10 changes: 5 additions & 5 deletions lib/credo/config_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ defmodule Credo.ConfigBuilder do
|> add_switch_ignore(switches)
|> add_switch_mute_exit_status(switches)
|> add_switch_only(switches)
|> add_switch_autocorrect(switches)
|> add_switch_autofix(switches)
|> add_switch_read_from_stdin(switches)
|> add_switch_strict(switches)
|> add_switch_min_priority(switches)
Expand Down Expand Up @@ -263,13 +263,13 @@ defmodule Credo.ConfigBuilder do

defp add_switch_only(exec, _), do: exec

# add_switch_autocorrect
# add_switch_autofix

defp add_switch_autocorrect(exec, %{autocorrect: true}) do
%Execution{exec | autocorrect: true}
defp add_switch_autofix(exec, %{autofix: true}) do
%Execution{exec | autofix: true}
end

defp add_switch_autocorrect(exec, _), do: exec
defp add_switch_autofix(exec, _), do: exec

# add_switch_ignore

Expand Down
2 changes: 1 addition & 1 deletion lib/credo/execution.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ defmodule Credo.Execution do
plugins: [],
parse_timeout: 5000,
strict: false,
autocorrect: false,
autofix: false,

# options, set by the command line
format: nil,
Expand Down
6 changes: 3 additions & 3 deletions test/credo/check/consistency/line_endings_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ defmodule Credo.Check.Readability.LineEndingsTest do
|> assert_issue
end

describe "autocorrect/1" do
describe "autofix/1" do
test "switches unix to windows line endings" do
starting = "defmodule Credo.Sample do\n@test_attribute :foo\nend\n"
expected = "defmodule Credo.Sample do\r\n@test_attribute :foo\r\nend\r\n"

issue = %Credo.Issue{message: "File is using unix line endings while most of the files use windows line endings."}

assert @described_check.autocorrect(starting, issue) == expected
assert @described_check.autofix(starting, issue) == expected
end

test "switches windows to unix line endings" do
Expand All @@ -77,7 +77,7 @@ defmodule Credo.Check.Readability.LineEndingsTest do

issue = %Credo.Issue{message: "File is using windows line endings while most of the files use unix line endings."}

assert @described_check.autocorrect(starting, issue) == expected
assert @described_check.autofix(starting, issue) == expected
end
end
end
10 changes: 5 additions & 5 deletions test/credo/check/readability/alias_order_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ defmodule Credo.Check.Readability.AliasOrderTest do
end)
end

describe "autocorrect/1" do
describe "autofix/1" do
test "puts all the aliases in the right order" do
starting = """
defmodule CredoSampleModule do
Expand All @@ -247,7 +247,7 @@ defmodule Credo.Check.Readability.AliasOrderTest do
end
"""

assert @described_check.autocorrect(starting, nil) == expected
assert @described_check.autofix(starting, nil) == expected
end

test "works with multi-aliases" do
Expand All @@ -265,7 +265,7 @@ defmodule Credo.Check.Readability.AliasOrderTest do
end
"""

assert @described_check.autocorrect(starting, nil) == expected
assert @described_check.autofix(starting, nil) == expected
end

test "works with as: option" do
Expand All @@ -283,7 +283,7 @@ defmodule Credo.Check.Readability.AliasOrderTest do
end
"""

assert @described_check.autocorrect(starting, nil) == expected
assert @described_check.autofix(starting, nil) == expected
end

test "works with multiple blocks of aliases including multi-aliases" do
Expand All @@ -310,7 +310,7 @@ defmodule Credo.Check.Readability.AliasOrderTest do
end
"""

assert @described_check.autocorrect(starting, nil) == expected
assert @described_check.autofix(starting, nil) == expected
end
end
end
4 changes: 2 additions & 2 deletions test/credo/check/refactor/unless_with_else_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ defmodule Credo.Check.Refactor.UnlessWithElseTest do
|> assert_issue()
end

describe "autocorrect/2" do
describe "autofix/2" do
test "changes the unless with else to an if clause" do
starting = """
defmodule CredoSampleModule do
Expand Down Expand Up @@ -78,7 +78,7 @@ defmodule Credo.Check.Refactor.UnlessWithElseTest do
end
"""

assert @described_check.autocorrect(starting, nil) == expected
assert @described_check.autofix(starting, nil) == expected
end
end
end
6 changes: 3 additions & 3 deletions test/credo/check/warning/unused_string_operation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ defmodule Credo.Check.Warning.UnusedStringOperationTest do
end)
end

describe "autocorrect/2" do
describe "autofix/2" do
test "removes the unused String function" do
starting = """
defmodule CredoSampleModule do
Expand All @@ -744,7 +744,7 @@ defmodule Credo.Check.Warning.UnusedStringOperationTest do
end
"""

assert @described_check.autocorrect(starting, nil) == expected
assert @described_check.autofix(starting, nil) == expected
end

test "it should report a violation when end of pipe" do
Expand All @@ -768,7 +768,7 @@ defmodule Credo.Check.Warning.UnusedStringOperationTest do
end
"""

assert @described_check.autocorrect(starting, nil) == expected
assert @described_check.autofix(starting, nil) == expected
end
end
end
Loading

0 comments on commit 4bb4095

Please sign in to comment.