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

#pretty_print, #to_json

Constructor Details

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

Returns a new instance of ModuleDeclaration.



6193
6194
6195
6196
6197
6198
# File 'lib/syntax_tree/node.rb', line 6193

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



6188
6189
6190
# File 'lib/syntax_tree/node.rb', line 6188

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6191
6192
6193
# File 'lib/syntax_tree/node.rb', line 6191

def comments
  @comments
end

#constantObject (readonly)

ConstPathRef | ConstRef | TopConstRef

the name of the module



6185
6186
6187
# File 'lib/syntax_tree/node.rb', line 6185

def constant
  @constant
end

Instance Method Details

#accept(visitor) ⇒ Object



6200
6201
6202
# File 'lib/syntax_tree/node.rb', line 6200

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

#child_nodesObject Also known as: deconstruct



6204
6205
6206
# File 'lib/syntax_tree/node.rb', line 6204

def child_nodes
  [constant, bodystmt]
end

#deconstruct_keys(keys) ⇒ Object



6210
6211
6212
6213
6214
6215
6216
6217
# File 'lib/syntax_tree/node.rb', line 6210

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

#format(q) ⇒ Object



6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
# File 'lib/syntax_tree/node.rb', line 6219

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