Skip to content

Commit

Permalink
Update for RuboCop v0.87+
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre committed Aug 4, 2020
1 parent 87b62d8 commit f3b6b0a
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 29 deletions.
11 changes: 6 additions & 5 deletions Gemfile.lock
Expand Up @@ -5,6 +5,7 @@ PATH
activesupport
better_html (~> 1.0.7)
html_tokenizer
parser (>= 2.7.1.4)
rainbow
rubocop (~> 0.79)
smart_properties
Expand All @@ -23,7 +24,7 @@ GEM
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
ast (2.4.0)
ast (2.4.1)
better_html (1.0.15)
actionview (>= 4.0)
activesupport (>= 4.0)
Expand All @@ -42,16 +43,16 @@ GEM
i18n (1.8.1)
concurrent-ruby (~> 1.0)
jaro_winkler (1.5.4)
loofah (2.5.0)
loofah (2.6.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
mini_portile2 (2.4.0)
minitest (5.13.0)
nokogiri (1.10.9)
nokogiri (1.10.10)
mini_portile2 (~> 2.4.0)
parallel (1.19.1)
parser (2.7.1.2)
ast (~> 2.4.0)
parser (2.7.1.4)
ast (~> 2.4.1)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
Expand Down
1 change: 1 addition & 0 deletions erb_lint.gemspec
Expand Up @@ -27,6 +27,7 @@ Gem::Specification.new do |s|
s.add_dependency('better_html', '~> 1.0.7')
s.add_dependency('html_tokenizer')
s.add_dependency('rubocop', '~> 0.79')
s.add_dependency('parser', '>= 2.7.1.4')
s.add_dependency('activesupport')
s.add_dependency('smart_properties')
s.add_dependency('rainbow')
Expand Down
2 changes: 2 additions & 0 deletions lib/erb_lint.rb
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'rubocop'

require 'erb_lint/corrector'
require 'erb_lint/file_loader'
require 'erb_lint/linter_config'
Expand Down
17 changes: 14 additions & 3 deletions lib/erb_lint/corrector.rb
Expand Up @@ -17,11 +17,22 @@ def corrections
end

def corrector
::RuboCop::Cop::Corrector.new(@processed_source.source_buffer, corrections)
BASE.new(@processed_source.source_buffer, corrections)
end

def diagnostics
corrector.diagnostics
if ::RuboCop::Version::STRING.to_f >= 0.87
require 'rubocop/cop/legacy/corrector'
BASE = ::RuboCop::Cop::Legacy::Corrector

def diagnostics
[]
end
else
BASE = ::RuboCop::Cop::Corrector

def diagnostics
corrector.diagnostics
end
end
end
end
73 changes: 52 additions & 21 deletions lib/erb_lint/linters/rubocop.rb
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require 'better_html'
require 'rubocop'
require 'tempfile'
require 'erb_lint/utils/offset_corrector'

Expand Down Expand Up @@ -35,17 +34,29 @@ def run(processed_source)
end
end

def autocorrect(processed_source, offense)
return unless offense.context

lambda do |corrector|
passthrough = Utils::OffsetCorrector.new(
processed_source,
corrector,
offense.context[:offset],
offense.context[:bound_range],
)
offense.context[:rubocop_correction].call(passthrough)
if ::RuboCop::Version::STRING.to_f >= 0.87
def autocorrect(_processed_source, offense)
return unless offense.context
rubocop_correction = offense.context[:rubocop_correction]
return unless rubocop_correction

lambda do |corrector|
corrector.import!(rubocop_correction, offset: offense.context[:offset])
end
end
else
def autocorrect(processed_source, offense)
return unless offense.context

lambda do |corrector|
passthrough = Utils::OffsetCorrector.new(
processed_source,
corrector,
offense.context[:offset],
offense.context[:bound_range],
)
offense.context[:rubocop_correction].call(passthrough)
end
end
end

Expand All @@ -68,15 +79,16 @@ def inspect_content(processed_source, erb_node)
source = rubocop_processed_source(aligned_source, processed_source.filename)
return unless source.valid_syntax?

team = build_team
team.inspect_file(source)
team.cops.each do |cop|
correction_offset = 0
cop.offenses.reject(&:disabled?).each do |rubocop_offense|
if rubocop_offense.corrected?
correction = cop.corrections[correction_offset]
correction_offset += 1
end
activate_team(processed_source, source, offset, code_node, build_team)
end

if ::RuboCop::Version::STRING.to_f >= 0.87
def activate_team(processed_source, source, offset, code_node, team)
report = team.investigate(source)
report.offenses.each do |rubocop_offense|
next if rubocop_offense.disabled?

correction = rubocop_offense.corrector if rubocop_offense.corrected?

offense_range = processed_source
.to_source_range(rubocop_offense.location)
Expand All @@ -85,6 +97,25 @@ def inspect_content(processed_source, erb_node)
add_offense(rubocop_offense, offense_range, correction, offset, code_node.loc.range)
end
end
else
def activate_team(processed_source, source, offset, code_node, team)
team.inspect_file(source)
team.cops.each do |cop|
correction_offset = 0
cop.offenses.reject(&:disabled?).each do |rubocop_offense|
if rubocop_offense.corrected?
correction = cop.corrections[correction_offset]
correction_offset += 1
end

offense_range = processed_source
.to_source_range(rubocop_offense.location)
.offset(offset)

add_offense(rubocop_offense, offense_range, correction, offset, code_node.loc.range)
end
end
end
end

def tempfile_from(filename, content)
Expand Down

0 comments on commit f3b6b0a

Please sign in to comment.