Skip to content

Commit

Permalink
Use the new Autocorrection API
Browse files Browse the repository at this point in the history
The autocorrection API was changed in Rubocop v0.87.0 (pull request rubocop/rubocop#7868).

Here, I change the superclass of `RuboCop::Cop::RSpec::Cop` from
`::RuboCop::Cop::Cop` to `::RuboCop::Cop::Base`.

This has a few consequences:

- Our `#message` methods get called with a different argument than
  before. It *can* be customized by defining a `#callback_argument`
  method, but I think it is clearer to avoid callbacks altogether.
- Our `#autocorrect` methods don't get called anymore. Instead, we
  extend `Autocorrector`, and the `corrector` is being yielded when
  calling `#add_offense`, so the code is mostly moved in there. For some
  cases, this means that some code can be removed, which is nice. For
  some cases, it means that the methods get too long, or the code
  complexity gets too high, and in those cases I chose to just call out
  to an `#autocorrect` method anyway, but of course passing the
  `corrector` and any usable local variables along.

This also means we bump the dependency of RuboCop quite a bit, from
'>= 0.68.1' to '>= 0.87.0'.
  • Loading branch information
bquorning committed Jul 9, 2020
1 parent f712dce commit a60492c
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Expand Up @@ -14,4 +14,4 @@ InternalAffairs/MethodNameEqual:
# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 104
Max: 106
12 changes: 4 additions & 8 deletions lib/rubocop/cop/rspec_rails/http_status.rb
Expand Up @@ -31,6 +31,7 @@ module Rails
# it { is_expected.to have_http_status :error }
#
class HttpStatus < Cop
extend AutoCorrector
include ConfigurableEnforcedStyle

def_node_matcher :http_status, <<-PATTERN
Expand All @@ -42,14 +43,9 @@ def on_send(node)
checker = checker_class.new(ast_node)
return unless checker.offensive?

add_offense(checker.node, message: checker.message)
end
end

def autocorrect(node)
lambda do |corrector|
checker = checker_class.new(node)
corrector.replace(node.loc.expression, checker.preferred_style)
add_offense(checker.node, message: checker.message) do |corrector|
corrector.replace(checker.node, checker.preferred_style)
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion rubocop-rspec_rails.gemspec
Expand Up @@ -36,7 +36,7 @@ Gem::Specification.new do |spec|
'documentation_uri' => 'https://rubocop-rspec.readthedocs.io/'
}

spec.add_runtime_dependency 'rubocop', '>= 0.68.1'
spec.add_runtime_dependency 'rubocop', '>= 0.87.0'

spec.add_development_dependency 'rack'
spec.add_development_dependency 'rake'
Expand Down
2 changes: 1 addition & 1 deletion tasks/cops_documentation.rake
Expand Up @@ -40,7 +40,7 @@ task generate_cops_documentation: :yard_for_generate_documentation do
]
config = config.for_cop(cop)
safe_auto_correct = config.fetch('SafeAutoCorrect', true)
autocorrect = if cop.new.support_autocorrect?
autocorrect = if cop.support_autocorrect?
"Yes #{'(Unsafe)' unless safe_auto_correct}"
else
'No'
Expand Down

0 comments on commit a60492c

Please sign in to comment.