Class: SyntaxTree::Defs

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

Overview

Defs represents defining a singleton method on an object.

def object.method(param) result end

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Defs.



3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
# File 'lib/syntax_tree/node.rb', line 3985

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



3980
3981
3982
# File 'lib/syntax_tree/node.rb', line 3980

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3983
3984
3985
# File 'lib/syntax_tree/node.rb', line 3983

def comments
  @comments
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



3974
3975
3976
# File 'lib/syntax_tree/node.rb', line 3974

def name
  @name
end

#operatorObject (readonly)

Op | Period

the operator being used to declare the method



3971
3972
3973
# File 'lib/syntax_tree/node.rb', line 3971

def operator
  @operator
end

#paramsObject (readonly)

Params | Paren

the parameter declaration for the method



3977
3978
3979
# File 'lib/syntax_tree/node.rb', line 3977

def params
  @params
end

#targetObject (readonly)

untyped

the target where the method is being defined



3968
3969
3970
# File 'lib/syntax_tree/node.rb', line 3968

def target
  @target
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



4003
4004
4005
# File 'lib/syntax_tree/node.rb', line 4003

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

#deconstruct_keys(keys) ⇒ Object



4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
# File 'lib/syntax_tree/node.rb', line 4009

def deconstruct_keys(keys)
  {
    target: target,
    operator: operator,
    name: name,
    params: params,
    bodystmt: bodystmt,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
# File 'lib/syntax_tree/node.rb', line 4021

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



4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
# File 'lib/syntax_tree/node.rb', line 4043

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



4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
# File 'lib/syntax_tree/node.rb', line 4066

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