Skip to content

Commit

Permalink
Support new Rubocop 0.84 interface for correctors
Browse files Browse the repository at this point in the history
Rubocop changed interface and after
rubocop/rubocop#7863 it requires correctors
to accept nodes.

Closes Shopify#162
  • Loading branch information
pftg committed May 22, 2020
1 parent b1e657a commit 8beb0f0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/erb_lint/utils/offset_corrector.rb
Expand Up @@ -38,7 +38,9 @@ def remove_trailing(range, size)
@corrector.remove_trailing(range_with_offset(range), size)
end

def range_with_offset(range)
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))
)
Expand All @@ -50,6 +52,21 @@ def bound(pos)
@bound_range.max,
].min
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
25 changes: 25 additions & 0 deletions spec/erb_lint/linters/rubocop_spec.rb
Expand Up @@ -95,6 +95,31 @@

it { expect(subject).to(eq("<%= safe_method %>\n")) }
end

context 'when autocorrecting from rubocop cops' do
let(:file) { <<~FILE }
<%= 'should_be_double_quoted' %>
FILE

let(:linter_config) do
described_class.config_schema.new(
only: ['Style/StringLiterals'],
rubocop_config: {
AllCops: {
TargetRubyVersion: '2.7',
},
'Style/StringLiterals': {
EnforcedStyle: 'double_quotes',
Enabled: true,
},
},
)
end

subject { corrected_content }

it { expect(subject).to(eq(%(<%= "should_be_double_quoted" %>\n))) }
end
end

context 'when multiple offenses are found in the same block' do
Expand Down
20 changes: 20 additions & 0 deletions spec/lib/erb_lint/utils/offset_corrector_spec.rb
@@ -0,0 +1,20 @@
# 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

0 comments on commit 8beb0f0

Please sign in to comment.