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, #pretty_print, #to_json

Constructor Details

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

Returns a new instance of AliasNode.



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

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



484
485
486
# File 'lib/syntax_tree/node.rb', line 484

def comments
  @comments
end

#leftObject (readonly)

DynaSymbol | GVar | SymbolLiteral

the new name of the method



478
479
480
# File 'lib/syntax_tree/node.rb', line 478

def left
  @left
end

#rightObject (readonly)

Backref | DynaSymbol | GVar | SymbolLiteral

the old name of the method



481
482
483
# File 'lib/syntax_tree/node.rb', line 481

def right
  @right
end

Instance Method Details

#===(other) ⇒ Object



535
536
537
# File 'lib/syntax_tree/node.rb', line 535

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [left, right]
end

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



501
502
503
504
505
506
507
508
509
510
511
# File 'lib/syntax_tree/node.rb', line 501

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



515
516
517
# File 'lib/syntax_tree/node.rb', line 515

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

#format(q) ⇒ Object



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# File 'lib/syntax_tree/node.rb', line 519

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)


539
540
541
# File 'lib/syntax_tree/node.rb', line 539

def var_alias?
  left.is_a?(GVar)
end