Class: SyntaxTree::Alias

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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.



711
712
713
714
715
716
# File 'lib/syntax_tree.rb', line 711

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



709
710
711
# File 'lib/syntax_tree.rb', line 709

def comments
  @comments
end

#leftObject (readonly)

DynaSymbol | SymbolLiteral

the new name of the method



700
701
702
# File 'lib/syntax_tree.rb', line 700

def left
  @left
end

#locationObject (readonly)

Location

the location of this node



706
707
708
# File 'lib/syntax_tree.rb', line 706

def location
  @location
end

#rightObject (readonly)

DynaSymbol | SymbolLiteral

the old name of the method



703
704
705
# File 'lib/syntax_tree.rb', line 703

def right
  @right
end

Instance Method Details

#child_nodesObject



718
719
720
# File 'lib/syntax_tree.rb', line 718

def child_nodes
  [left, right]
end

#format(q) ⇒ Object



722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
# File 'lib/syntax_tree.rb', line 722

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

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

#pretty_print(q) ⇒ Object



738
739
740
741
742
743
744
745
746
747
748
749
750
# File 'lib/syntax_tree.rb', line 738

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



752
753
754
755
756
757
758
759
760
# File 'lib/syntax_tree.rb', line 752

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