Class: Rubyzen::Declarations::ExpressionDeclaration
- Inherits:
-
Object
- Object
- Rubyzen::Declarations::ExpressionDeclaration
- Includes:
- Providers::ClassNameProvider, Providers::FilePathProvider, Providers::LineNumberProvider, Providers::SourceCodeProvider
- Defined in:
- lib/rubyzen/declarations/expression_declaration.rb
Overview
Represents an arbitrary Ruby value-expression node — the value a method returns, the receiver of a call, a positional argument, the value of an assignment, and so on. Wraps any AST node and exposes its “kind” through predicates, so rules can ask structural questions without touching the raw AST.
Instance Attribute Summary collapse
- #node ⇒ RuboCop::AST::Node readonly
-
#parent ⇒ Object
readonly
The declaration that produced this expression.
Instance Method Summary collapse
-
#constant? ⇒ Boolean
True if the expression is a bare constant, e.g.
-
#constant_name ⇒ String?
Returns the constant name when the expression is a constant or constructs from one.
-
#constructor? ⇒ Boolean
True if the expression is a constructor call, e.g.
-
#hash_literal? ⇒ Boolean
True if the expression is a braced Hash literal with at least one pair.
-
#initialize(node, parent) ⇒ ExpressionDeclaration
constructor
A new instance of ExpressionDeclaration.
-
#local_variable? ⇒ Boolean
True if the expression references a local variable.
-
#method_call? ⇒ Boolean
True if the expression is a method call (a
sendnode). -
#method_name ⇒ String?
Returns the called method name when the expression is a method call.
-
#name ⇒ String
Returns a short identifier: the constant, variable, or method name, falling back to the node type.
-
#string? ⇒ Boolean
True if the expression is a string literal.
-
#symbol? ⇒ Boolean
True if the expression is a symbol literal.
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) ⇒ ExpressionDeclaration
Returns a new instance of ExpressionDeclaration.
27 28 29 30 |
# File 'lib/rubyzen/declarations/expression_declaration.rb', line 27 def initialize(node, parent) @node = node @parent = parent end |
Instance Attribute Details
#node ⇒ RuboCop::AST::Node (readonly)
20 21 22 |
# File 'lib/rubyzen/declarations/expression_declaration.rb', line 20 def node @node end |
#parent ⇒ Object (readonly)
Returns the declaration that produced this expression.
23 24 25 |
# File 'lib/rubyzen/declarations/expression_declaration.rb', line 23 def parent @parent end |
Instance Method Details
#constant? ⇒ Boolean
Returns true if the expression is a bare constant, e.g. Repos::Foo.
41 42 43 |
# File 'lib/rubyzen/declarations/expression_declaration.rb', line 41 def constant? node.const_type? end |
#constant_name ⇒ String?
Returns the constant name when the expression is a constant or constructs from one.
78 79 80 81 82 83 |
# File 'lib/rubyzen/declarations/expression_declaration.rb', line 78 def constant_name return node.const_name if constant? return node.receiver.const_name if constructor? && node.receiver&.const_type? nil end |
#constructor? ⇒ Boolean
Returns true if the expression is a constructor call, e.g. Repos::Foo.new.
56 57 58 |
# File 'lib/rubyzen/declarations/expression_declaration.rb', line 56 def constructor? method_call? && node.method_name == :new end |
#hash_literal? ⇒ Boolean
Returns true if the expression is a braced Hash literal with at least one pair.
61 62 63 |
# File 'lib/rubyzen/declarations/expression_declaration.rb', line 61 def hash_literal? node.hash_type? && node.braces? && node.pairs.any? end |
#local_variable? ⇒ Boolean
Returns true if the expression references a local variable.
46 47 48 |
# File 'lib/rubyzen/declarations/expression_declaration.rb', line 46 def local_variable? node.lvar_type? end |
#method_call? ⇒ Boolean
Returns true if the expression is a method call (a send node).
51 52 53 |
# File 'lib/rubyzen/declarations/expression_declaration.rb', line 51 def method_call? node.send_type? end |
#method_name ⇒ String?
Returns the called method name when the expression is a method call.
88 89 90 |
# File 'lib/rubyzen/declarations/expression_declaration.rb', line 88 def method_name node.method_name.to_s if method_call? end |
#name ⇒ String
Returns a short identifier: the constant, variable, or method name, falling back to the node type.
36 37 38 |
# File 'lib/rubyzen/declarations/expression_declaration.rb', line 36 def name constant_name || local_variable_name || (method_call? ? method_name : node.type.to_s) end |
#string? ⇒ Boolean
Returns true if the expression is a string literal.
71 72 73 |
# File 'lib/rubyzen/declarations/expression_declaration.rb', line 71 def string? node.str_type? end |
#symbol? ⇒ Boolean
Returns true if the expression is a symbol literal.
66 67 68 |
# File 'lib/rubyzen/declarations/expression_declaration.rb', line 66 def symbol? node.sym_type? end |