Class: Rubyzen::Declarations::AssignmentDeclaration

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

Overview

Represents a local-variable assignment (an lvasgn node), e.g. x = Repos::Foo.new.

NOTE: Multiple assignment (+a, b = …+) is only partially modelled. Each target is surfaced as its own AssignmentDeclaration with a correct #name, but #value is nil: in the AST the right-hand side lives on the enclosing masgn node, not on the per-target lvasgn, and which value each variable receives generally cannot be known statically (e.g. a, b = build_pair). If a rule ever needs to trace destructured assignments, model the shared source explicitly (a multiple_assignment? predicate + shared_source) rather than attributing the shared right-hand side to each variable’s #value.

Examples:

assignment = method.assignments.first
assignment.name                 #=> "x"
assignment.value.constructor?   #=> true
assignment.value.constant_name  #=> "Repos::Foo"

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) ⇒ AssignmentDeclaration

Returns a new instance of AssignmentDeclaration.

Parameters:



33
34
35
36
# File 'lib/rubyzen/declarations/assignment_declaration.rb', line 33

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

Instance Attribute Details

#nodeRuboCop::AST::Node (readonly)

Returns:

  • (RuboCop::AST::Node)


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

def node
  @node
end

#parentMethodDeclaration, BlockDeclaration (readonly)



29
30
31
# File 'lib/rubyzen/declarations/assignment_declaration.rb', line 29

def parent
  @parent
end

Instance Method Details

#nameString

Returns the name of the assigned local variable.

Returns:

  • (String)

    e.g. “x”



41
42
43
# File 'lib/rubyzen/declarations/assignment_declaration.rb', line 41

def name
  node.children.first.to_s
end

#valueExpressionDeclaration?

Returns the assigned value as an expression, or nil when there is no value node (e.g. the per-variable targets of a multiple assignment, a, b = foo).

Returns:



49
50
51
52
53
54
# File 'lib/rubyzen/declarations/assignment_declaration.rb', line 49

def value
  value_node = node.children[1]
  return nil if value_node.nil?

  ExpressionDeclaration.new(value_node, self)
end