Class: Rubyzen::Declarations::ConstantDeclaration

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

Overview

Represents a constant assignment (+MAX = 100+) or reference (MAX).

Examples:

const = file.constants.filter(&:assignment?).first
const.name        #=> "MAX"
const.value       #=> 100
const.top_level?  #=> 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) ⇒ ConstantDeclaration

Returns a new instance of ConstantDeclaration.

Parameters:



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

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

Instance Attribute Details

#nodeRuboCop::AST::Node (readonly)

Returns:

  • (RuboCop::AST::Node)


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

def node
  @node
end

#parentFileDeclaration, ... (readonly)



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

def parent
  @parent
end

Instance Method Details

#assignment?Boolean

Returns whether this is a constant assignment (:casgn).

Returns:

  • (Boolean)


68
69
70
# File 'lib/rubyzen/declarations/constant_declaration.rb', line 68

def assignment?
  node.type == :casgn
end

#enclosing_classClassDeclaration?

Returns the enclosing Rubyzen::Declarations::ClassDeclaration, if any.

Returns:



97
98
99
100
101
# File 'lib/rubyzen/declarations/constant_declaration.rb', line 97

def enclosing_class
  find_enclosing_ast_node(:class) do |n|
    Rubyzen::Declarations::ClassDeclaration.new(n, file_declaration)
  end
end

#enclosing_moduleModuleDeclaration?

Returns the enclosing ModuleDeclaration, if any.

Returns:



113
114
115
116
117
# File 'lib/rubyzen/declarations/constant_declaration.rb', line 113

def enclosing_module
  find_enclosing_ast_node(:module) do |n|
    Rubyzen::Declarations::ModuleDeclaration.new(n, file_declaration)
  end
end

#in_class?Boolean

Returns whether this constant is defined inside a class.

Returns:

  • (Boolean)


106
107
108
# File 'lib/rubyzen/declarations/constant_declaration.rb', line 106

def in_class?
  !enclosing_class.nil?
end

#in_module?Boolean

Returns whether this constant is defined inside a module.

Returns:

  • (Boolean)


122
123
124
# File 'lib/rubyzen/declarations/constant_declaration.rb', line 122

def in_module?
  !enclosing_module.nil?
end

#nameString

Returns the constant name.

Returns:

  • (String)


33
34
35
36
37
38
39
40
# File 'lib/rubyzen/declarations/constant_declaration.rb', line 33

def name
  case node.type
  when :casgn
    node.children[1].to_s
  when :const
    node.const_name
  end
end

#reference?Boolean

Returns whether this is a constant reference (:const).

Returns:

  • (Boolean)


75
76
77
# File 'lib/rubyzen/declarations/constant_declaration.rb', line 75

def reference?
  node.type == :const
end

#scoped?Boolean

Returns whether this constant is defined inside a class or module.

Returns:

  • (Boolean)


129
130
131
# File 'lib/rubyzen/declarations/constant_declaration.rb', line 129

def scoped?
  !top_level?
end

#top_level?Boolean

Returns whether this constant is defined at file scope (not inside a class or module).

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rubyzen/declarations/constant_declaration.rb', line 82

def top_level?
  return false unless parent.is_a?(Rubyzen::Declarations::FileDeclaration)

  current_node = node
  while current_node
    current_node = current_node.parent
    return false if current_node && (current_node.type == :class || current_node.type == :module)
  end

  true
end

#valueString, ...

Returns the assigned value for constant assignments.

Returns:

  • (String, Integer, Float, Boolean, nil)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rubyzen/declarations/constant_declaration.rb', line 45

def value
  return nil unless assignment?

  value_node = node.children[2]
  return nil unless value_node

  case value_node.type
  when :str
    value_node.str_content
  when :int
    value_node.children[0]
  when :float
    value_node.children[0]
  when :true, :false
    value_node.type == :true
  else
    value_node.source
  end
end