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.



4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
# File 'lib/syntax_tree.rb', line 4802

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



4794
4795
4796
# File 'lib/syntax_tree.rb', line 4794

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4800
4801
4802
# File 'lib/syntax_tree.rb', line 4800

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



4797
4798
4799
# File 'lib/syntax_tree.rb', line 4797

def location
  @location
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



4788
4789
4790
# File 'lib/syntax_tree.rb', line 4788

def name
  @name
end

#operatorObject (readonly)

Op | Period

the operator being used to declare the method



4785
4786
4787
# File 'lib/syntax_tree.rb', line 4785

def operator
  @operator
end

#paramsObject (readonly)

Params | Paren

the parameter declaration for the method



4791
4792
4793
# File 'lib/syntax_tree.rb', line 4791

def params
  @params
end

#targetObject (readonly)

untyped

the target where the method is being defined



4782
4783
4784
# File 'lib/syntax_tree.rb', line 4782

def target
  @target
end

Instance Method Details

#child_nodesObject



4820
4821
4822
# File 'lib/syntax_tree.rb', line 4820

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

#format(q) ⇒ Object



4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
# File 'lib/syntax_tree.rb', line 4824

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



4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
# File 'lib/syntax_tree.rb', line 4846

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



4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
# File 'lib/syntax_tree.rb', line 4869

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