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: defines #infer_param_type (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
24 25 26 27 |
# File 'lib/docscribe/infer/params.rb', line 24 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: defines #inferred_param_type (visibility: private)
Infer type for a regular or keyword parameter with optional default.
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/docscribe/infer/params.rb', line 50 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: defines #options_hash_keyword? (visibility: private)
Whether a keyword parameter named ‘options:’ with a hash default should be typed as Hash.
82 83 84 |
# File 'lib/docscribe/infer/params.rb', line 82 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: defines #options_keyword_type (visibility: private)
Return ‘Hash’ for a keyword parameter named ‘options:’ when special-cased, else fallback.
70 71 72 |
# File 'lib/docscribe/infer/params.rb', line 70 def (name, , fallback_type) && name == 'options:' ? 'Hash' : fallback_type end |
.parse_expr(src) ⇒ Parser::AST::Node?
module_function: defines #parse_expr (visibility: private)
Parse a standalone expression for parameter-default inference.
Returns nil if the expression is empty or cannot be parsed.
95 96 97 98 99 100 101 102 103 |
# File 'lib/docscribe/infer/params.rb', line 95 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 # steep:ignore nil end |
.prefix_param_type(name) ⇒ String?
module_function: defines #prefix_param_type (visibility: private)
Return type for special parameter prefixes.
34 35 36 37 38 39 40 |
# File 'lib/docscribe/infer/params.rb', line 34 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 |