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.



3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
# File 'lib/syntax_tree/node.rb', line 3416

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



3414
3415
3416
# File 'lib/syntax_tree/node.rb', line 3414

def comments
  @comments
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



3405
3406
3407
# File 'lib/syntax_tree/node.rb', line 3405

def name
  @name
end

#operatorObject (readonly)

Op | Period

the operator being used to declare the method



3402
3403
3404
# File 'lib/syntax_tree/node.rb', line 3402

def operator
  @operator
end

#parenObject (readonly)

nil | Params | Paren

the parameter declaration for the method



3408
3409
3410
# File 'lib/syntax_tree/node.rb', line 3408

def paren
  @paren
end

#statementObject (readonly)

untyped

the expression to be executed by the method



3411
3412
3413
# File 'lib/syntax_tree/node.rb', line 3411

def statement
  @statement
end

#targetObject (readonly)

untyped

the target where the method is being defined



3399
3400
3401
# File 'lib/syntax_tree/node.rb', line 3399

def target
  @target
end

Instance Method Details

#accept(visitor) ⇒ Object



3434
3435
3436
# File 'lib/syntax_tree/node.rb', line 3434

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

#child_nodesObject Also known as: deconstruct



3438
3439
3440
# File 'lib/syntax_tree/node.rb', line 3438

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

#deconstruct_keys(keys) ⇒ Object



3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
# File 'lib/syntax_tree/node.rb', line 3444

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

#format(q) ⇒ Object



3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
# File 'lib/syntax_tree/node.rb', line 3456

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