From 2f3fe50201a9c9aca690526bd164970853eda68c Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 11 Feb 2023 02:40:24 +0900 Subject: [PATCH] Make custom cop inherit `RuboCop::Cop::Base` Follow https://github.com/rubocop/rubocop/pull/7868. The legacy `RuboCop::Cop::Cop` API is soft deprecated and this PR makes custom cop inherit `RuboCop::Cop::Base` API instead. > maintain any RuboCop extensions, as the legacy API > will be removed in RuboCop 2.0. https://metaredux.com/posts/2020/10/21/rubocop-1-0.html --- rubocop/jekyll/assert_equal_literal_actual.rb | 15 ++++++++------- rubocop/jekyll/no_p_allowed.rb | 11 +++++------ rubocop/jekyll/no_puts_allowed.rb | 11 +++++------ 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/rubocop/jekyll/assert_equal_literal_actual.rb b/rubocop/jekyll/assert_equal_literal_actual.rb index 5b66569275b..7c7b9f5a71a 100644 --- a/rubocop/jekyll/assert_equal_literal_actual.rb +++ b/rubocop/jekyll/assert_equal_literal_actual.rb @@ -26,8 +26,11 @@ module Jekyll # @alpha.omega # ) # - class AssertEqualLiteralActual < Cop - MSG = "Provide the 'expected value' as the first argument to `assert_equal`.".freeze + class AssertEqualLiteralActual < Base + extend AutoCorrector + + MSG = "Provide the 'expected value' as the first argument to `assert_equal`." + RESTRICT_ON_SEND = %i[assert_equal].freeze SIMPLE_LITERALS = %i( true @@ -61,12 +64,10 @@ class AssertEqualLiteralActual < Cop def on_send(node) return unless literal_actual?(node) || literal_actual_with_msg?(node) - add_offense(node, location: :expression) - end - def autocorrect(node) - lambda do |corrector| - corrector.replace(node.loc.expression, replacement(node)) + range = node.loc.expression + add_offense(range) do |corrector| + corrector.replace(range, replacement(node)) end end diff --git a/rubocop/jekyll/no_p_allowed.rb b/rubocop/jekyll/no_p_allowed.rb index cc7d997b71f..b77f8f83c3d 100644 --- a/rubocop/jekyll/no_p_allowed.rb +++ b/rubocop/jekyll/no_p_allowed.rb @@ -1,20 +1,19 @@ # frozen_string_literal: true -require "rubocop" - module RuboCop module Cop module Jekyll - class NoPAllowed < Cop - MSG = "Avoid using `p` to print things. Use `Jekyll.logger` instead.".freeze + class NoPAllowed < Base + MSG = "Avoid using `p` to print things. Use `Jekyll.logger` instead." + RESTRICT_ON_SEND = %i[p].freeze def_node_search :p_called?, <<-PATTERN - (send _ :p _) + (send _ :p _) PATTERN def on_send(node) if p_called?(node) - add_offense(node, :location => :selector) + add_offense(node.loc.selector) end end end diff --git a/rubocop/jekyll/no_puts_allowed.rb b/rubocop/jekyll/no_puts_allowed.rb index a666aacbfc3..81717cc3c70 100644 --- a/rubocop/jekyll/no_puts_allowed.rb +++ b/rubocop/jekyll/no_puts_allowed.rb @@ -1,20 +1,19 @@ # frozen_string_literal: true -require "rubocop" - module RuboCop module Cop module Jekyll - class NoPutsAllowed < Cop - MSG = "Avoid using `puts` to print things. Use `Jekyll.logger` instead.".freeze + class NoPutsAllowed < Base + MSG = "Avoid using `puts` to print things. Use `Jekyll.logger` instead." + RESTRICT_ON_SEND = %i[puts].freeze def_node_search :puts_called?, <<-PATTERN - (send nil? :puts _) + (send nil? :puts _) PATTERN def on_send(node) if puts_called?(node) - add_offense(node, :location => :selector) + add_offense(node.loc.selector) end end end