mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Add --autocorrect
CLI option
This commit is contained in:
parent
d3d3ccd7e3
commit
99e7ccd23b
4 changed files with 33 additions and 1 deletions
|
@ -8,6 +8,7 @@ module Ameba::Cli
|
|||
def run(args = ARGV)
|
||||
opts = parse_args args
|
||||
config = Config.load opts.config, opts.colors?
|
||||
config.autocorrect = opts.autocorrect?
|
||||
|
||||
if globs = opts.globs
|
||||
config.globs = globs
|
||||
|
@ -47,6 +48,7 @@ module Ameba::Cli
|
|||
property? all = false
|
||||
property? colors = true
|
||||
property? without_affected_code = false
|
||||
property? autocorrect = false
|
||||
end
|
||||
|
||||
def parse_args(args, opts = Opts.new)
|
||||
|
@ -89,6 +91,10 @@ module Ameba::Cli
|
|||
opts.all = true
|
||||
end
|
||||
|
||||
parser.on("-a", "--autocorrect", "Autocorrect issues") do
|
||||
opts.autocorrect = true
|
||||
end
|
||||
|
||||
parser.on("--gen-config",
|
||||
"Generate a configuration file acting as a TODO list") do
|
||||
opts.formatter = :todo
|
||||
|
@ -133,6 +139,7 @@ module Ameba::Cli
|
|||
if name = opts.formatter
|
||||
config.formatter = name
|
||||
end
|
||||
config.formatter.config[:autocorrect] = opts.autocorrect?
|
||||
config.formatter.config[:without_affected_code] =
|
||||
opts.without_affected_code?
|
||||
end
|
||||
|
|
|
@ -54,6 +54,8 @@ class Ameba::Config
|
|||
# ```
|
||||
property excluded : Array(String)
|
||||
|
||||
property? autocorrect = false
|
||||
|
||||
@rule_groups : Hash(String, Array(Rule::Base))
|
||||
|
||||
# Creates a new instance of `Ameba::Config` based on YAML parameters.
|
||||
|
|
|
@ -36,7 +36,15 @@ module Ameba::Formatter
|
|||
next if issue.disabled?
|
||||
next if (location = issue.location).nil?
|
||||
|
||||
output.puts location.colorize(:cyan)
|
||||
output.print location.colorize(:cyan)
|
||||
if issue.correctable?
|
||||
if config[:autocorrect]?
|
||||
output.print " [Corrected]".colorize(:green)
|
||||
else
|
||||
output.print " [Correctable]".colorize(:yellow)
|
||||
end
|
||||
end
|
||||
output.puts
|
||||
output.puts \
|
||||
"[#{issue.rule.severity.symbol}] " \
|
||||
"#{issue.rule.name}: " \
|
||||
|
|
|
@ -29,6 +29,8 @@ module Ameba
|
|||
# Checks for unneeded disable directives. Always inspects a source last
|
||||
@unneeded_disable_directive_rule : Rule::Base?
|
||||
|
||||
private getter? autocorrect : Bool
|
||||
|
||||
# Instantiates a runner using a `config`.
|
||||
#
|
||||
# ```
|
||||
|
@ -43,6 +45,7 @@ module Ameba
|
|||
@formatter = config.formatter
|
||||
@severity = config.severity
|
||||
@rules = config.rules.select(&.enabled).reject!(&.special?)
|
||||
@autocorrect = config.autocorrect?
|
||||
|
||||
@unneeded_disable_directive_rule =
|
||||
config.rules
|
||||
|
@ -50,6 +53,7 @@ module Ameba
|
|||
end
|
||||
|
||||
protected def initialize(@rules, @sources, @formatter, @severity)
|
||||
@autocorrect = false
|
||||
end
|
||||
|
||||
# Performs the inspection. Iterates through all sources and test it using
|
||||
|
@ -89,12 +93,16 @@ module Ameba
|
|||
private def run_source(source)
|
||||
@formatter.source_started source
|
||||
|
||||
# TODO: run autocorrection recursively. A new `Issue#source` property must
|
||||
# be added so that `affected_code` will return the code from the old
|
||||
# source instead of the autocorrected one.
|
||||
if @syntax_rule.catch(source).valid?
|
||||
@rules.each do |rule|
|
||||
next if rule.excluded?(source)
|
||||
rule.test(source)
|
||||
end
|
||||
check_unneeded_directives(source)
|
||||
autocorrect(source) if autocorrect?
|
||||
end
|
||||
|
||||
@formatter.source_finished source
|
||||
|
@ -135,5 +143,12 @@ module Ameba
|
|||
rule.test(source)
|
||||
end
|
||||
end
|
||||
|
||||
private def autocorrect(source)
|
||||
corrected_code = Source::Corrector.correct(source)
|
||||
return unless corrected_code
|
||||
|
||||
File.write(source.path, corrected_code)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue