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
-
.collect_send_raise(raises, node) ⇒ void
Collect exception names from a ‘raise` or `fail` send node.
-
.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
.collect_send_raise(raises, node) ⇒ void
module_function: when included, also defines # (instance visibility: private)
This method returns an undefined value.
Collect exception names from a ‘raise` or `fail` send node.
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/docscribe/infer/raises.rb', line 65 def collect_send_raise(raises, node) recv, meth, *args = *node return 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 |
.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”]`
48 49 50 51 52 53 54 55 56 |
# File 'lib/docscribe/infer/raises.rb', line 48 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 |
# File 'lib/docscribe/infer/raises.rb', line 22 def infer_raises_from_node(node) raises = [] #: Array[String] ASTWalk.walk(node) do |n| case n.type when :resbody raises.concat(exception_names_from_rescue_list(n.children[0])) when :send collect_send_raise(raises, n) end end raises.uniq end |