Class: SyntaxTree::AliasNode

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

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(left:, right:, location:) ⇒ AliasNode

Returns a new instance of AliasNode.



496
497
498
499
500
501
# File 'lib/syntax_tree/node.rb', line 496

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



494
495
496
# File 'lib/syntax_tree/node.rb', line 494

def comments
  @comments
end

#leftObject (readonly)

DynaSymbol | GVar | SymbolLiteral

the new name of the method



488
489
490
# File 'lib/syntax_tree/node.rb', line 488

def left
  @left
end

#rightObject (readonly)

Backref | DynaSymbol | GVar | SymbolLiteral

the old name of the method



491
492
493
# File 'lib/syntax_tree/node.rb', line 491

def right
  @right
end

Instance Method Details

#===(other) ⇒ Object



545
546
547
# File 'lib/syntax_tree/node.rb', line 545

def ===(other)
  other.is_a?(AliasNode) && left === other.left && right === other.right
end

#accept(visitor) ⇒ Object



503
504
505
# File 'lib/syntax_tree/node.rb', line 503

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

#child_nodesObject Also known as: deconstruct



507
508
509
# File 'lib/syntax_tree/node.rb', line 507

def child_nodes
  [left, right]
end

#copy(left: nil, right: nil, location: nil) ⇒ Object



511
512
513
514
515
516
517
518
519
520
521
# File 'lib/syntax_tree/node.rb', line 511

def copy(left: nil, right: nil, location: nil)
  node =
    AliasNode.new(
      left: left || self.left,
      right: right || self.right,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



525
526
527
# File 'lib/syntax_tree/node.rb', line 525

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

#format(q) ⇒ Object



529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# File 'lib/syntax_tree/node.rb', line 529

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
        left_argument.comments.any? ? q.breakable_force : q.breakable_space
        q.format(AliasArgumentFormatter.new(right), stackable: false)
      end
    end
  end
end

#var_alias?Boolean

Returns:

  • (Boolean)


549
550
551
# File 'lib/syntax_tree/node.rb', line 549

def var_alias?
  left.is_a?(GVar)
end