Skip to content

Commit

Permalink
[rubocop#9760] Allow positional range arg in range_with_surrounding_s…
Browse files Browse the repository at this point in the history
…pace

For consistency with other methods
  • Loading branch information
pirj committed Jun 24, 2022
1 parent 4998f9e commit 1ebe60d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
@@ -0,0 +1 @@
* [#9760](https://github.com/rubocop/rubocop/issues/9760): Change RangeHelp#range_with_surrounding_space to allow passing the range as a positional argument. ([@pirj][])
10 changes: 7 additions & 3 deletions lib/rubocop/cop/mixin/range_help.rb
Expand Up @@ -51,9 +51,13 @@ def range_with_surrounding_comma(range, side = :both)
Parser::Source::Range.new(buffer, begin_pos, end_pos)
end

def range_with_surrounding_space(range:, side: :both,
newlines: true, whitespace: false,
continuations: false)
NOT_GIVEN = Module.new
def range_with_surrounding_space(range_positional = NOT_GIVEN, # rubocop:disable Metrics/ParameterLists
range: NOT_GIVEN, side: :both, newlines: true,
whitespace: false, continuations: false)

range = range_positional unless range_positional == NOT_GIVEN

buffer = @processed_source.buffer
src = buffer.source

Expand Down
50 changes: 34 additions & 16 deletions spec/rubocop/cop/range_help_spec.rb
Expand Up @@ -35,33 +35,51 @@
end

describe 'source indicated by #range_with_surrounding_space' do
subject do
obj = TestRangeHelp.new
obj.instance_exec(processed_source) { |src| @processed_source = src }
r = obj.send(:range_with_surrounding_space, range: input_range, side: side)
processed_source.buffer.source[r.begin_pos...r.end_pos]
end

let(:source) { 'f { a(2) }' }
let(:processed_source) { parse_source(source) }
let(:input_range) { Parser::Source::Range.new(processed_source.buffer, 5, 9) }
let(:obj) { TestRangeHelp.new }

context 'when side is :both' do
let(:side) { :both }
before do
obj.instance_exec(processed_source) { |src| @processed_source = src }
end

shared_examples 'works with various `side`s' do
context 'when side is :both' do
let(:side) { :both }

it { is_expected.to eq(' a(2) ') }
end

context 'when side is :left' do
let(:side) { :left }

it { is_expected.to eq(' a(2)') }
end

context 'when side is :right' do
let(:side) { :right }

it { is_expected.to eq(' a(2) ') }
it { is_expected.to eq('a(2) ') }
end
end

context 'when side is :left' do
let(:side) { :left }
context 'when passing range as a kwarg' do
subject do
r = obj.send(:range_with_surrounding_space, range: input_range, side: side)
processed_source.buffer.source[r.begin_pos...r.end_pos]
end

it { is_expected.to eq(' a(2)') }
it_behaves_like 'works with various `side`s'
end

context 'when side is :right' do
let(:side) { :right }
context 'when passing range as a positional argument' do
subject do
r = obj.send(:range_with_surrounding_space, input_range, side: side)
processed_source.buffer.source[r.begin_pos...r.end_pos]
end

it { is_expected.to eq('a(2) ') }
it_behaves_like 'works with various `side`s'
end
end

Expand Down

0 comments on commit 1ebe60d

Please sign in to comment.