Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Koichi ITO <[email protected]>
  • Loading branch information
r7kamura and koic authored Dec 7, 2023
1 parent 1f086da commit 2b54c22
Showing 1 changed file with 5 additions and 32 deletions.
37 changes: 5 additions & 32 deletions lib/rubocop/cop/rails/response_parsed_body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 2b54c22

Please sign in to comment.