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:, comments: []) ⇒ ModuleDeclaration

Returns a new instance of ModuleDeclaration.



6284
6285
6286
6287
6288
6289
# File 'lib/syntax_tree/node.rb', line 6284

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



6279
6280
6281
# File 'lib/syntax_tree/node.rb', line 6279

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6282
6283
6284
# File 'lib/syntax_tree/node.rb', line 6282

def comments
  @comments
end

#constantObject (readonly)

ConstPathRef | ConstRef | TopConstRef

the name of the module



6276
6277
6278
# File 'lib/syntax_tree/node.rb', line 6276

def constant
  @constant
end

Instance Method Details

#accept(visitor) ⇒ Object



6291
6292
6293
# File 'lib/syntax_tree/node.rb', line 6291

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

#child_nodesObject Also known as: deconstruct



6295
6296
6297
# File 'lib/syntax_tree/node.rb', line 6295

def child_nodes
  [constant, bodystmt]
end

#deconstruct_keys(_keys) ⇒ Object



6301
6302
6303
6304
6305
6306
6307
6308
# File 'lib/syntax_tree/node.rb', line 6301

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

#format(q) ⇒ Object



6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
# File 'lib/syntax_tree/node.rb', line 6310

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