Module: Docscribe::Infer::Params
- Defined in:
- lib/docscribe/infer/params.rb
Overview
Parameter type inference.
Class Method Summary collapse
-
.infer_param_type(name, default_str, fallback_type: FALLBACK_TYPE, treat_options_keyword_as_hash: true) ⇒ String
Infer a parameter type from an internal parameter name representation and an optional default expression.
-
.inferred_param_type(name, default_str, fallback_type, treat_options_keyword_as_hash:) ⇒ String
Infer type for a regular or keyword parameter with optional default.
-
.options_hash_keyword?(name, default_str, type, treat_options_keyword_as_hash) ⇒ Boolean
Whether a keyword parameter named ‘options:’ with a hash default should be typed as Hash.
-
.options_keyword_type(name, treat_options_keyword_as_hash, fallback_type) ⇒ String
Return ‘Hash’ for a keyword parameter named ‘options:’ when special-cased, else fallback.
-
.parse_expr(src) ⇒ Parser::AST::Node?
Parse a standalone expression for parameter-default inference.
-
.prefix_param_type(name) ⇒ String?
Return type for special parameter prefixes.
Class Method Details
.infer_param_type(name, default_str, fallback_type: FALLBACK_TYPE, treat_options_keyword_as_hash: true) ⇒ String
module_function: when included, also defines #infer_param_type (instance visibility: private)
Infer a parameter type from an internal parameter name representation and an optional default expression.
Handles:
-
positional/rest/block parameter prefixes (‘*`, `**`, `&`)
-
keyword params with and without defaults
-
special-casing ‘options:` as `Hash` when enabled
-
literal defaults via AST parsing
25 26 27 28 |
# File 'lib/docscribe/infer/params.rb', line 25 def infer_param_type(name, default_str, fallback_type: FALLBACK_TYPE, treat_options_keyword_as_hash: true) prefix_param_type(name) || inferred_param_type(name, default_str, fallback_type, treat_options_keyword_as_hash: ) end |
.inferred_param_type(name, default_str, fallback_type, treat_options_keyword_as_hash:) ⇒ String
module_function: when included, also defines # (instance visibility: private)
Infer type for a regular or keyword parameter with optional default.
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/docscribe/infer/params.rb', line 53 def inferred_param_type(name, default_str, fallback_type, treat_options_keyword_as_hash:) if name.end_with?(':') && default_str.nil? return (name, , fallback_type) end node = parse_expr(default_str) ty = Literals.type_from_literal(node, fallback_type: fallback_type) return 'Hash' if (name, default_str, ty, ) ty end |
.options_hash_keyword?(name, default_str, type, treat_options_keyword_as_hash) ⇒ Boolean
module_function: when included, also defines #options_hash_keyword? (instance visibility: private)
Whether a keyword parameter named ‘options:’ with a hash default should be typed as Hash.
85 86 87 |
# File 'lib/docscribe/infer/params.rb', line 85 def (name, default_str, type, ) && name == 'options:' && (default_str == '{}' || type == 'Hash') end |
.options_keyword_type(name, treat_options_keyword_as_hash, fallback_type) ⇒ String
module_function: when included, also defines #options_keyword_type (instance visibility: private)
Return ‘Hash’ for a keyword parameter named ‘options:’ when special-cased, else fallback.
73 74 75 |
# File 'lib/docscribe/infer/params.rb', line 73 def (name, , fallback_type) && name == 'options:' ? 'Hash' : fallback_type end |
.parse_expr(src) ⇒ Parser::AST::Node?
module_function: when included, also defines #parse_expr (instance visibility: private)
Parse a standalone expression for parameter-default inference.
Returns nil if the expression is empty or cannot be parsed.
97 98 99 100 101 102 103 104 105 |
# File 'lib/docscribe/infer/params.rb', line 97 def parse_expr(src) return nil if src.nil? || src.strip.empty? buffer = Parser::Source::Buffer.new('(param)') buffer.source = src Docscribe::Parsing.parse_buffer(buffer) rescue Parser::SyntaxError nil end |
.prefix_param_type(name) ⇒ String?
module_function: when included, also defines # (instance visibility: private)
Return type for special parameter prefixes.
36 37 38 39 40 41 42 |
# File 'lib/docscribe/infer/params.rb', line 36 def prefix_param_type(name) return 'Array' if name.start_with?('*') && !name.start_with?('**') return 'Hash' if name.start_with?('**') return 'Proc' if name.start_with?('&') nil end |