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.



6395
6396
6397
6398
6399
6400
# File 'lib/syntax_tree/node.rb', line 6395

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

Instance Attribute Details

#bodystmt ⇒ Object (readonly)

[BodyStmt] the expressions to be executed in the context of the module



6390
6391
6392
# File 'lib/syntax_tree/node.rb', line 6390

def bodystmt
  @bodystmt
end

#comments ⇒ Object (readonly)

[Array[ Comment | EmbDoc ]] the comments attached to this node



6393
6394
6395
# File 'lib/syntax_tree/node.rb', line 6393

def comments
  @comments
end

#constant ⇒ Object (readonly)

[ConstPathRef | ConstRef | TopConstRef] the name of the module



6387
6388
6389
# File 'lib/syntax_tree/node.rb', line 6387

def constant
  @constant
end

Instance Method Details

#accept(visitor) ⇒ Object



6402
6403
6404
# File 'lib/syntax_tree/node.rb', line 6402

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

#child_nodes ⇒ Object Also known as: deconstruct



6406
6407
6408
# File 'lib/syntax_tree/node.rb', line 6406

def child_nodes
  [constant, bodystmt]
end

#deconstruct_keys(_keys) ⇒ Object



6412
6413
6414
6415
6416
6417
6418
6419
# File 'lib/syntax_tree/node.rb', line 6412

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

#format(q) ⇒ Object



6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
# File 'lib/syntax_tree/node.rb', line 6421

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