Class: Reek::TreeDresser

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of TreeDresser.



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

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

Instance Attribute Details

#klass_mapObject (readonly, private)

Returns the value of attribute klass_map.



54
55
56
# File 'lib/reek/tree_dresser.rb', line 54

def klass_map
  @klass_map
end

Instance Method Details

#dress(sexp, comment_map) ⇒ Object

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

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

class Klazz; def meth(argument); argument.call_me; end; end

corresponds to this sexp:

(class
  (const nil :Klazz) nil
  (def :meth
    (args
      (arg :argument))
    (send
      (lvar :argument) :call_me)))

where every node is of type Parser::AST::Node. Passing this into ‘dress` will return the exact same structure, but this time the nodes will contain type-dependent mixins, e.g. this:

 (const nil :Klazz)
will be of type Reek::AST::Node with  Reek::AST::SexpExtensions::ConstNode mixed in.

Parameters:

  • sexp (Parser::AST::Node)

    the given sexp

  • comment_map (Hash)

    see the documentation for SourceCode#syntax_tree

Returns:

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



42
43
44
45
46
47
48
49
50
# File 'lib/reek/tree_dresser.rb', line 42

def dress(sexp, comment_map)
  return sexp unless sexp.is_a? ::Parser::AST::Node

  type = sexp.type
  children = sexp.children.map { |child| dress(child, comment_map) }
  comments = comment_map[sexp]
  klass_map.klass_for(type).new(type, children,
                                location: sexp.loc, comments: comments)
end