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.



4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
# File 'lib/syntax_tree.rb', line 4633

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



4625
4626
4627
# File 'lib/syntax_tree.rb', line 4625

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4631
4632
4633
# File 'lib/syntax_tree.rb', line 4631

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



4628
4629
4630
# File 'lib/syntax_tree.rb', line 4628

def location
  @location
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



4619
4620
4621
# File 'lib/syntax_tree.rb', line 4619

def name
  @name
end

#operatorObject (readonly)

Op | Period

the operator being used to declare the method



4616
4617
4618
# File 'lib/syntax_tree.rb', line 4616

def operator
  @operator
end

#paramsObject (readonly)

Params | Paren

the parameter declaration for the method



4622
4623
4624
# File 'lib/syntax_tree.rb', line 4622

def params
  @params
end

#targetObject (readonly)

untyped

the target where the method is being defined



4613
4614
4615
# File 'lib/syntax_tree.rb', line 4613

def target
  @target
end

Instance Method Details

#child_nodesObject



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

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

#format(q) ⇒ Object



4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
# File 'lib/syntax_tree.rb', line 4655

def format(q)
  q.group do
    q.group do
      q.text("def ")
      q.format(target)
      q.format(CallOperatorFormatter.new(operator))
      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



4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
# File 'lib/syntax_tree.rb', line 4677

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



4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
# File 'lib/syntax_tree.rb', line 4700

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