diff --git a/lib/erb_lint/cli.rb b/lib/erb_lint/cli.rb index 34b847ae..2be7c926 100644 --- a/lib/erb_lint/cli.rb +++ b/lib/erb_lint/cli.rb @@ -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 @@ -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 diff --git a/lib/erb_lint/corrector.rb b/lib/erb_lint/corrector.rb index 7ccecf5b..804d03ee 100644 --- a/lib/erb_lint/corrector.rb +++ b/lib/erb_lint/corrector.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require "rubocop/cop/legacy/corrector" + module ERBLint class Corrector attr_reader :processed_source, :offenses, :corrected_content @@ -17,22 +19,7 @@ def corrections 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 - end + ::RuboCop::Cop::Legacy::Corrector.new(@processed_source.source_buffer, corrections) end end end diff --git a/lib/erb_lint/file_loader.rb b/lib/erb_lint/file_loader.rb index 1eb0a272..316664fb 100644 --- a/lib/erb_lint/file_loader.rb +++ b/lib/erb_lint/file_loader.rb @@ -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 diff --git a/lib/erb_lint/linters/rubocop.rb b/lib/erb_lint/linters/rubocop.rb index bc67f691..abe8e3b6 100644 --- a/lib/erb_lint/linters/rubocop.rb +++ b/lib/erb_lint/linters/rubocop.rb @@ -2,7 +2,6 @@ require "better_html" require "tempfile" -require "erb_lint/utils/offset_corrector" module ERBLint module Linters @@ -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 @@ -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 diff --git a/lib/erb_lint/utils/offset_corrector.rb b/lib/erb_lint/utils/offset_corrector.rb deleted file mode 100644 index 4f16659d..00000000 --- a/lib/erb_lint/utils/offset_corrector.rb +++ /dev/null @@ -1,69 +0,0 @@ -# frozen_string_literal: true - -module ERBLint - module Utils - class OffsetCorrector - def initialize(processed_source, corrector, offset, bound_range) - @processed_source = processed_source - @corrector = corrector - @offset = offset - @bound_range = bound_range - end - - def remove(range) - @corrector.remove(range_with_offset(range)) - end - - def insert_before(range, content) - @corrector.insert_before(range_with_offset(range), content) - end - - def insert_after(range, content) - @corrector.insert_after(range_with_offset(range), content) - end - - def replace(range, content) - @corrector.replace(range_with_offset(range), content) - end - - def remove_preceding(range, size) - @corrector.remove_preceding(range_with_offset(range), size) - end - - def remove_leading(range, size) - @corrector.remove_leading(range_with_offset(range), size) - end - - def remove_trailing(range, size) - @corrector.remove_trailing(range_with_offset(range), size) - end - - def range_with_offset(node_or_range) - range = to_range(node_or_range) - - @processed_source.to_source_range( - bound(@offset + range.begin_pos)..bound(@offset + (range.end_pos - 1)), - ) - end - - def bound(pos) - pos.clamp(@bound_range.min, @bound_range.max) - end - - private - - def to_range(node_or_range) - case node_or_range - when ::RuboCop::AST::Node, ::Parser::Source::Comment - node_or_range.loc.expression - when ::Parser::Source::Range - node_or_range - else - raise TypeError, - "Expected a Parser::Source::Range, Comment or " \ - "Rubocop::AST::Node, got #{node_or_range.class}" - end - end - end - end -end diff --git a/spec/lib/erb_lint/utils/offset_corrector_spec.rb b/spec/lib/erb_lint/utils/offset_corrector_spec.rb deleted file mode 100644 index d374c271..00000000 --- a/spec/lib/erb_lint/utils/offset_corrector_spec.rb +++ /dev/null @@ -1,20 +0,0 @@ -# frozen_string_literal: true - -require "spec_helper" - -describe ERBLint::Utils::OffsetCorrector do - let(:processed_source) { ERBLint::ProcessedSource.new("file.rb", '<%= "" %>') } - - it "supports node as argument" do - described_class - .new(processed_source, double(:corrector, remove: true), 0, 0..1) - .remove(node) - end - - private - - def node - parser = Parser::CurrentRuby.new(RuboCop::AST::Builder.new) - parser.parse(Parser::Source::Buffer.new("(string)").tap { |buffer| buffer.source = '""' }) - end -end