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:
parent
233db299cd
commit
519c1b46d8
@ -32,7 +32,7 @@ module RuboCop
|
|||||||
|
|
||||||
@stanzas ||= cask_body.each_node
|
@stanzas ||= cask_body.each_node
|
||||||
.select(&:stanza?)
|
.select(&:stanza?)
|
||||||
.map { |node| Stanza.new(node, stanza_comments(node)) }
|
.map { |node| Stanza.new(node, cask_node) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def toplevel_stanzas
|
def toplevel_stanzas
|
||||||
@ -51,10 +51,6 @@ module RuboCop
|
|||||||
@sorted_toplevel_stanzas ||= sort_stanzas(toplevel_stanzas)
|
@sorted_toplevel_stanzas ||= sort_stanzas(toplevel_stanzas)
|
||||||
end
|
end
|
||||||
|
|
||||||
def stanzaify(nodes)
|
|
||||||
nodes.map { |node| Stanza.new(node, stanza_comments(node)) }
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def sort_stanzas(stanzas)
|
def sort_stanzas(stanzas)
|
||||||
@ -72,17 +68,6 @@ module RuboCop
|
|||||||
def stanza_order_index(stanza)
|
def stanza_order_index(stanza)
|
||||||
Constants::STANZA_ORDER.index(stanza.stanza_name)
|
Constants::STANZA_ORDER.index(stanza.stanza_name)
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -12,12 +12,12 @@ module RuboCop
|
|||||||
class Stanza
|
class Stanza
|
||||||
extend Forwardable
|
extend Forwardable
|
||||||
|
|
||||||
def initialize(method_node, comments)
|
def initialize(method_node, cask_node)
|
||||||
@method_node = method_node
|
@method_node = method_node
|
||||||
@comments = comments
|
@cask_node = cask_node
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :method_node, :comments
|
attr_reader :method_node, :cask_node
|
||||||
|
|
||||||
alias stanza_node method_node
|
alias stanza_node method_node
|
||||||
|
|
||||||
@ -52,6 +52,16 @@ module RuboCop
|
|||||||
stanza_group == other.stanza_group
|
stanza_group == other.stanza_group
|
||||||
end
|
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)
|
def ==(other)
|
||||||
self.class == other.class && stanza_node == other.stanza_node
|
self.class == other.class && stanza_node == other.stanza_node
|
||||||
end
|
end
|
||||||
|
|||||||
@ -31,7 +31,8 @@ module RuboCop
|
|||||||
next unless on_block.block_type?
|
next unless on_block.block_type?
|
||||||
|
|
||||||
block_contents = on_block.child_nodes.select(&:begin_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)
|
add_offenses(inner_stanzas)
|
||||||
end
|
end
|
||||||
@ -41,7 +42,7 @@ module RuboCop
|
|||||||
|
|
||||||
attr_reader :cask_block, :line_ops
|
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)
|
def add_offenses(stanzas)
|
||||||
stanzas.each_cons(2) do |stanza, next_stanza|
|
stanzas.each_cons(2) do |stanza, next_stanza|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user