|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
| 3 | +# ruby/parser helper parses ruby files and saves the :ruby_definitions to data. |
| 4 | +# The :ruby_definitions is an object of RubyDefinitions, |
| 5 | +# which contains all the classes, modules, methods, constants, ancestors, |
| 6 | +# and provides some methods, such as find_classes_by_superclass. |
| 7 | +Synvert::Helper.new 'ruby/parse' do |options| |
| 8 | + configure(parser: Synvert::PRISM_PARSER) |
| 9 | + |
| 10 | + # Set number_of_workers to 1 to skip parallel. |
| 11 | + with_configurations(number_of_workers: 1) do |
| 12 | + definitions = RubyDefinitions.new |
| 13 | + |
| 14 | + within_file Synvert::ALL_RUBY_FILES do |
| 15 | + add_callback :module_node, at: 'start' do |node| |
| 16 | + name = node.constant_path.to_source |
| 17 | + definitions.add_module(name) |
| 18 | + end |
| 19 | + |
| 20 | + add_callback :module_node, at: 'end' do |node| |
| 21 | + definitions.pop |
| 22 | + end |
| 23 | + |
| 24 | + add_callback :class_node, at: 'start' do |node| |
| 25 | + name = node.constant_path.to_source |
| 26 | + superclass = node.superclass&.to_source |
| 27 | + definitions.add_class(name, superclass) |
| 28 | + end |
| 29 | + |
| 30 | + add_callback :class_node, at: 'end' do |node| |
| 31 | + definitions.pop |
| 32 | + end |
| 33 | + |
| 34 | + add_callback :singleton_class_node, at: 'start' do |node| |
| 35 | + definitions.add_singleton |
| 36 | + end |
| 37 | + |
| 38 | + add_callback :singleton_class_node, at: 'end' do |node| |
| 39 | + definitions.pop |
| 40 | + end |
| 41 | + |
| 42 | + add_callback :constant_write_node do |node| |
| 43 | + definitions.add_constants(node.name.to_s) |
| 44 | + end |
| 45 | + |
| 46 | + add_callback :call_node, at: 'start' do |node| |
| 47 | + if node.receiver.nil? && node.name == :include && definitions.current_node_type == "class" && !node.arguments.nil? && %i[constant_read_node constant_path_node].include?(node.arguments.arguments.first.type) |
| 48 | + definitions.add_included_module(node.arguments.arguments.first.to_source) |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + add_callback :call_node, at: 'start' do |node| |
| 53 | + # we can't handle the class_eval / included / class_methods |
| 54 | + if !node.receiver.nil? && %i[class_eval included class_methods].include?(node.name) |
| 55 | + throw(:abort) |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + add_callback :def_node, at: 'start' do |node| |
| 60 | + # we can't handle `def self.inclueded` method |
| 61 | + if !node.receiver.nil? && node.name == :included |
| 62 | + throw(:abort) |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + add_callback :def_node, at: 'start' do |node| |
| 67 | + throw(:abort) if definitions.is_root? |
| 68 | + |
| 69 | + name = node.name.to_s |
| 70 | + if node.receiver.nil? |
| 71 | + definitions.add_method(name) |
| 72 | + else |
| 73 | + definitions.add_static_method(name) |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + add_callback :def_node, at: 'end' do |node| |
| 78 | + definitions.pop |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + definitions.setup_ancestors |
| 83 | + save_data :ruby_definitions, definitions |
| 84 | + end |
| 85 | +end |
| 86 | + |
3 | 87 | class RubyDefinitions
|
4 | 88 | attr_reader :node
|
5 | 89 |
|
@@ -247,83 +331,3 @@ def to_h
|
247 | 331 | { name: @name }
|
248 | 332 | end
|
249 | 333 | end
|
250 |
| - |
251 |
| -Synvert::Helper.new 'ruby/parse' do |options| |
252 |
| - configure(parser: Synvert::PRISM_PARSER) |
253 |
| - |
254 |
| - # Set number_of_workers to 1 to skip parallel. |
255 |
| - with_configurations(number_of_workers: 1) do |
256 |
| - definitions = RubyDefinitions.new |
257 |
| - |
258 |
| - within_file Synvert::ALL_RUBY_FILES do |
259 |
| - add_callback :module_node, at: 'start' do |node| |
260 |
| - name = node.constant_path.to_source |
261 |
| - definitions.add_module(name) |
262 |
| - end |
263 |
| - |
264 |
| - add_callback :module_node, at: 'end' do |node| |
265 |
| - definitions.pop |
266 |
| - end |
267 |
| - |
268 |
| - add_callback :class_node, at: 'start' do |node| |
269 |
| - name = node.constant_path.to_source |
270 |
| - superclass = node.superclass&.to_source |
271 |
| - definitions.add_class(name, superclass) |
272 |
| - end |
273 |
| - |
274 |
| - add_callback :class_node, at: 'end' do |node| |
275 |
| - definitions.pop |
276 |
| - end |
277 |
| - |
278 |
| - add_callback :singleton_class_node, at: 'start' do |node| |
279 |
| - definitions.add_singleton |
280 |
| - end |
281 |
| - |
282 |
| - add_callback :singleton_class_node, at: 'end' do |node| |
283 |
| - definitions.pop |
284 |
| - end |
285 |
| - |
286 |
| - add_callback :constant_write_node do |node| |
287 |
| - definitions.add_constants(node.name.to_s) |
288 |
| - end |
289 |
| - |
290 |
| - add_callback :call_node, at: 'start' do |node| |
291 |
| - if node.receiver.nil? && node.name == :include && definitions.current_node_type == "class" && !node.arguments.nil? && %i[constant_read_node constant_path_node].include?(node.arguments.arguments.first.type) |
292 |
| - definitions.add_included_module(node.arguments.arguments.first.to_source) |
293 |
| - end |
294 |
| - end |
295 |
| - |
296 |
| - add_callback :call_node, at: 'start' do |node| |
297 |
| - # we can't handle the class_eval / included / class_methods |
298 |
| - if !node.receiver.nil? && %i[class_eval included class_methods].include?(node.name) |
299 |
| - throw(:abort) |
300 |
| - end |
301 |
| - end |
302 |
| - |
303 |
| - add_callback :def_node, at: 'start' do |node| |
304 |
| - # we can't handle `def self.inclueded` method |
305 |
| - if !node.receiver.nil? && node.name == :included |
306 |
| - throw(:abort) |
307 |
| - end |
308 |
| - end |
309 |
| - |
310 |
| - add_callback :def_node, at: 'start' do |node| |
311 |
| - throw(:abort) if definitions.is_root? |
312 |
| - |
313 |
| - name = node.name.to_s |
314 |
| - if node.receiver.nil? |
315 |
| - definitions.add_method(name) |
316 |
| - else |
317 |
| - definitions.add_static_method(name) |
318 |
| - end |
319 |
| - end |
320 |
| - |
321 |
| - add_callback :def_node, at: 'end' do |node| |
322 |
| - definitions.pop |
323 |
| - end |
324 |
| - end |
325 |
| - |
326 |
| - definitions.setup_ancestors |
327 |
| - save_data :ruby_definitions, definitions |
328 |
| - end |
329 |
| -end |
|
0 commit comments