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

Constructor Details

#initialize(constant:, bodystmt:, location:, comments: []) ⇒ ModuleDeclaration

Returns a new instance of ModuleDeclaration.



7112
7113
7114
7115
7116
7117
# File 'lib/syntax_tree/node.rb', line 7112

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

Instance Attribute Details

#bodystmtObject (readonly)

BodyStmt

the expressions to be executed in the context of the module



7107
7108
7109
# File 'lib/syntax_tree/node.rb', line 7107

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7110
7111
7112
# File 'lib/syntax_tree/node.rb', line 7110

def comments
  @comments
end

#constantObject (readonly)

ConstPathRef | ConstRef | TopConstRef

the name of the module



7104
7105
7106
# File 'lib/syntax_tree/node.rb', line 7104

def constant
  @constant
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



7119
7120
7121
# File 'lib/syntax_tree/node.rb', line 7119

def child_nodes
  [constant, bodystmt]
end

#deconstruct_keys(keys) ⇒ Object



7125
7126
7127
7128
7129
7130
7131
7132
# File 'lib/syntax_tree/node.rb', line 7125

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

#format(q) ⇒ Object



7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
# File 'lib/syntax_tree/node.rb', line 7134

def format(q)
  declaration = -> do
    q.group do
      q.text("module ")
      q.format(constant)
    end
  end

  if bodystmt.empty?
    q.group do
      declaration.call
      q.breakable(force: true)
      q.text("end")
    end
  else
    q.group do
      declaration.call

      q.indent do
        q.breakable(force: true)
        q.format(bodystmt)
      end

      q.breakable(force: true)
      q.text("end")
    end
  end
end

#pretty_print(q) ⇒ Object



7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
# File 'lib/syntax_tree/node.rb', line 7163

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("module")

    q.breakable
    q.pp(constant)

    q.breakable
    q.pp(bodystmt)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



7177
7178
7179
7180
7181
7182
7183
7184
7185
# File 'lib/syntax_tree/node.rb', line 7177

def to_json(*opts)
  {
    type: :module,
    constant: constant,
    bodystmt: bodystmt,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end