Module: Docscribe::Infer::Params

Defined in:
lib/docscribe/infer/params.rb

Overview

Parameter type inference.

Class Method Summary collapse

Class Method Details

.infer_param_type(name, default_str, fallback_type: FALLBACK_TYPE, treat_options_keyword_as_hash: true) ⇒ String

Note:

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

Parameters:

  • name (String)

    parameter name as used internally (may include ‘*`, `**`, `&`, or trailing `:`)

  • default_str (String?)

    source for the default value expression

  • fallback_type (String) (defaults to: FALLBACK_TYPE)

    type returned when inference is uncertain

  • treat_options_keyword_as_hash (Boolean) (defaults to: true)

    whether ‘options:` should

Returns:

  • (String)


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: treat_options_keyword_as_hash)
end

.inferred_param_type(name, default_str, fallback_type, treat_options_keyword_as_hash:) ⇒ String

Note:

module_function: defines #inferred_param_type (visibility: private)

Infer type for a regular or keyword parameter with optional default.

Parameters:

  • name (String)

    parameter name

  • default_str (String?)

    default expression source

  • fallback_type (String)

    type returned when not special-cased

  • treat_options_keyword_as_hash (Boolean)

    whether to treat ‘options:’ as Hash

Returns:

  • (String)


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 options_keyword_type(name, treat_options_keyword_as_hash, fallback_type)
  end

  node = parse_expr(default_str)
  ty = Literals.type_from_literal(node, fallback_type: fallback_type)

  return 'Hash' if options_hash_keyword?(name, default_str, ty, treat_options_keyword_as_hash)

  ty
end

.options_hash_keyword?(name, default_str, type, treat_options_keyword_as_hash) ⇒ Boolean

Note:

module_function: defines #options_hash_keyword? (visibility: private)

Whether a keyword parameter named ‘options:’ with a hash default should be typed as Hash.

Parameters:

  • name (String)

    parameter name

  • default_str (String?)

    default expression source

  • type (String)

    inferred type

  • treat_options_keyword_as_hash (Boolean)

    whether to treat ‘options:’ as Hash

Returns:

  • (Boolean)


82
83
84
# File 'lib/docscribe/infer/params.rb', line 82

def options_hash_keyword?(name, default_str, type, treat_options_keyword_as_hash)
  treat_options_keyword_as_hash && name == 'options:' && (default_str == '{}' || type == 'Hash')
end

.options_keyword_type(name, treat_options_keyword_as_hash, fallback_type) ⇒ String

Note:

module_function: defines #options_keyword_type (visibility: private)

Return ‘Hash’ for a keyword parameter named ‘options:’ when special-cased, else fallback.

Parameters:

  • name (String)

    parameter name

  • treat_options_keyword_as_hash (Boolean)

    whether to treat ‘options:’ as Hash

  • fallback_type (String)

    type returned when not special-cased

Returns:

  • (String)


70
71
72
# File 'lib/docscribe/infer/params.rb', line 70

def options_keyword_type(name, treat_options_keyword_as_hash, fallback_type)
  treat_options_keyword_as_hash && name == 'options:' ? 'Hash' : fallback_type
end

.parse_expr(src) ⇒ Parser::AST::Node?

Note:

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.

Parameters:

  • src (String?)

    expression source

Returns:

  • (Parser::AST::Node, nil)

    if Parser::SyntaxError

  • (nil)

    if Parser::SyntaxError

Raises:

  • (Parser::SyntaxError)


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?

Note:

module_function: defines #prefix_param_type (visibility: private)

Return type for special parameter prefixes.

Parameters:

  • name (String)

    parameter name

Returns:

  • (String, nil)


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