Module: Docscribe::Infer::Literals
- Defined in:
- lib/docscribe/infer/literals.rb
Overview
Literal inference: map simple AST literals to type names.
Class Method Summary collapse
-
.const_type_for(node, _fallback_type) ⇒ String?
Extract a constant name from a ‘:const` node.
-
.literal_type_for(type) ⇒ String?
Map a node type symbol to a known literal type name.
-
.send_new_type_for(node, _fallback_type) ⇒ String?
Extract a type from a ‘Foo.new` send node.
-
.type_from_literal(node, fallback_type: FALLBACK_TYPE) ⇒ String
Infer a type name from a literal-like AST node.
Class Method Details
.const_type_for(node, _fallback_type) ⇒ String?
module_function: defines #const_type_for (visibility: private)
Extract a constant name from a ‘:const` node.
46 47 48 49 50 |
# File 'lib/docscribe/infer/literals.rb', line 46 def const_type_for(node, _fallback_type) return unless node.type == :const node.children.last.to_s end |
.literal_type_for(type) ⇒ String?
module_function: defines #literal_type_for (visibility: private)
Map a node type symbol to a known literal type name.
36 37 38 |
# File 'lib/docscribe/infer/literals.rb', line 36 def literal_type_for(type) LITERAL_TYPE_MAP[type] end |
.send_new_type_for(node, _fallback_type) ⇒ String?
module_function: defines #send_new_type_for (visibility: private)
Extract a type from a ‘Foo.new` send node.
58 59 60 61 62 63 64 65 |
# File 'lib/docscribe/infer/literals.rb', line 58 def send_new_type_for(node, _fallback_type) return unless node.type == :send recv, meth, = node.children return unless meth == :new && recv&.type == :const recv.children.last.to_s end |
.type_from_literal(node, fallback_type: FALLBACK_TYPE) ⇒ String
module_function: defines #type_from_literal (visibility: private)
Infer a type name from a literal-like AST node.
Supports common literal/value node types such as:
-
integers, floats, strings, symbols
-
booleans and nil
-
arrays, hashes, regexps
-
constants
-
‘Foo.new` constructor calls
If the node does not match a supported pattern, the fallback type is returned.
24 25 26 27 28 29 |
# File 'lib/docscribe/infer/literals.rb', line 24 def type_from_literal(node, fallback_type: FALLBACK_TYPE) return fallback_type unless node literal_type_for(node.type) || const_type_for(node, fallback_type) || send_new_type_for(node, fallback_type) || fallback_type end |