Class: SyntaxTree::ModuleDeclaration

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

Overview

ModuleDeclaration represents defining a module using the module keyword.

module Namespace
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ModuleDeclaration.



8410
8411
8412
8413
8414
8415
# File 'lib/syntax_tree.rb', line 8410

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



8402
8403
8404
# File 'lib/syntax_tree.rb', line 8402

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8408
8409
8410
# File 'lib/syntax_tree.rb', line 8408

def comments
  @comments
end

#constantObject (readonly)

ConstPathRef | ConstRef | TopConstRef

the name of the module



8399
8400
8401
# File 'lib/syntax_tree.rb', line 8399

def constant
  @constant
end

#locationObject (readonly)

Location

the location of this node



8405
8406
8407
# File 'lib/syntax_tree.rb', line 8405

def location
  @location
end

Instance Method Details

#child_nodesObject



8417
8418
8419
# File 'lib/syntax_tree.rb', line 8417

def child_nodes
  [constant, bodystmt]
end

#format(q) ⇒ Object



8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
# File 'lib/syntax_tree.rb', line 8421

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



8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
# File 'lib/syntax_tree.rb', line 8450

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



8464
8465
8466
8467
8468
8469
8470
8471
8472
# File 'lib/syntax_tree.rb', line 8464

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