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

Let format_offense add variable-width whitespace #8113

Merged
merged 1 commit into from Jun 8, 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 @@ -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