Class: RuboCop::Cop::Sorbet::ForbidTStruct::Property
- Inherits:
-
Object
- Object
- RuboCop::Cop::Sorbet::ForbidTStruct::Property
- Defined in:
- lib/rubocop/cop/sorbet/forbid_t_struct.rb
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#factory ⇒ Object
readonly
Returns the value of attribute factory.
-
#kind ⇒ Object
readonly
Returns the value of attribute kind.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#node ⇒ Object
readonly
Returns the value of attribute node.
Instance Method Summary collapse
- #attr_accessor ⇒ Object
- #attr_sig ⇒ Object
-
#initialize(node, kind, name, type, default:, factory:) ⇒ Property
constructor
A new instance of Property.
- #initialize_assign ⇒ Object
- #initialize_param ⇒ Object
- #initialize_sig_param ⇒ Object
- #nilable? ⇒ Boolean
- #type ⇒ Object
Constructor Details
#initialize(node, kind, name, type, default:, factory:) ⇒ Property
Returns a new instance of Property.
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/rubocop/cop/sorbet/forbid_t_struct.rb', line 107 def initialize(node, kind, name, type, default:, factory:) @node = node @kind = kind @name = name @type = type @default = default @factory = factory # A T::Struct should have both a default and a factory, if we find one let's raise an error raise if @default && @factory end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
105 106 107 |
# File 'lib/rubocop/cop/sorbet/forbid_t_struct.rb', line 105 def default @default end |
#factory ⇒ Object (readonly)
Returns the value of attribute factory.
105 106 107 |
# File 'lib/rubocop/cop/sorbet/forbid_t_struct.rb', line 105 def factory @factory end |
#kind ⇒ Object (readonly)
Returns the value of attribute kind.
105 106 107 |
# File 'lib/rubocop/cop/sorbet/forbid_t_struct.rb', line 105 def kind @kind end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
105 106 107 |
# File 'lib/rubocop/cop/sorbet/forbid_t_struct.rb', line 105 def name @name end |
#node ⇒ Object (readonly)
Returns the value of attribute node.
105 106 107 |
# File 'lib/rubocop/cop/sorbet/forbid_t_struct.rb', line 105 def node @node end |
Instance Method Details
#attr_accessor ⇒ Object
123 124 125 |
# File 'lib/rubocop/cop/sorbet/forbid_t_struct.rb', line 123 def attr_accessor "#{kind} :#{name}" end |
#attr_sig ⇒ Object
119 120 121 |
# File 'lib/rubocop/cop/sorbet/forbid_t_struct.rb', line 119 def attr_sig "sig { returns(#{type}) }" end |
#initialize_assign ⇒ Object
144 145 146 147 148 149 |
# File 'lib/rubocop/cop/sorbet/forbid_t_struct.rb', line 144 def initialize_assign rb = String.new rb << "@#{name} = #{name}" rb << ".call" if factory rb end |
#initialize_param ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/rubocop/cop/sorbet/forbid_t_struct.rb', line 131 def initialize_param rb = String.new rb << "#{name}:" if default rb << " #{default}" elsif factory rb << " #{factory}" elsif nilable? rb << " nil" end rb end |
#initialize_sig_param ⇒ Object
127 128 129 |
# File 'lib/rubocop/cop/sorbet/forbid_t_struct.rb', line 127 def initialize_sig_param "#{name}: #{type}" end |
#nilable? ⇒ Boolean
151 152 153 |
# File 'lib/rubocop/cop/sorbet/forbid_t_struct.rb', line 151 def nilable? type.start_with?("T.nilable(") end |
#type ⇒ Object
155 156 157 158 |
# File 'lib/rubocop/cop/sorbet/forbid_t_struct.rb', line 155 def type copy = @type.gsub(/[[:space:]]+/, "").strip # Remove newlines and spaces copy.gsub(",", ", ") # Add a space after each comma end |