Class: RubomaticHtml::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/rubomatic-html/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(linted_files) ⇒ Runner

Returns a new instance of Runner.

Parameters:

  • linted_files (Array<String>)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rubomatic-html/runner.rb', line 30

def initialize(linted_files)
  files_to_lint = Array(linted_files)

  if files_to_lint.empty?
    files_to_lint = Dir[File.join('app', 'views', '**', '*')]
  end

  @files_to_lint = files_to_lint

  custom_config = ::YAML.safe_load(Pathname.new('.rubomatic-html.yml').read).freeze
  config = {}
  base_transformations = { 'Enabled' => :enabled, 'Exclude' => :exclude }

  CONFIG.each do |cop_name, cop_config|
    the_cop = all_cops.find { |cop| cop.name == cop_name }
    transformations = base_transformations.merge(the_cop&.allowed_config_transform || {})
    config[cop_name] = {}

    transformations.each do |allowed_node, ruby_node|
      config[cop_name][ruby_node] = cop_config[allowed_node] if cop_config.has_key?(allowed_node)

      next unless custom_config.fetch(cop_name, {}).has_key?(allowed_node)

      config[cop_name][ruby_node] = custom_config.dig(cop_name, allowed_node)
    end

    config[cop_name][:exclude] = Array(config[cop_name][:exclude])
  end

  @config = config
end

Instance Attribute Details

#configHash

Returns:

  • (Hash)


26
27
28
# File 'lib/rubomatic-html/runner.rb', line 26

def config
  @config
end

#files_to_lintArray<String>

Returns:

  • (Array<String>)


24
25
26
# File 'lib/rubomatic-html/runner.rb', line 24

def files_to_lint
  @files_to_lint
end

Instance Method Details

#runvoid

This method returns an undefined value.

Runs all cops against all files



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rubomatic-html/runner.rb', line 66

def run
  all_config = config.fetch('AllCops')

  files_to_lint.each do |file|
    next if all_config.fetch(:exclude).any? { |ignored| file.end_with?(ignored) }

    ext = File.extname(file)

    next if ext.match?(/haml/i)

    check_it = ext.match?(/html/i)
    check_it ||= ext.match?(/erb\z/i)

    next unless check_it

    run_file(file)
  end
end