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.



749
750
751
752
753
754
# File 'lib/syntax_tree.rb', line 749

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



747
748
749
# File 'lib/syntax_tree.rb', line 747

def comments
  @comments
end

#leftObject (readonly)

DynaSymbol | SymbolLiteral

the new name of the method



738
739
740
# File 'lib/syntax_tree.rb', line 738

def left
  @left
end

#locationObject (readonly)

Location

the location of this node



744
745
746
# File 'lib/syntax_tree.rb', line 744

def location
  @location
end

#rightObject (readonly)

DynaSymbol | SymbolLiteral

the old name of the method



741
742
743
# File 'lib/syntax_tree.rb', line 741

def right
  @right
end

Instance Method Details

#child_nodesObject



756
757
758
# File 'lib/syntax_tree.rb', line 756

def child_nodes
  [left, right]
end

#format(q) ⇒ Object



760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
# File 'lib/syntax_tree.rb', line 760

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



776
777
778
779
780
781
782
783
784
785
786
787
788
# File 'lib/syntax_tree.rb', line 776

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



790
791
792
793
794
795
796
797
798
# File 'lib/syntax_tree.rb', line 790

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