Sha256: 3b513091ffd8edd34ecb531c8911f50b8cd6f5cee1aec736a633988c57303166

Contents?: true

Size: 1.8 KB

Versions: 5

Compression:

Stored size: 1.8 KB

Contents

require "rubocop"

module Standard
  class MergesSettings
    Settings = Struct.new(:runner, :options, :paths)

    def call(argv, standard_yaml)
      standard_argv, rubocop_argv = separate_argv(argv)
      standard_cli_flags = parse_standard_argv(standard_argv)
      rubocop_cli_flags, lint_paths = RuboCop::Options.new.parse(rubocop_argv)

      Settings.new(
        determine_command(standard_argv),
        merge(standard_yaml, standard_cli_flags, without_banned(rubocop_cli_flags)),
        lint_paths
      )
    end

    private

    def separate_argv(argv)
      argv.partition do |flag|
        ["--generate-todo", "--fix", "--no-fix", "--version", "-v", "--help", "-h", "--lsp"].include?(flag)
      end
    end

    def parse_standard_argv(argv)
      argv.each_with_object({}) do |arg, cli_flags|
        if arg == "--fix"
          cli_flags[:autocorrect] = true
        elsif arg == "--no-fix"
          cli_flags[:autocorrect] = false
        end
      end
    end

    def determine_command(argv)
      if (argv & ["--help", "-h"]).any?
        :help
      elsif (argv & ["--version", "-v"]).any?
        :version
      elsif (argv & ["--generate-todo"]).any?
        :genignore
      elsif (argv & ["--lsp"]).any?
        :lsp
      else
        :rubocop
      end
    end

    def merge(standard_yaml, standard_cli_flags, rubocop_cli_flags)
      {
        autocorrect: standard_yaml[:fix],
        formatters: [[standard_yaml[:format] || "Standard::Formatter", nil]],
        parallel: standard_yaml[:parallel],
        todo_file: standard_yaml[:todo_file],
        todo_ignore_files: standard_yaml[:todo_ignore_files]
      }.merge(standard_cli_flags).merge(rubocop_cli_flags)
    end

    def without_banned(rubocop_cli_flags)
      rubocop_cli_flags.tap do |flags|
        flags.delete(:config)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
standard-1.21.1 lib/standard/merges_settings.rb
standard-1.21.0 lib/standard/merges_settings.rb
standard-1.20.0 lib/standard/merges_settings.rb
standard-1.19.1 lib/standard/merges_settings.rb
standard-1.19.0 lib/standard/merges_settings.rb