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)
 | 
					  def run(args = ARGV)
 | 
				
			||||||
    opts = parse_args args
 | 
					    opts = parse_args args
 | 
				
			||||||
    config = Config.load opts.config, opts.colors?
 | 
					    config = Config.load opts.config, opts.colors?
 | 
				
			||||||
 | 
					    config.autocorrect = opts.autocorrect?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if globs = opts.globs
 | 
					    if globs = opts.globs
 | 
				
			||||||
      config.globs = globs
 | 
					      config.globs = globs
 | 
				
			||||||
| 
						 | 
					@ -47,6 +48,7 @@ module Ameba::Cli
 | 
				
			||||||
    property? all = false
 | 
					    property? all = false
 | 
				
			||||||
    property? colors = true
 | 
					    property? colors = true
 | 
				
			||||||
    property? without_affected_code = false
 | 
					    property? without_affected_code = false
 | 
				
			||||||
 | 
					    property? autocorrect = false
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def parse_args(args, opts = Opts.new)
 | 
					  def parse_args(args, opts = Opts.new)
 | 
				
			||||||
| 
						 | 
					@ -89,6 +91,10 @@ module Ameba::Cli
 | 
				
			||||||
        opts.all = true
 | 
					        opts.all = true
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      parser.on("-a", "--autocorrect", "Autocorrect issues") do
 | 
				
			||||||
 | 
					        opts.autocorrect = true
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      parser.on("--gen-config",
 | 
					      parser.on("--gen-config",
 | 
				
			||||||
        "Generate a configuration file acting as a TODO list") do
 | 
					        "Generate a configuration file acting as a TODO list") do
 | 
				
			||||||
        opts.formatter = :todo
 | 
					        opts.formatter = :todo
 | 
				
			||||||
| 
						 | 
					@ -133,6 +139,7 @@ module Ameba::Cli
 | 
				
			||||||
    if name = opts.formatter
 | 
					    if name = opts.formatter
 | 
				
			||||||
      config.formatter = name
 | 
					      config.formatter = name
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					    config.formatter.config[:autocorrect] = opts.autocorrect?
 | 
				
			||||||
    config.formatter.config[:without_affected_code] =
 | 
					    config.formatter.config[:without_affected_code] =
 | 
				
			||||||
      opts.without_affected_code?
 | 
					      opts.without_affected_code?
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -54,6 +54,8 @@ class Ameba::Config
 | 
				
			||||||
  # ```
 | 
					  # ```
 | 
				
			||||||
  property excluded : Array(String)
 | 
					  property excluded : Array(String)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  property? autocorrect = false
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @rule_groups : Hash(String, Array(Rule::Base))
 | 
					  @rule_groups : Hash(String, Array(Rule::Base))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  # Creates a new instance of `Ameba::Config` based on YAML parameters.
 | 
					  # Creates a new instance of `Ameba::Config` based on YAML parameters.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -36,7 +36,15 @@ module Ameba::Formatter
 | 
				
			||||||
          next if issue.disabled?
 | 
					          next if issue.disabled?
 | 
				
			||||||
          next if (location = issue.location).nil?
 | 
					          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 \
 | 
					          output.puts \
 | 
				
			||||||
            "[#{issue.rule.severity.symbol}] " \
 | 
					            "[#{issue.rule.severity.symbol}] " \
 | 
				
			||||||
            "#{issue.rule.name}: " \
 | 
					            "#{issue.rule.name}: " \
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -29,6 +29,8 @@ module Ameba
 | 
				
			||||||
    # Checks for unneeded disable directives. Always inspects a source last
 | 
					    # Checks for unneeded disable directives. Always inspects a source last
 | 
				
			||||||
    @unneeded_disable_directive_rule : Rule::Base?
 | 
					    @unneeded_disable_directive_rule : Rule::Base?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private getter? autocorrect : Bool
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Instantiates a runner using a `config`.
 | 
					    # Instantiates a runner using a `config`.
 | 
				
			||||||
    #
 | 
					    #
 | 
				
			||||||
    # ```
 | 
					    # ```
 | 
				
			||||||
| 
						 | 
					@ -43,6 +45,7 @@ module Ameba
 | 
				
			||||||
      @formatter = config.formatter
 | 
					      @formatter = config.formatter
 | 
				
			||||||
      @severity = config.severity
 | 
					      @severity = config.severity
 | 
				
			||||||
      @rules = config.rules.select(&.enabled).reject!(&.special?)
 | 
					      @rules = config.rules.select(&.enabled).reject!(&.special?)
 | 
				
			||||||
 | 
					      @autocorrect = config.autocorrect?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      @unneeded_disable_directive_rule =
 | 
					      @unneeded_disable_directive_rule =
 | 
				
			||||||
        config.rules
 | 
					        config.rules
 | 
				
			||||||
| 
						 | 
					@ -50,6 +53,7 @@ module Ameba
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    protected def initialize(@rules, @sources, @formatter, @severity)
 | 
					    protected def initialize(@rules, @sources, @formatter, @severity)
 | 
				
			||||||
 | 
					      @autocorrect = false
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Performs the inspection. Iterates through all sources and test it using
 | 
					    # Performs the inspection. Iterates through all sources and test it using
 | 
				
			||||||
| 
						 | 
					@ -89,12 +93,16 @@ module Ameba
 | 
				
			||||||
    private def run_source(source)
 | 
					    private def run_source(source)
 | 
				
			||||||
      @formatter.source_started 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?
 | 
					      if @syntax_rule.catch(source).valid?
 | 
				
			||||||
        @rules.each do |rule|
 | 
					        @rules.each do |rule|
 | 
				
			||||||
          next if rule.excluded?(source)
 | 
					          next if rule.excluded?(source)
 | 
				
			||||||
          rule.test(source)
 | 
					          rule.test(source)
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
        check_unneeded_directives(source)
 | 
					        check_unneeded_directives(source)
 | 
				
			||||||
 | 
					        autocorrect(source) if autocorrect?
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      @formatter.source_finished source
 | 
					      @formatter.source_finished source
 | 
				
			||||||
| 
						 | 
					@ -135,5 +143,12 @@ module Ameba
 | 
				
			||||||
        rule.test(source)
 | 
					        rule.test(source)
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
    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
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue