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.
This commit is contained in:
Issy Long 2023-04-12 19:12:51 +01:00
parent 233db299cd
commit 519c1b46d8
No known key found for this signature in database
GPG Key ID: 8247C390DADC67D4
3 changed files with 17 additions and 21 deletions

View File

@ -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

View File

@ -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

View File

@ -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|