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.

Defined Under Namespace

Classes: Runner

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



74
75
76
# File 'lib/haml_lint/linter/rubocop.rb', line 74

def last_extracted_source
  @last_extracted_source
end

#last_new_ruby_sourceObject

Returns the value of attribute last_new_ruby_source.



75
76
77
# File 'lib/haml_lint/linter/rubocop.rb', line 75

def last_new_ruby_source
  @last_new_ruby_source
end

Class Method Details

.cops_names_not_supporting_autocorrectObject



127
128
129
130
131
132
133
134
135
# File 'lib/haml_lint/linter/rubocop.rb', line 127

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)


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/haml_lint/linter/rubocop.rb', line 77

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