|
7 | 7 | with_configurations(number_of_workers: 1) do
|
8 | 8 | definitions = { modules: [], classes: [], constants: [] }
|
9 | 9 | current_context = definitions
|
10 |
| - context_stack = [] |
| 10 | + context_stack = [current_context] |
11 | 11 |
|
12 | 12 | helper_method :find_in_definitions do |names, definitions|
|
13 | 13 | return nil if names.empty? || definitions.nil?
|
|
48 | 48 | new_context = existing_module
|
49 | 49 | else
|
50 | 50 | full_name = [current_context[:full_name], name].compact.join('::')
|
51 |
| - new_context = { name: name, full_name: full_name, modules: [], classes: [], methods: [], static_methods: [], singleton: [], constants: [] } |
| 51 | + new_context = { name: name, full_name: full_name, type: "module", modules: [], classes: [], methods: [], static_methods: [], singleton: {}, constants: [] } |
52 | 52 | current_context[:modules] << new_context
|
53 | 53 | end
|
54 | 54 |
|
55 |
| - context_stack.push(current_context) |
| 55 | + context_stack.push(new_context) |
56 | 56 | current_context = new_context
|
57 | 57 | end
|
58 | 58 |
|
59 | 59 | add_callback :module_node, at: 'end' do |node|
|
60 |
| - current_context = context_stack.pop |
| 60 | + context_stack.pop |
| 61 | + current_context = context_stack.last |
61 | 62 | end
|
62 | 63 |
|
63 | 64 | add_callback :class_node, at: 'start' do |node|
|
|
68 | 69 | new_context = existing_class
|
69 | 70 | else
|
70 | 71 | full_name = [current_context[:full_name], name].compact.join('::')
|
71 |
| - new_context = { name: name, full_name: full_name, superclass: superclass, modules: [], classes: [], methods: [], static_methods: [], singleton: {}, constants: [], included_modules: [] } |
| 72 | + new_context = { name: name, full_name: full_name, type: "class", superclass: superclass, modules: [], classes: [], methods: [], static_methods: [], singleton: {}, constants: [], included_modules: [] } |
72 | 73 | current_context[:classes] << new_context
|
73 | 74 | end
|
74 | 75 |
|
75 |
| - context_stack.push(current_context) |
| 76 | + context_stack.push(new_context) |
76 | 77 | current_context = new_context
|
77 | 78 | end
|
78 | 79 |
|
79 | 80 | add_callback :class_node, at: 'end' do |node|
|
80 |
| - current_context = context_stack.pop |
| 81 | + context_stack.pop |
| 82 | + current_context = context_stack.last |
81 | 83 | end
|
82 | 84 |
|
83 | 85 | add_callback :singleton_class_node, at: 'start' do |node|
|
84 | 86 | existing_singleton = current_context[:singleton]
|
85 | 87 | if !existing_singleton.empty?
|
86 | 88 | new_context = existing_singleton
|
87 | 89 | else
|
88 |
| - new_context = { methods: [], constants: [] } |
| 90 | + new_context = { type: 'singleton', methods: [], constants: [] } |
89 | 91 | current_context[:singleton] = new_context
|
90 | 92 | end
|
91 | 93 |
|
92 |
| - context_stack.push(current_context) |
| 94 | + context_stack.push(new_context) |
93 | 95 | current_context = new_context
|
94 | 96 | end
|
95 | 97 |
|
96 | 98 | add_callback :singleton_class_node, at: 'end' do |node|
|
97 |
| - current_context = context_stack.pop |
| 99 | + context_stack.pop |
| 100 | + current_context = context_stack.last |
98 | 101 | end
|
99 | 102 |
|
100 | 103 | add_callback :constant_write_node do |node|
|
101 | 104 | current_context[:constants] << { name: node.name.to_s }
|
102 | 105 | end
|
103 | 106 |
|
104 | 107 | add_callback :call_node, at: 'start' do |node|
|
105 |
| - if node.receiver.nil? && node.name == :include |
| 108 | + if node.receiver.nil? && node.name == :include && current_context[:type] == "class" && !node.arguments.nil? && %i[constant_read_node constant_path_node].include?(node.arguments.arguments.first.type) |
106 | 109 | current_context[:included_modules] << node.arguments.arguments.first.to_source
|
107 | 110 | end
|
| 111 | + end |
108 | 112 |
|
109 |
| - if node.name == :class_eval |
110 |
| - klass_definition = find_class(node.receiver.to_source, definitions) |
111 |
| - current_context = klass_definition if klass_definition |
| 113 | + add_callback :call_node, at: 'start' do |node| |
| 114 | + # we can't handle the class_eval / included / class_methods |
| 115 | + if !node.receiver.nil? && %i[class_eval included class_methods].include?(node.name) |
| 116 | + throw(:abort) |
112 | 117 | end
|
113 | 118 | end
|
114 | 119 |
|
115 | 120 | add_callback :def_node, at: 'start' do |node|
|
116 |
| - name = node.name.to_s |
| 121 | + # we can't handle `def self.inclueded` method |
| 122 | + if !node.receiver.nil? && node.name == :included |
| 123 | + throw(:abort) |
| 124 | + end |
| 125 | + end |
| 126 | + |
| 127 | + add_callback :def_node, at: 'start' do |node| |
| 128 | + throw(:abort) unless context_stack.last[:type] |
117 | 129 |
|
| 130 | + name = node.name.to_s |
118 | 131 | new_context = { name: name }
|
119 | 132 | if node.receiver.nil?
|
120 | 133 | current_context[:methods] << new_context
|
121 | 134 | else
|
122 | 135 | current_context[:static_methods] << new_context
|
123 | 136 | end
|
124 | 137 |
|
125 |
| - context_stack.push(current_context) |
| 138 | + context_stack.push(new_context) |
126 | 139 | current_context = new_context
|
127 | 140 | end
|
128 | 141 |
|
129 | 142 | add_callback :def_node, at: 'end' do |node|
|
130 |
| - current_context = context_stack.pop |
| 143 | + context_stack.pop |
| 144 | + current_context = context_stack.last |
131 | 145 | end
|
132 | 146 | end
|
133 | 147 |
|
@@ -168,11 +182,12 @@ def add_ancestors(definitions)
|
168 | 182 | superclass = klass[:superclass]
|
169 | 183 | while superclass
|
170 | 184 | superclass_class = find_class(superclass, definitions)
|
171 |
| - ancestors << superclass_class[:full_name] |
172 | 185 | if superclass_class
|
| 186 | + ancestors << superclass_class[:full_name] |
173 | 187 | ancestors.concat(superclass_class[:included_modules]) if superclass_class[:included_modules]
|
174 | 188 | superclass = superclass_class[:superclass]
|
175 | 189 | else
|
| 190 | + ancestors << superclass |
176 | 191 | superclass = nil
|
177 | 192 | end
|
178 | 193 | end
|
|
0 commit comments