diff --git a/changelog/change_range_help_range_with_surrounding_space_to_allow_positional_range_argument.md b/changelog/change_range_help_range_with_surrounding_space_to_allow_positional_range_argument.md new file mode 100644 index 00000000000..5d415fbf54f --- /dev/null +++ b/changelog/change_range_help_range_with_surrounding_space_to_allow_positional_range_argument.md @@ -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][]) diff --git a/lib/rubocop/cop/mixin/range_help.rb b/lib/rubocop/cop/mixin/range_help.rb index ad05413c58b..6c29eae57cc 100644 --- a/lib/rubocop/cop/mixin/range_help.rb +++ b/lib/rubocop/cop/mixin/range_help.rb @@ -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 diff --git a/spec/rubocop/cop/range_help_spec.rb b/spec/rubocop/cop/range_help_spec.rb index ce5aa2a1d4b..e2f371804ea 100644 --- a/spec/rubocop/cop/range_help_spec.rb +++ b/spec/rubocop/cop/range_help_spec.rb @@ -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