Class: RuboCop::Cop::Sorbet::ForbidTStruct::Property

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#defaultObject (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

#factoryObject (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

#kindObject (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

#nameObject (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

#nodeObject (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_accessorObject



123
124
125
# File 'lib/rubocop/cop/sorbet/forbid_t_struct.rb', line 123

def attr_accessor
  "#{kind} :#{name}"
end

#attr_sigObject



119
120
121
# File 'lib/rubocop/cop/sorbet/forbid_t_struct.rb', line 119

def attr_sig
  "sig { returns(#{type}) }"
end

#initialize_assignObject



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_paramObject



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_paramObject



127
128
129
# File 'lib/rubocop/cop/sorbet/forbid_t_struct.rb', line 127

def initialize_sig_param
  "#{name}: #{type}"
end

#nilable?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/rubocop/cop/sorbet/forbid_t_struct.rb', line 151

def nilable?
  type.start_with?("T.nilable(")
end

#typeObject



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