diff --git a/CHANGELOG.md b/CHANGELOG.md index 334bc2fbc29..5405090a33e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/rubocop/rspec/expect_offense.rb b/lib/rubocop/rspec/expect_offense.rb index db5f0f3c198..30fd29a7a72 100644 --- a/lib/rubocop/rspec/expect_offense.rb +++ b/lib/rubocop/rspec/expect_offense.rb @@ -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