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



7997
7998
7999
8000
8001
8002
# File 'lib/syntax_tree.rb', line 7997

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



7989
7990
7991
# File 'lib/syntax_tree.rb', line 7989

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7995
7996
7997
# File 'lib/syntax_tree.rb', line 7995

def comments
  @comments
end

#constantObject (readonly)

ConstPathRef | ConstRef | TopConstRef

the name of the module



7986
7987
7988
# File 'lib/syntax_tree.rb', line 7986

def constant
  @constant
end

#locationObject (readonly)

Location

the location of this node



7992
7993
7994
# File 'lib/syntax_tree.rb', line 7992

def location
  @location
end

Instance Method Details

#child_nodesObject



8004
8005
8006
# File 'lib/syntax_tree.rb', line 8004

def child_nodes
  [constant, bodystmt]
end

#format(q) ⇒ Object



8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
# File 'lib/syntax_tree.rb', line 8008

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



8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
# File 'lib/syntax_tree.rb', line 8037

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



8051
8052
8053
8054
8055
8056
8057
8058
8059
# File 'lib/syntax_tree.rb', line 8051

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