Class: Reek::TreeDresser Private

Inherits:
Object
  • Object
show all
Defined in:
lib/reek/tree_dresser.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Adorns an abstract syntax tree with mix-in modules to make accessing the tree more understandable and less implementation-dependent.

Instance Method Summary collapse

Constructor Details

#initialize(klass_map = AST::ASTNodeClassMap.new) ⇒ TreeDresser

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of TreeDresser.



10
11
12
# File 'lib/reek/tree_dresser.rb', line 10

def initialize(klass_map = AST::ASTNodeClassMap.new)
  @klass_map = klass_map
end

Instance Method Details

#dress(sexp, comment_map, parent = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Recursively enhance an AST with type-dependent mixins, and comments.

See How-reek-works-internally for the big picture of how this works.

Parameters:

  • sexp (Parser::AST::Node)
    • the given sexp

  • comment_map (Hash)
    • see the documentation for SourceCode#syntax_tree

  • parent (Parser::AST::Node) (defaults to: nil)
    • the parent sexp

Returns:

  • an instance of Reek::AST::Node with type-dependent sexp extensions mixed in.



23
24
25
26
27
28
29
30
# File 'lib/reek/tree_dresser.rb', line 23

def dress(sexp, comment_map, parent = nil)
  return sexp unless sexp.is_a? ::Parser::AST::Node
  type = sexp.type
  children = sexp.children.map { |child| dress(child, comment_map, sexp) }
  comments = comment_map[sexp]
  @klass_map.klass_for(type).new(type, children,
                                 location: sexp.loc, comments: comments, parent: parent)
end