Class: SyntaxTree::DefEndless

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

Overview

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

def method = result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of DefEndless.



4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
# File 'lib/syntax_tree.rb', line 4397

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



4395
4396
4397
# File 'lib/syntax_tree.rb', line 4395

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



4392
4393
4394
# File 'lib/syntax_tree.rb', line 4392

def location
  @location
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



4383
4384
4385
# File 'lib/syntax_tree.rb', line 4383

def name
  @name
end

#operatorObject (readonly)

Op | Period

the operator being used to declare the method



4380
4381
4382
# File 'lib/syntax_tree.rb', line 4380

def operator
  @operator
end

#parenObject (readonly)

nil | Paren

the parameter declaration for the method



4386
4387
4388
# File 'lib/syntax_tree.rb', line 4386

def paren
  @paren
end

#statementObject (readonly)

untyped

the expression to be executed by the method



4389
4390
4391
# File 'lib/syntax_tree.rb', line 4389

def statement
  @statement
end

#targetObject (readonly)

untyped

the target where the method is being defined



4377
4378
4379
# File 'lib/syntax_tree.rb', line 4377

def target
  @target
end

Instance Method Details

#child_nodesObject



4415
4416
4417
# File 'lib/syntax_tree.rb', line 4415

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

#format(q) ⇒ Object



4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
# File 'lib/syntax_tree.rb', line 4419

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

    if target
      q.format(target)
      q.format(CallOperatorFormatter.new(operator))
    end

    q.format(name)
    q.format(paren) if paren && !paren.contents.empty?

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

#pretty_print(q) ⇒ Object



4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
# File 'lib/syntax_tree.rb', line 4441

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("def_endless")

    if target
      q.breakable
      q.pp(target)

      q.breakable
      q.pp(operator)
    end

    q.breakable
    q.pp(name)

    if paren
      q.breakable
      q.pp(paren)
    end

    q.breakable
    q.pp(statement)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
# File 'lib/syntax_tree.rb', line 4468

def to_json(*opts)
  {
    type: :def_endless,
    name: name,
    paren: paren,
    stmt: statement,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end