From 2b54c22108e247211fe004b7795ec1e1686a1dec Mon Sep 17 00:00:00 2001 From: Ryo Nakamura Date: Thu, 7 Dec 2023 23:29:46 +0900 Subject: [PATCH] Apply suggestions from code review Co-authored-by: Koichi ITO --- lib/rubocop/cop/rails/response_parsed_body.rb | 37 +++---------------- 1 file changed, 5 insertions(+), 32 deletions(-) diff --git a/lib/rubocop/cop/rails/response_parsed_body.rb b/lib/rubocop/cop/rails/response_parsed_body.rb index 0708982cfa..184ddcb3a7 100644 --- a/lib/rubocop/cop/rails/response_parsed_body.rb +++ b/lib/rubocop/cop/rails/response_parsed_body.rb @@ -43,27 +43,12 @@ class ResponseParsedBody < Base ) PATTERN - # @!method nokogiri_html_parse_response_body?(node) - def_node_matcher :nokogiri_html_parse_response_body?, <<~PATTERN + # @!method nokogiri_html_parse_response_body(node) + def_node_matcher :nokogiri_html_parse_response_body, <<~PATTERN (send (const (const {nil? cbase} :Nokogiri) - :HTML - ) - :parse - (send - (send nil? :response) - :body - ) - ) - PATTERN - - # @!method nokogiri_html5_parse_response_body?(node) - def_node_matcher :nokogiri_html5_parse_response_body?, <<~PATTERN - (send - (const - (const {nil? cbase} :Nokogiri) - :HTML5 + ${:HTML :HTML5} ) :parse (send @@ -79,7 +64,6 @@ def on_send(node) return unless target_rails_version >= 7.1 check_nokogiri_html_parse_response_body(node) - check_nokogiri_html5_parse_response_body(node) end private @@ -100,22 +84,11 @@ def check_json_parse_response_body(node) end def check_nokogiri_html_parse_response_body(node) - return unless nokogiri_html_parse_response_body?(node) - - add_offense( - node, - message: 'Prefer `response.parsed_body` to `Nokogiri::HTML.parse(response.body)`.' - ) do |corrector| - autocorrect(corrector, node) - end - end - - def check_nokogiri_html5_parse_response_body(node) - return unless nokogiri_html5_parse_response_body?(node) + return unless (const = nokogiri_html_parse_response_body(node)) add_offense( node, - message: 'Prefer `response.parsed_body` to `Nokogiri::HTML5.parse(response.body)`.' + message: "Prefer `response.parsed_body` to `Nokogiri::#{const}.parse(response.body)`." ) do |corrector| autocorrect(corrector, node) end