Class: SyntaxTree::Alias

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

Overview

Alias represents the use of the alias keyword with regular arguments (not global variables). The alias keyword is used to make a method respond to another name as well as the current one.

alias aliased_name name

For the example above, in the current context you can now call aliased_name and it will execute the name method. When you’re aliasing two methods, you can either provide bare words (like the example above) or you can provide symbols (note that this includes dynamic symbols like :“left-#middle-right”).

Defined Under Namespace

Classes: AliasArgumentFormatter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(left:, right:, location:, comments: []) ⇒ Alias

Returns a new instance of Alias.



409
410
411
412
413
414
# File 'lib/syntax_tree/node.rb', line 409

def initialize(left:, right:, location:, comments: [])
  @left = left
  @right = right
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



407
408
409
# File 'lib/syntax_tree/node.rb', line 407

def comments
  @comments
end

#leftObject (readonly)

DynaSymbol | SymbolLiteral

the new name of the method



398
399
400
# File 'lib/syntax_tree/node.rb', line 398

def left
  @left
end

#locationObject (readonly)

Location

the location of this node



404
405
406
# File 'lib/syntax_tree/node.rb', line 404

def location
  @location
end

#rightObject (readonly)

DynaSymbol | SymbolLiteral

the old name of the method



401
402
403
# File 'lib/syntax_tree/node.rb', line 401

def right
  @right
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



416
417
418
# File 'lib/syntax_tree/node.rb', line 416

def child_nodes
  [left, right]
end

#deconstruct_keys(keys) ⇒ Object



422
423
424
# File 'lib/syntax_tree/node.rb', line 422

def deconstruct_keys(keys)
  { left: left, right: right, location: location, comments: comments }
end

#format(q) ⇒ Object



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/syntax_tree/node.rb', line 426

def format(q)
  keyword = "alias "
  left_argument = AliasArgumentFormatter.new(left)

  q.group do
    q.text(keyword)
    q.format(left_argument, stackable: false)
    q.group do
      q.nest(keyword.length) do
        q.breakable(force: left_argument.comments.any?)
        q.format(AliasArgumentFormatter.new(right), stackable: false)
      end
    end
  end
end

#pretty_print(q) ⇒ Object



442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/syntax_tree/node.rb', line 442

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

    q.breakable
    q.pp(left)

    q.breakable
    q.pp(right)

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

#to_json(*opts) ⇒ Object



456
457
458
459
460
461
462
463
464
# File 'lib/syntax_tree/node.rb', line 456

def to_json(*opts)
  {
    type: :alias,
    left: left,
    right: right,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end