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

Support new Rubocop 0.84 interface for correctors #172

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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