@@ -9,20 +9,12 @@ class ConstNodeInspector
9
9
include ConstantNameInspector
10
10
11
11
def constant_name_from_node ( node , ancestors :)
12
- return nil unless Node . type ( node ) == Node ::CONSTANT
13
-
14
- # Only process the root `const` node for namespaced constant references. For example, in the
15
- # reference `Spam::Eggs::Thing`, we only process the const node associated with `Spam`.
12
+ return nil unless Node . constant? ( node )
16
13
parent = ancestors . first
17
- return nil if parent && Node . type ( parent ) == Node ::CONSTANT
18
-
19
- if constant_in_module_or_class_definition? ( node , parent : parent )
20
- # We're defining a class with this name, in which case the constant is implicitly fully qualified by its
21
- # enclosing namespace
22
- name = Node . parent_module_name ( ancestors : ancestors )
23
- name ||= Node . enclosing_namespace_path ( node , ancestors : ancestors ) . push ( Node . constant_name ( node ) ) . join ( "::" )
14
+ return nil unless root_constant? ( parent )
24
15
25
- "::" + name
16
+ if parent && constant_in_module_or_class_definition? ( node , parent : parent )
17
+ fully_qualify_constant ( node , ancestors : ancestors )
26
18
else
27
19
begin
28
20
Node . constant_name ( node )
@@ -34,11 +26,29 @@ def constant_name_from_node(node, ancestors:)
34
26
35
27
private
36
28
29
+ # Only process the root `const` node for namespaced constant references. For example, in the
30
+ # reference `Spam::Eggs::Thing`, we only process the const node associated with `Spam`.
31
+ def root_constant? ( parent )
32
+ !( parent && Node . constant? ( parent ) )
33
+ end
34
+
37
35
def constant_in_module_or_class_definition? ( node , parent :)
38
- if parent
39
- parent_name = Node . module_name_from_definition ( parent )
40
- parent_name && parent_name == Node . constant_name ( node )
41
- end
36
+ parent_name = Node . module_name_from_definition ( parent )
37
+ parent_name && parent_name == Node . constant_name ( node )
38
+ end
39
+
40
+ def fully_qualify_constant ( node , ancestors :)
41
+ # We're defining a class with this name, in which case the constant is implicitly fully qualified by its
42
+ # enclosing namespace
43
+ name = Node . parent_module_name ( ancestors : ancestors )
44
+ name ||= generate_qualified_constant ( node , ancestors )
45
+ "::" + name
46
+ end
47
+
48
+ def generate_qualified_constant ( node , ancestors :)
49
+ namespace_path = Node . enclosing_namespace_path ( node , ancestors : ancestors )
50
+ constant_name = Node . constant_name ( node )
51
+ namespace_path . push ( constant_name ) . join ( "::" )
42
52
end
43
53
end
44
54
end
0 commit comments