Class: Rubyzen::Declarations::ExpressionDeclaration

Inherits:
Object
  • Object
show all
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.

Examples:

expr = call_site.receiver_expression
expr.constructor?     #=> true   (for Repos::Foo.new.bar)
expr.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) ⇒ ExpressionDeclaration

Returns a new instance of ExpressionDeclaration.

Parameters:

  • node (RuboCop::AST::Node)

    the value-expression node

  • parent (Object)

    the declaration that produced this expression



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

#nodeRuboCop::AST::Node (readonly)

Returns:

  • (RuboCop::AST::Node)


20
21
22
# File 'lib/rubyzen/declarations/expression_declaration.rb', line 20

def node
  @node
end

#parentObject (readonly)

Returns the declaration that produced this expression.

Returns:

  • (Object)

    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.

Returns:

  • (Boolean)

    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_nameString?

Returns the constant name when the expression is a constant or constructs from one.

Returns:

  • (String, nil)

    e.g. “Repos::Foo” for both Repos::Foo and Repos::Foo.new



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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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).

Returns:

  • (Boolean)

    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_nameString?

Returns the called method name when the expression is a method call.

Returns:

  • (String, nil)


88
89
90
# File 'lib/rubyzen/declarations/expression_declaration.rb', line 88

def method_name
  node.method_name.to_s if method_call?
end

#nameString

Returns a short identifier: the constant, variable, or method name, falling back to the node type.

Returns:

  • (String)


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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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