Module: Docscribe::Infer::Raises
- Defined in:
- lib/docscribe/infer/raises.rb
Overview
Exception inference from AST (‘raise`/`fail` calls and `rescue` clauses).
Class Method Summary collapse
-
.exception_names_from_rescue_list(exc_list) ⇒ Array<String>
Extract exception class names from a rescue exception list.
-
.infer_raises_from_node(node) ⇒ Array<String>
Infer exception class names raised or rescued within a node.
Class Method Details
.exception_names_from_rescue_list(exc_list) ⇒ Array<String>
module_function: when included, also defines #exception_names_from_rescue_list (instance visibility: private)
Extract exception class names from a rescue exception list.
Examples:
-
nil => ‘[StandardError]`
-
‘Foo` => `[“Foo”]`
-
‘[Foo, Bar]` => `[“Foo”, “Bar”]`
57 58 59 60 61 62 63 64 65 |
# File 'lib/docscribe/infer/raises.rb', line 57 def exception_names_from_rescue_list(exc_list) if exc_list.nil? [DEFAULT_ERROR] elsif exc_list.type == :array exc_list.children.map { |e| Names.const_full_name(e) || DEFAULT_ERROR } else [Names.const_full_name(exc_list) || DEFAULT_ERROR] end end |
.infer_raises_from_node(node) ⇒ Array<String>
module_function: when included, also defines #infer_raises_from_node (instance visibility: private)
Infer exception class names raised or rescued within a node.
Sources considered:
-
‘rescue Foo, Bar`
-
bare ‘rescue` (=> StandardError)
-
‘raise Foo`
-
bare ‘raise` / `fail` (=> StandardError)
Returns unique exception names in discovery order.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/docscribe/infer/raises.rb', line 22 def infer_raises_from_node(node) raises = [] ASTWalk.walk(node) do |n| case n.type when :resbody exc_list = n.children[0] raises.concat(exception_names_from_rescue_list(exc_list)) when :send recv, meth, *args = *n next unless recv.nil? && %i[raise fail].include?(meth) if args.empty? raises << DEFAULT_ERROR else c = Names.const_full_name(args[0]) raises << (c || DEFAULT_ERROR) end end end raises.uniq end |