Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve autocorrect deprecations #378

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions lib/erb_lint/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def run_with_corrections(runner, filename, file_content)
runner.run(processed_source)
break unless autocorrect? && runner.offenses.any?

corrector = correct(processed_source, runner.offenses)
corrector = corrector(processed_source, runner.offenses)
break if corrector.corrections.empty?
break if processed_source.file_content == corrector.corrected_content

Expand Down Expand Up @@ -202,10 +202,8 @@ def read_content(filename)
$stdin.binmode.read.force_encoding(Encoding::UTF_8)
end

def correct(processed_source, offenses)
corrector = ERBLint::Corrector.new(processed_source, offenses)
failure!(corrector.diagnostics.join(", ")) if corrector.diagnostics.any?
corrector
def corrector(processed_source, offenses)
ERBLint::Corrector.new(processed_source, offenses)
end

def config_filename
Expand Down
21 changes: 5 additions & 16 deletions lib/erb_lint/corrector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Corrector
def initialize(processed_source, offenses)
@processed_source = processed_source
@offenses = offenses
corrector = RuboCop::Cop::Corrector.new(@processed_source.source_buffer)
correct!(corrector)
@corrected_content = corrector.rewrite
end

Expand All @@ -16,22 +18,9 @@ def corrections
end.compact
end

def corrector
BASE.new(@processed_source.source_buffer, corrections)
end

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
def correct!(corrector)
corrections.each do |correction|
correction.call(corrector)
end
end
end
Expand Down
10 changes: 2 additions & 8 deletions lib/erb_lint/file_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,8 @@ def initialize(base_path)
@base_path = base_path
end

if RUBY_VERSION >= "2.6"
def yaml(filename)
YAML.safe_load(read_content(filename), permitted_classes: [Regexp, Symbol], filename: filename) || {}
end
else
def yaml(filename)
YAML.safe_load(read_content(filename), [Regexp, Symbol], [], false, filename) || {}
end
def yaml(filename)
YAML.safe_load(read_content(filename), permitted_classes: [Regexp, Symbol], filename: filename) || {}
end

private
Expand Down
68 changes: 15 additions & 53 deletions lib/erb_lint/linters/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

require "better_html"
require "tempfile"
require "erb_lint/utils/offset_corrector"

module ERBLint
module Linters
Expand Down Expand Up @@ -36,30 +35,14 @@ def run(processed_source)
end
end

if ::RuboCop::Version::STRING.to_f >= 0.87
def autocorrect(_processed_source, offense)
return unless offense.context
def autocorrect(_processed_source, offense)
return unless offense.context

rubocop_correction = offense.context[:rubocop_correction]
return unless rubocop_correction
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
lambda do |corrector|
corrector.import!(rubocop_correction, offset: offense.context[:offset])
end
end

Expand All @@ -85,39 +68,18 @@ def inspect_content(processed_source, erb_node)
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?
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?
correction = rubocop_offense.corrector if rubocop_offense.corrected?

offense_range = processed_source
.to_source_range(rubocop_offense.location)
.offset(offset)
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
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
add_offense(rubocop_offense, offense_range, correction, offset, code_node.loc.range)
end
end

Expand Down
69 changes: 0 additions & 69 deletions lib/erb_lint/utils/offset_corrector.rb

This file was deleted.

20 changes: 0 additions & 20 deletions spec/lib/erb_lint/utils/offset_corrector_spec.rb

This file was deleted.

Loading