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

[#9760] Allow passing range as a positional argument to RangeHelp#range_with_surrounding_space #10748

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious – why is NOT_GIVEN a module and not e.g. a symbol?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Module.new == Module.new evaluates to false. Chances of someone passing a :not_given positional argument are nearing zero, but anyway.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thanks.

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