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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ModuleDeclaration.



7373
7374
7375
7376
7377
7378
# File 'lib/syntax_tree/node.rb', line 7373

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



7365
7366
7367
# File 'lib/syntax_tree/node.rb', line 7365

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7371
7372
7373
# File 'lib/syntax_tree/node.rb', line 7371

def comments
  @comments
end

#constantObject (readonly)

ConstPathRef | ConstRef | TopConstRef

the name of the module



7362
7363
7364
# File 'lib/syntax_tree/node.rb', line 7362

def constant
  @constant
end

#locationObject (readonly)

Location

the location of this node



7368
7369
7370
# File 'lib/syntax_tree/node.rb', line 7368

def location
  @location
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



7380
7381
7382
# File 'lib/syntax_tree/node.rb', line 7380

def child_nodes
  [constant, bodystmt]
end

#deconstruct_keys(keys) ⇒ Object



7386
7387
7388
7389
7390
7391
7392
7393
# File 'lib/syntax_tree/node.rb', line 7386

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

#format(q) ⇒ Object



7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
# File 'lib/syntax_tree/node.rb', line 7395

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



7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
# File 'lib/syntax_tree/node.rb', line 7424

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



7438
7439
7440
7441
7442
7443
7444
7445
7446
# File 'lib/syntax_tree/node.rb', line 7438

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