Class: SyntaxTree::ModuleDeclaration

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

ModuleDeclaration represents defining a module using the module keyword.

module Namespace
end

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(constant:, bodystmt:, location:) ⇒ ModuleDeclaration



7681
7682
7683
7684
7685
7686
# File 'lib/syntax_tree/node.rb', line 7681

def initialize(constant:, bodystmt:, location:)
  @constant = constant
  @bodystmt = bodystmt
  @location = location
  @comments = []
end

Instance Attribute Details

#bodystmtObject (readonly)

BodyStmt

the expressions to be executed in the context of the module



7676
7677
7678
# File 'lib/syntax_tree/node.rb', line 7676

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7679
7680
7681
# File 'lib/syntax_tree/node.rb', line 7679

def comments
  @comments
end

#constantObject (readonly)

ConstPathRef | ConstRef | TopConstRef

the name of the module



7673
7674
7675
# File 'lib/syntax_tree/node.rb', line 7673

def constant
  @constant
end

Instance Method Details

#===(other) ⇒ Object



7741
7742
7743
7744
# File 'lib/syntax_tree/node.rb', line 7741

def ===(other)
  other.is_a?(ModuleDeclaration) && constant === other.constant &&
    bodystmt === other.bodystmt
end

#accept(visitor) ⇒ Object



7688
7689
7690
# File 'lib/syntax_tree/node.rb', line 7688

def accept(visitor)
  visitor.visit_module(self)
end

#child_nodesObject Also known as: deconstruct



7692
7693
7694
# File 'lib/syntax_tree/node.rb', line 7692

def child_nodes
  [constant, bodystmt]
end

#copy(constant: nil, bodystmt: nil, location: nil) ⇒ Object



7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
# File 'lib/syntax_tree/node.rb', line 7696

def copy(constant: nil, bodystmt: nil, location: nil)
  node =
    ModuleDeclaration.new(
      constant: constant || self.constant,
      bodystmt: bodystmt || self.bodystmt,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



7710
7711
7712
7713
7714
7715
7716
7717
# File 'lib/syntax_tree/node.rb', line 7710

def deconstruct_keys(_keys)
  {
    constant: constant,
    bodystmt: bodystmt,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
# File 'lib/syntax_tree/node.rb', line 7719

def format(q)
  if bodystmt.empty?
    q.group do
      format_declaration(q)
      q.breakable_force
      q.text("end")
    end
  else
    q.group do
      format_declaration(q)

      q.indent do
        q.breakable_force
        q.format(bodystmt)
      end

      q.breakable_force
      q.text("end")
    end
  end
end