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.



8333
8334
8335
8336
8337
8338
# File 'lib/syntax_tree.rb', line 8333

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



8325
8326
8327
# File 'lib/syntax_tree.rb', line 8325

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8331
8332
8333
# File 'lib/syntax_tree.rb', line 8331

def comments
  @comments
end

#constantObject (readonly)

ConstPathRef | ConstRef | TopConstRef

the name of the module



8322
8323
8324
# File 'lib/syntax_tree.rb', line 8322

def constant
  @constant
end

#locationObject (readonly)

Location

the location of this node



8328
8329
8330
# File 'lib/syntax_tree.rb', line 8328

def location
  @location
end

Instance Method Details

#child_nodesObject



8340
8341
8342
# File 'lib/syntax_tree.rb', line 8340

def child_nodes
  [constant, bodystmt]
end

#format(q) ⇒ Object



8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
# File 'lib/syntax_tree.rb', line 8344

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



8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
# File 'lib/syntax_tree.rb', line 8373

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



8387
8388
8389
8390
8391
8392
8393
8394
8395
# File 'lib/syntax_tree.rb', line 8387

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