Skip to content

Commit

Permalink
Add autocorrect checks to ExplainFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
FnControlOption committed Oct 27, 2021
1 parent 8d3b760 commit 1b6fe40
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/ameba/cli/cmd.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module Ameba::Cli
runner = Ameba.run(config)

if location = opts.location_to_explain
runner.explain(location)
runner.explain(location, autocorrect: opts.autocorrect?)
else
exit 1 unless runner.success?
end
Expand Down
17 changes: 14 additions & 3 deletions src/ameba/formatter/explain_formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Ameba::Formatter

getter output : IO::FileDescriptor | IO::Memory
getter location : Crystal::Location
getter? autocorrect : Bool

# Creates a new instance of ExplainFormatter.
# Accepts *output* which indicates the io where the explanation will be wrtitten to.
Expand All @@ -20,7 +21,7 @@ module Ameba::Formatter
# ExplainFormatter.new output,
# {file: path, line: line_number, column: column_number}
# ```
def initialize(@output, location)
def initialize(@output, location, @autocorrect = false)
@location = Crystal::Location.new(location[:file], location[:line], location[:column])
end

Expand All @@ -40,12 +41,22 @@ module Ameba::Formatter

return unless (location = issue.location)

output_title "ISSUE INFO"
output_paragraph [
issue_info = [
issue.message.colorize(:red).to_s,
location.to_s.colorize(:cyan).to_s,
]

if issue.correctable?
if autocorrect?
issue_info << "Corrected".colorize(:green).to_s
else
issue_info << "Correctable".colorize(:yellow).to_s
end
end

output_title "ISSUE INFO"
output_paragraph issue_info

if affected_code = affected_code(issue, context_lines: 3)
output_title "AFFECTED CODE"
output_paragraph affected_code
Expand Down
4 changes: 2 additions & 2 deletions src/ameba/runner.cr
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ module Ameba
# runner.run
# runner.explain({file: file, line: l, column: c})
# ```
def explain(location, output = STDOUT)
Formatter::ExplainFormatter.new(output, location).finished @sources
def explain(location, output = STDOUT, autocorrect = false)
Formatter::ExplainFormatter.new(output, location, autocorrect).finished @sources
end

# Indicates whether the last inspection successful or not.
Expand Down

0 comments on commit 1b6fe40

Please sign in to comment.