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.



762
763
764
765
766
767
# File 'lib/syntax_tree.rb', line 762

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



760
761
762
# File 'lib/syntax_tree.rb', line 760

def comments
  @comments
end

#leftObject (readonly)

DynaSymbol | SymbolLiteral

the new name of the method



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

def left
  @left
end

#locationObject (readonly)

Location

the location of this node



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

def location
  @location
end

#rightObject (readonly)

DynaSymbol | SymbolLiteral

the old name of the method



754
755
756
# File 'lib/syntax_tree.rb', line 754

def right
  @right
end

Instance Method Details

#child_nodesObject



769
770
771
# File 'lib/syntax_tree.rb', line 769

def child_nodes
  [left, right]
end

#format(q) ⇒ Object



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

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



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

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



803
804
805
806
807
808
809
810
811
# File 'lib/syntax_tree.rb', line 803

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