Class: SyntaxTree::DefEndless

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

Overview

DefEndless represents defining a single-line method since Ruby 3.0+.

def method = result

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#pretty_print, #to_json

Constructor Details

#initialize(target:, operator:, name:, paren:, statement:, location:, comments: []) ⇒ DefEndless

Returns a new instance of DefEndless.



3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
# File 'lib/syntax_tree/node.rb', line 3064

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3062
3063
3064
# File 'lib/syntax_tree/node.rb', line 3062

def comments
  @comments
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



3053
3054
3055
# File 'lib/syntax_tree/node.rb', line 3053

def name
  @name
end

#operatorObject (readonly)

Op | Period

the operator being used to declare the method



3050
3051
3052
# File 'lib/syntax_tree/node.rb', line 3050

def operator
  @operator
end

#parenObject (readonly)

nil | Params | Paren

the parameter declaration for the method



3056
3057
3058
# File 'lib/syntax_tree/node.rb', line 3056

def paren
  @paren
end

#statementObject (readonly)

untyped

the expression to be executed by the method



3059
3060
3061
# File 'lib/syntax_tree/node.rb', line 3059

def statement
  @statement
end

#targetObject (readonly)

untyped

the target where the method is being defined



3047
3048
3049
# File 'lib/syntax_tree/node.rb', line 3047

def target
  @target
end

Instance Method Details

#accept(visitor) ⇒ Object



3082
3083
3084
# File 'lib/syntax_tree/node.rb', line 3082

def accept(visitor)
  visitor.visit_def_endless(self)
end

#child_nodesObject Also known as: deconstruct



3086
3087
3088
# File 'lib/syntax_tree/node.rb', line 3086

def child_nodes
  [target, operator, name, paren, statement]
end

#deconstruct_keys(keys) ⇒ Object



3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
# File 'lib/syntax_tree/node.rb', line 3092

def deconstruct_keys(keys)
  {
    target: target,
    operator: operator,
    name: name,
    paren: paren,
    statement: statement,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
# File 'lib/syntax_tree/node.rb', line 3104

def format(q)
  q.group do
    q.text("def ")

    if target
      q.format(target)
      q.format(CallOperatorFormatter.new(operator), stackable: false)
    end

    q.format(name)

    if paren
      params = paren
      params = params.contents if params.is_a?(Paren)
      q.format(paren) unless params.empty?
    end

    q.text(" =")
    q.group do
      q.indent do
        q.breakable
        q.format(statement)
      end
    end
  end
end