Class: Rubyzen::Declarations::ReturnDeclaration

Inherits:
Object
  • Object
show all
Includes:
Providers::ClassNameProvider, Providers::FilePathProvider, Providers::LineNumberProvider, Providers::SourceCodeProvider
Defined in:
lib/rubyzen/declarations/return_declaration.rb

Overview

Represents a single point at which a method or block yields a value: the implicit final expression of its body, or an explicit return statement.

Examples:

ret = method.returns.first
ret.explicit?                 #=> false
ret.expression.hash_literal?  #=> true

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Providers::SourceCodeProvider

#source_code

Methods included from Providers::ClassNameProvider

#class_name

Methods included from Providers::LineNumberProvider

#line

Methods included from Providers::FilePathProvider

#file_path

Constructor Details

#initialize(node, parent) ⇒ ReturnDeclaration

Returns a new instance of ReturnDeclaration.

Parameters:



25
26
27
28
# File 'lib/rubyzen/declarations/return_declaration.rb', line 25

def initialize(node, parent)
  @node = node
  @parent = parent
end

Instance Attribute Details

#nodeRuboCop::AST::Node (readonly)

Returns the return node, or the implicit final-expression node.

Returns:

  • (RuboCop::AST::Node)

    the return node, or the implicit final-expression node



18
19
20
# File 'lib/rubyzen/declarations/return_declaration.rb', line 18

def node
  @node
end

#parentMethodDeclaration, BlockDeclaration (readonly)

Returns the declaration that returns this value.

Returns:



21
22
23
# File 'lib/rubyzen/declarations/return_declaration.rb', line 21

def parent
  @parent
end

Instance Method Details

#explicit?Boolean

Returns true if this is an explicit return statement.

Returns:

  • (Boolean)

    true if this is an explicit return statement



31
32
33
# File 'lib/rubyzen/declarations/return_declaration.rb', line 31

def explicit?
  node.return_type?
end

#expressionExpressionDeclaration?

The value expression being returned.

Returns:



43
44
45
46
47
48
# File 'lib/rubyzen/declarations/return_declaration.rb', line 43

def expression
  value_node = explicit? ? node.children.first : node
  return nil if value_node.nil?

  ExpressionDeclaration.new(value_node, self)
end

#implicit?Boolean

Returns true if this is the implicit final expression of the body.

Returns:

  • (Boolean)

    true if this is the implicit final expression of the body



36
37
38
# File 'lib/rubyzen/declarations/return_declaration.rb', line 36

def implicit?
  !explicit?
end