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.



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

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



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

def comments
  @comments
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



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

def name
  @name
end

#operatorObject (readonly)

Op | Period

the operator being used to declare the method



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

def operator
  @operator
end

#parenObject (readonly)

nil | Params | Paren

the parameter declaration for the method



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

def paren
  @paren
end

#statementObject (readonly)

untyped

the expression to be executed by the method



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

def statement
  @statement
end

#targetObject (readonly)

untyped

the target where the method is being defined



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

def target
  @target
end

Instance Method Details

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

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

#deconstruct_keys(keys) ⇒ Object



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

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

#format(q) ⇒ Object



3455
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
# File 'lib/syntax_tree/node.rb', line 3455

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