Skip to content

Commit

Permalink
Add #wrap to corrector
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre authored and bbatsov committed Apr 10, 2020
1 parent 6f65213 commit a065802
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
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

0 comments on commit a065802

Please sign in to comment.