Class: Rubyzen::Declarations::AssignmentDeclaration
- Inherits:
-
Object
- Object
- Rubyzen::Declarations::AssignmentDeclaration
- 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.
Instance Attribute Summary collapse
- #node ⇒ RuboCop::AST::Node readonly
- #parent ⇒ MethodDeclaration, BlockDeclaration readonly
Instance Method Summary collapse
-
#initialize(node, parent) ⇒ AssignmentDeclaration
constructor
A new instance of AssignmentDeclaration.
-
#name ⇒ String
Returns the name of the assigned local variable.
-
#value ⇒ ExpressionDeclaration?
Returns the assigned value as an expression, or
nilwhen there is no value node (e.g. the per-variable targets of a multiple assignment, a, b = foo).
Methods included from Providers::SourceCodeProvider
Methods included from Providers::ClassNameProvider
Methods included from Providers::LineNumberProvider
Methods included from Providers::FilePathProvider
Constructor Details
#initialize(node, parent) ⇒ AssignmentDeclaration
Returns a new instance of AssignmentDeclaration.
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
#node ⇒ RuboCop::AST::Node (readonly)
26 27 28 |
# File 'lib/rubyzen/declarations/assignment_declaration.rb', line 26 def node @node end |
#parent ⇒ MethodDeclaration, BlockDeclaration (readonly)
29 30 31 |
# File 'lib/rubyzen/declarations/assignment_declaration.rb', line 29 def parent @parent end |
Instance Method Details
#name ⇒ String
Returns the name of the assigned local variable.
41 42 43 |
# File 'lib/rubyzen/declarations/assignment_declaration.rb', line 41 def name node.children.first.to_s end |
#value ⇒ ExpressionDeclaration?
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).
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 |