Class: HamlLint::Linter::RuboCop

Inherits:
HamlLint::Linter show all
Includes:
HamlLint::LinterRegistry
Defined in:
lib/haml_lint/linter/rubocop.rb

Overview

Runs RuboCop on the Ruby code contained within HAML templates.

The processing is done by extracting a Ruby file that matches the content, including the indentation, of the HAML file. This way, we can run RuboCop with autocorrect and get new Ruby code which should be HAML compatible.

The ruby extraction makes “Chunks” which wrap each HAML constructs. The Chunks can then use the corrected Ruby code to apply the corrections back in the HAML using logic specific to each type of Chunk.

The work is spread across the classes in the HamlLint::RubyExtraction module.

Constant Summary collapse

SEVERITY_MAP =

Maps the ::RuboCop::Cop::Severity levels to our own levels.

{
  error: :error,
  fatal: :error,
  convention: :warning,
  refactor: :warning,
  warning: :warning,
  info: :info,
}.freeze

Instance Attribute Summary collapse

Attributes inherited from HamlLint::Linter

#lints

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HamlLint::LinterRegistry

extract_linters_from, included

Methods inherited from HamlLint::Linter

#initialize, #name, #run, #run_or_raise, supports_autocorrect?, #supports_autocorrect?

Methods included from HamlVisitor

#visit, #visit_children

Constructor Details

This class inherits a constructor from HamlLint::Linter

Instance Attribute Details

#last_extracted_sourceObject

Debug fields, also used in tests



34
35
36
# File 'lib/haml_lint/linter/rubocop.rb', line 34

def last_extracted_source
  @last_extracted_source
end

#last_new_ruby_sourceObject

Returns the value of attribute last_new_ruby_source.



35
36
37
# File 'lib/haml_lint/linter/rubocop.rb', line 35

def last_new_ruby_source
  @last_new_ruby_source
end

Class Method Details

.cops_names_not_supporting_autocorrectObject



87
88
89
90
91
92
93
94
95
# File 'lib/haml_lint/linter/rubocop.rb', line 87

def self.cops_names_not_supporting_autocorrect
  return @cops_names_not_supporting_autocorrect if @cops_names_not_supporting_autocorrect
  return [] unless ::RuboCop::Cop::Registry.respond_to?(:all)

  cops_without_autocorrect = ::RuboCop::Cop::Registry.all.reject(&:support_autocorrect?)
  # This cop cannot be disabled
  cops_without_autocorrect.delete(::RuboCop::Cop::Lint::Syntax)
  @cops_names_not_supporting_autocorrect = cops_without_autocorrect.map { |cop| cop.badge.to_s }.freeze
end

Instance Method Details

#visit_root(_node) {|:skip_children| ... } ⇒ Object

rubocop:disable Metrics

Yields:

  • (:skip_children)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/haml_lint/linter/rubocop.rb', line 37

def visit_root(_node) # rubocop:disable Metrics
  # Need to call the received block to avoid Linter automatically visiting children
  # Only important thing is that the argument is not ":children"
  yield :skip_children

  if document.indentation && document.indentation != '  '
    @lints <<
      HamlLint::Lint.new(
        self,
        document.file,
        nil,
        "Only supported indentation is 2 spaces, got: #{document.indentation.dump}",
        :error
      )
    return
  end

  @last_extracted_source = nil
  @last_new_ruby_source = nil

  coordinator = HamlLint::RubyExtraction::Coordinator.new(document)

  extracted_source = coordinator.extract_ruby_source
  if ENV['HAML_LINT_INTERNAL_DEBUG'] == 'true'
    puts "------ Extracted ruby from #{@document.file}:"
    puts extracted_source.source
    puts '------'
  end

  @last_extracted_source = extracted_source

  if extracted_source.source.empty?
    @last_new_ruby_source = ''
    return
  end

  new_ruby_code = process_ruby_source(extracted_source.source, extracted_source.source_map)

  if @autocorrect && ENV['HAML_LINT_INTERNAL_DEBUG'] == 'true'
    puts "------ Autocorrected extracted ruby from #{@document.file}:"
    puts new_ruby_code
    puts '------'
  end

  if @autocorrect && transfer_corrections?(extracted_source.source, new_ruby_code)
    @last_new_ruby_source = new_ruby_code
    transfer_corrections(coordinator, new_ruby_code)
  end
end