From 519c1b46d862a77fccb04827361c5f5a92528b62 Mon Sep 17 00:00:00 2001 From: Issy Long Date: Wed, 12 Apr 2023 19:12:51 +0100 Subject: [PATCH] In-line converting nodes to stanzas; move comment detection to `Stanza` - Since comment detection is only used in `Stanza`, move it there. - The `stanzaify` method was only in `CaskBlock` since the other use of `Stanza.new` was. Since it's only used in one other place, move it to where it's used. --- .../Homebrew/rubocops/cask/ast/cask_block.rb | 17 +---------------- Library/Homebrew/rubocops/cask/ast/stanza.rb | 16 +++++++++++++--- .../Homebrew/rubocops/cask/stanza_grouping.rb | 5 +++-- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/Library/Homebrew/rubocops/cask/ast/cask_block.rb b/Library/Homebrew/rubocops/cask/ast/cask_block.rb index a67160d2bd..0d4bb1bbc0 100644 --- a/Library/Homebrew/rubocops/cask/ast/cask_block.rb +++ b/Library/Homebrew/rubocops/cask/ast/cask_block.rb @@ -32,7 +32,7 @@ module RuboCop @stanzas ||= cask_body.each_node .select(&:stanza?) - .map { |node| Stanza.new(node, stanza_comments(node)) } + .map { |node| Stanza.new(node, cask_node) } end def toplevel_stanzas @@ -51,10 +51,6 @@ module RuboCop @sorted_toplevel_stanzas ||= sort_stanzas(toplevel_stanzas) end - def stanzaify(nodes) - nodes.map { |node| Stanza.new(node, stanza_comments(node)) } - end - private def sort_stanzas(stanzas) @@ -72,17 +68,6 @@ module RuboCop def stanza_order_index(stanza) Constants::STANZA_ORDER.index(stanza.stanza_name) end - - def stanza_comments(stanza_node) - stanza_node.each_node.reduce([]) do |comments, node| - comments | comments_hash[node.loc] - end - end - - def comments_hash - @comments_hash ||= Parser::Source::Comment - .associate_locations(cask_node, comments) - end end end end diff --git a/Library/Homebrew/rubocops/cask/ast/stanza.rb b/Library/Homebrew/rubocops/cask/ast/stanza.rb index 88badac8cd..bc82f9b0f8 100644 --- a/Library/Homebrew/rubocops/cask/ast/stanza.rb +++ b/Library/Homebrew/rubocops/cask/ast/stanza.rb @@ -12,12 +12,12 @@ module RuboCop class Stanza extend Forwardable - def initialize(method_node, comments) + def initialize(method_node, cask_node) @method_node = method_node - @comments = comments + @cask_node = cask_node end - attr_reader :method_node, :comments + attr_reader :method_node, :cask_node alias stanza_node method_node @@ -52,6 +52,16 @@ module RuboCop stanza_group == other.stanza_group end + def stanza_comments(stanza_node) + stanza_node.each_node.reduce([]) do |comments, node| + comments | comments_hash[node.loc] + end + end + + def comments_hash + @comments_hash ||= Parser::Source::Comment.associate_locations(cask_node, stanza_comments(stanza_node)) + end + def ==(other) self.class == other.class && stanza_node == other.stanza_node end diff --git a/Library/Homebrew/rubocops/cask/stanza_grouping.rb b/Library/Homebrew/rubocops/cask/stanza_grouping.rb index a38786b308..116cd4a522 100644 --- a/Library/Homebrew/rubocops/cask/stanza_grouping.rb +++ b/Library/Homebrew/rubocops/cask/stanza_grouping.rb @@ -31,7 +31,8 @@ module RuboCop next unless on_block.block_type? block_contents = on_block.child_nodes.select(&:begin_type?) - inner_stanzas = stanzaify(block_contents.map(&:child_nodes).flatten.select(&:send_type?)) + inner_nodes = block_contents.map(&:child_nodes).flatten.select(&:send_type?) + inner_stanzas = inner_nodes.map { |node| RuboCop::Cask::AST::Stanza.new(node, cask_node) } add_offenses(inner_stanzas) end @@ -41,7 +42,7 @@ module RuboCop attr_reader :cask_block, :line_ops - def_delegators :cask_block, :cask_node, :stanzaify, :toplevel_stanzas + def_delegators :cask_block, :cask_node, :toplevel_stanzas def add_offenses(stanzas) stanzas.each_cons(2) do |stanza, next_stanza|