Class: SyntaxTree::Defs

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

Overview

Defs represents defining a singleton method on an object.

def object.method(param) result end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target:, operator:, name:, params:, bodystmt:, location:, comments: []) ⇒ Defs

Returns a new instance of Defs.



4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
# File 'lib/syntax_tree.rb', line 4670

def initialize(
  target:,
  operator:,
  name:,
  params:,
  bodystmt:,
  location:,
  comments: []
)
  @target = target
  @operator = operator
  @name = name
  @params = params
  @bodystmt = bodystmt
  @location = location
  @comments = comments
end

Instance Attribute Details

#bodystmtObject (readonly)

BodyStmt

the expressions to be executed by the method



4662
4663
4664
# File 'lib/syntax_tree.rb', line 4662

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4668
4669
4670
# File 'lib/syntax_tree.rb', line 4668

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



4665
4666
4667
# File 'lib/syntax_tree.rb', line 4665

def location
  @location
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



4656
4657
4658
# File 'lib/syntax_tree.rb', line 4656

def name
  @name
end

#operatorObject (readonly)

Op | Period

the operator being used to declare the method



4653
4654
4655
# File 'lib/syntax_tree.rb', line 4653

def operator
  @operator
end

#paramsObject (readonly)

Params | Paren

the parameter declaration for the method



4659
4660
4661
# File 'lib/syntax_tree.rb', line 4659

def params
  @params
end

#targetObject (readonly)

untyped

the target where the method is being defined



4650
4651
4652
# File 'lib/syntax_tree.rb', line 4650

def target
  @target
end

Instance Method Details

#child_nodesObject



4688
4689
4690
# File 'lib/syntax_tree.rb', line 4688

def child_nodes
  [target, operator, name, params, bodystmt]
end

#format(q) ⇒ Object



4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
# File 'lib/syntax_tree.rb', line 4692

def format(q)
  q.group do
    q.group do
      q.text("def ")
      q.format(target)
      q.format(CallOperatorFormatter.new(operator), stackable: false)
      q.format(name)
      q.format(params) if !params.is_a?(Params) || !params.empty?
    end

    unless bodystmt.empty?
      q.indent do
        q.breakable(force: true)
        q.format(bodystmt)
      end
    end

    q.breakable(force: true)
    q.text("end")
  end
end

#pretty_print(q) ⇒ Object



4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
# File 'lib/syntax_tree.rb', line 4714

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("defs")

    q.breakable
    q.pp(target)

    q.breakable
    q.pp(operator)

    q.breakable
    q.pp(name)

    q.breakable
    q.pp(params)

    q.breakable
    q.pp(bodystmt)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
# File 'lib/syntax_tree.rb', line 4737

def to_json(*opts)
  {
    type: :defs,
    target: target,
    op: operator,
    name: name,
    params: params,
    bodystmt: bodystmt,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end