Sha256: f5635fad9d31e2b78cf942b553eeef0417cb4cbec6f3505ff2c72a0f6ef7d456

Contents?: true

Size: 1.84 KB

Versions: 28

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

require_relative 'ast/ast_node_class_map'

module Reek
  #
  # Adorns an abstract syntax tree with mix-in modules to make accessing
  # the tree more understandable and less implementation-dependent.
  #
  class TreeDresser
    def initialize(klass_map: AST::ASTNodeClassMap.new)
      @klass_map = klass_map
    end

    # Recursively enhance an AST with type-dependent mixins, and comments.
    #
    # See {file:docs/How-reek-works-internally.md} 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.
    #
    # @param sexp [Parser::AST::Node] the given sexp
    # @param comment_map [Hash] see the documentation for SourceCode#syntax_tree
    #
    # @return an instance of Reek::AST::Node with type-dependent sexp extensions mixed in.
    #
    # @quality :reek:FeatureEnvy
    # @quality :reek:TooManyStatements { max_statements: 6 }
    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

    private

    attr_reader :klass_map
  end
end

Version data entries

28 entries across 26 versions & 2 rubygems

Version Path
reek-6.4.0 lib/reek/tree_dresser.rb
reek-6.3.0 lib/reek/tree_dresser.rb
reek-6.2.0 lib/reek/tree_dresser.rb
reek-6.1.4 lib/reek/tree_dresser.rb
reek-6.1.3 lib/reek/tree_dresser.rb
reek-6.1.2 lib/reek/tree_dresser.rb
reek-6.1.1 lib/reek/tree_dresser.rb
reek-6.1.0 lib/reek/tree_dresser.rb
reek-6.0.6 lib/reek/tree_dresser.rb
reek-6.0.5 lib/reek/tree_dresser.rb
reek-6.0.4 lib/reek/tree_dresser.rb
reek-6.0.3 lib/reek/tree_dresser.rb
reek-6.0.2 lib/reek/tree_dresser.rb
reek-6.0.1 lib/reek/tree_dresser.rb
reek-6.0.0 lib/reek/tree_dresser.rb
reek-5.6.0 lib/reek/tree_dresser.rb
reek-5.5.0 lib/reek/tree_dresser.rb
reek-5.4.1 lib/reek/tree_dresser.rb
reek-5.4.0 lib/reek/tree_dresser.rb
reek-5.3.2 lib/reek/tree_dresser.rb