Skip to content

Commit

Permalink
Merge pull request #8113 from eugeneius/format_offense_whitespace
Browse files Browse the repository at this point in the history
Let format_offense add variable-width whitespace
  • Loading branch information
koic committed Jun 8, 2020
2 parents fb2d023 + c5a5b9e commit 1f461d4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
### New features

* [#8111](https://github.com/rubocop-hq/rubocop/pull/8111): Add auto-correct for `Style/StructInheritance`. ([@tejasbubane][])
* [#8113](https://github.com/rubocop-hq/rubocop/pull/8113): Let `expect_offense` templates add variable-length whitespace with `_{foo}`. ([@eugeneius][])

## 0.85.1 (2020-06-07)

Expand Down
14 changes: 12 additions & 2 deletions lib/rubocop/rspec/expect_offense.rb
Expand Up @@ -72,19 +72,29 @@ module RSpec
#
# expect_no_corrections
#
# If your code has variables of different lengths, you can use `%{foo}`
# and `^{foo}` to format your template:
# If your code has variables of different lengths, you can use `%{foo}`,
# `^{foo}`, and `_{foo}` to format your template:
#
# %w[raise fail].each do |keyword|
# expect_offense(<<~RUBY, keyword: keyword)
# %{keyword}(RuntimeError, msg)
# ^{keyword}^^^^^^^^^^^^^^^^^^^ Redundant `RuntimeError` argument can be removed.
# RUBY
#
# %w[has_one has_many].each do |type|
# expect_offense(<<~RUBY, type: type)
# class Book
# %{type} :chapter, foreign_key: 'book_id'
# _{type} ^^^^^^^^^^^^^^^^^^^^^^ Specifying the default value is redundant.
# end
# RUBY
# end
module ExpectOffense
def format_offense(source, **replacements)
replacements.each do |keyword, value|
source = source.gsub("%{#{keyword}}", value)
.gsub("^{#{keyword}}", '^' * value.size)
.gsub("_{#{keyword}}", ' ' * value.size)
end
source
end
Expand Down

0 comments on commit 1f461d4

Please sign in to comment.