class Reek::AST::Node
Base class for AST
nodes extended with utility methods. Contains some methods to ease the transition from Sexp to AST::Node
.
Constants
- NodeLookupRule
-
Struct representing the rules for the search performed by
each_node
method.
Attributes
Public Class Methods
Source
# File lib/reek/ast/node.rb, line 17 def initialize(type, children = [], options = {}) @comments = options.fetch(:comments, []) super end
Public Instance Methods
Source
# File lib/reek/ast/node.rb, line 112 def condition; end
Method will be overridden by the code in the IfNode, CaseNode, and LogicOperatorBase sexp extensions.
Source
# File lib/reek/ast/node.rb, line 74 def contains_nested_node?(target_type) each_node(target_type).any? end
Source
# File lib/reek/ast/node.rb, line 67 def each_node(target_types, ignoring = [], &blk) return enum_for(:each_node, target_types, ignoring) unless blk lookup_rule = NodeLookupRule.new(Array(target_types), ignoring) look_for(lookup_rule, &blk) end
Carries out a depth-first traversal of this syntax tree, yielding every Sexp of the searched for type or types. The traversal stops at any node whose type is listed in ‘ignoring`.
If a type is searched for and listed in ignoring, it will be yielded but traversal will not continue to its children.
If the root’s type is ignored, traversal does not stop, unless the root is of a target type.
Takes a block as well.
@param target_types [Symbol, Array<Symbol>] the type or types to look
for
@param ignoring [Array<Symbol>] types to ignore @param blk block to execute for every hit. Gets passed in the
matching element itself.
@example
node.each_node(:send, [:mlhs]) do |call_node| .... end node.each_node(:lvar).any? { |it| it.var_name == 'something' } node.each_node([:block]).flat_map do |elem| ... end
Returns an array with all matching nodes.
Source
# File lib/reek/ast/node.rb, line 80 def format_to_ruby if location lines = location.expression.source.split("\n").map(&:strip) case lines.length when 1 then lines.first when 2 then lines.join('; ') else [lines.first, lines.last].join(' ... ') end else to_s end end
@quality :reek:DuplicateMethodCall { max_calls: 2 } is ok for lines.first @quality :reek:FeatureEnvy
Source
# File lib/reek/ast/node.rb, line 22 def full_comment comments.map(&:text).join("\n") end
Source
# File lib/reek/ast/node.rb, line 26 def leading_comment line = location.line comment_lines = comments.select do |comment| comment.location.line < line end comment_lines.map(&:text).join("\n") end
Source
# File lib/reek/ast/node.rb, line 94 def length 1 end
Provide length for statement counting. A sexp counts as one statement.
Source
# File lib/reek/ast/node.rb, line 107 def source loc.expression.source_buffer.name end
Source
# File lib/reek/ast/node.rb, line 103 def statements [self] end
Most nodes represent only one statement (although they can have nested statements). The special type :begin exists primarily to contain more statements.
@return Array of unique outer-level statements contained in this node
Protected Instance Methods
Source
# File lib/reek/ast/node.rb, line 117 def look_for(lookup_rule, &blk) if lookup_rule.target_types.include?(type) yield self return if lookup_rule.ignoring.include?(type) end each_sexp do |elem| elem.look_for_recurse(lookup_rule, &blk) end end
See “.each_node” for documentation.
Source
# File lib/reek/ast/node.rb, line 128 def look_for_recurse(lookup_rule, &blk) yield self if lookup_rule.target_types.include?(type) return if lookup_rule.ignoring.include?(type) each_sexp do |elem| elem.look_for_recurse(lookup_rule, &blk) end end
See “.each_node” for documentation.
Private Instance Methods
Source
# File lib/reek/ast/node.rb, line 141 def each_sexp children.each { |elem| yield elem if elem.is_a? ::Parser::AST::Node } end