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

Add #wrap to corrector #7862

Merged
merged 1 commit into from Apr 10, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

### New features

* [#7862](https://github.com/rubocop-hq/rubocop/issues/7862): Corrector now has a `wrap` method. ([@marcandre][])
* [#7850](https://github.com/rubocop-hq/rubocop/issues/7850): Make it possible to enable/disable pending cops. ([@koic][])

### Bug fixes
Expand Down
10 changes: 10 additions & 0 deletions lib/rubocop/cop/corrector.rb
Expand Up @@ -101,6 +101,16 @@ def insert_after(range, content)
@source_rewriter.insert_after(range, content)
end

# Wraps the given source range with the given before and after texts
#
# @param [Parser::Source::Range] range
# @param [String] before
# @param [String] after
def wrap(range, before, after)
validate_range range
@source_rewriter.wrap(range, before, after)
end

# Replaces the code of the source range `range` with `content`.
#
# @param [Parser::Source::Range] range
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/corrector_spec.rb
Expand Up @@ -50,6 +50,12 @@ def do_rewrite(corrections = nil, &block)
end.to rewrite_to 'true and nil; false'
end

it 'allows insertion before and after a source range' do
expect do |corrector|
corrector.wrap(operator, '(', ')')
end.to rewrite_to 'true (and) false'
end

it 'allows replacement of a range' do
expect { |c| c.replace(operator, 'or') }.to rewrite_to 'true or false'
end
Expand Down