Class: RuboCop::Sorbet::RBSParser::Signature

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/sorbet/rbs_parser.rb

Overview

One RBS signature (a #: line plus any #| continuation lines). Encapsulates the parsed structure so callers query the signature rather than re-parsing comments through the module.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(processed_source, comments) ⇒ Signature

Returns a new instance of Signature.



78
79
80
81
# File 'lib/rubocop/sorbet/rbs_parser.rb', line 78

def initialize(processed_source, comments)
  @processed_source = processed_source
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Returns the value of attribute comments.



76
77
78
# File 'lib/rubocop/sorbet/rbs_parser.rb', line 76

def comments
  @comments
end

Instance Method Details

#return_typeObject

The return type expression as a string (e.g. "String", "void", "^(Integer) -> void", "Integer | String"), or nil if the signature has no return arrow or an empty return.



86
87
88
# File 'lib/rubocop/sorbet/rbs_parser.rb', line 86

def return_type
  parsed[:expr]
end

#return_type_rangeObject

[highlight_range, replace_range] covering the return type expression, or nil if there is no return type. highlight_range covers the first token (which may span #| lines); replace_range covers the entire return expression.



94
95
96
97
98
# File 'lib/rubocop/sorbet/rbs_parser.rb', line 94

def return_type_range
  return unless parsed[:expr]

  [parsed[:highlight], parsed[:replace]]
end

#void?Boolean

True if the signature declares a void return type.

Returns:

  • (Boolean)


101
102
103
# File 'lib/rubocop/sorbet/rbs_parser.rb', line 101

def void?
  return_type == "void"
end