Class: SyntaxTree::Alias

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:, comments: []) ⇒ Alias

Returns a new instance of Alias.



395
396
397
398
399
400
# File 'lib/syntax_tree/node.rb', line 395

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



393
394
395
# File 'lib/syntax_tree/node.rb', line 393

def comments
  @comments
end

#leftObject (readonly)

DynaSymbol | SymbolLiteral

the new name of the method



387
388
389
# File 'lib/syntax_tree/node.rb', line 387

def left
  @left
end

#rightObject (readonly)

DynaSymbol | SymbolLiteral

the old name of the method



390
391
392
# File 'lib/syntax_tree/node.rb', line 390

def right
  @right
end

Instance Method Details

#accept(visitor) ⇒ Object



402
403
404
# File 'lib/syntax_tree/node.rb', line 402

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

#child_nodesObject Also known as: deconstruct



406
407
408
# File 'lib/syntax_tree/node.rb', line 406

def child_nodes
  [left, right]
end

#deconstruct_keys(_keys) ⇒ Object



412
413
414
# File 'lib/syntax_tree/node.rb', line 412

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

#format(q) ⇒ Object



416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/syntax_tree/node.rb', line 416

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