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

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
Alignment, CommentsHelp, RangeHelp
Defined in:
lib/rubocop/cop/sorbet/forbid_t_struct.rb

Overview

Disallow using T::Struct and T::Props.

Examples:


# bad
class MyStruct < T::Struct
  const :foo, String
  prop :bar, Integer, default: 0

  def some_method; end
end

# good
class MyStruct
  extend T::Sig

  sig { returns(String) }
  attr_reader :foo

  sig { returns(Integer) }
  attr_accessor :bar

  sig { params(foo: String, bar: Integer) }
  def initialize(foo:, bar: 0)
    @foo = foo
    @bar = bar
  end

  def some_method; end
end

AutocorrectStyle: rbs


# bad
class MyStruct < T::Struct
  const :foo, String
  prop :bar, T.nilable(Integer), default: 0
end

# good
class MyStruct
  #: String
  attr_reader :foo

  #: Integer?
  attr_accessor :bar

  #: (foo: String, ?bar: Integer?) -> void
  def initialize(foo:, bar: 0)
    @foo = foo
    @bar = bar
  end
end

Defined Under Namespace

Classes: Property, TStructWalker

Constant Summary collapse

RESTRICT_ON_SEND =
[:include, :prepend, :extend].freeze
MSG_STRUCT =
"Using `T::Struct` or its variants is deprecated in this codebase."
MSG_PROPS =
"Using `T::Props` or its variants is deprecated in this codebase."
VALID_AUTOCORRECT_STYLES =
["sig", "rbs"].freeze

Instance Method Summary collapse

Instance Method Details

#on_class(node) ⇒ Object



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/rubocop/cop/sorbet/forbid_t_struct.rb', line 314

def on_class(node)
  return unless t_struct?(node.parent_class)

  add_offense(node, message: MSG_STRUCT) do |corrector|
    walker = TStructWalker.new(autocorrect_style)
    walker.walk(node.body)

    range = range_between(node.identifier.source_range.end_pos, node.parent_class.source_range.end_pos)
    corrector.remove(range)
    next if node.single_line?

    if rbs? && walker.extend_t_sig_node
      corrector.remove(range_by_whole_lines(walker.extend_t_sig_node.source_range, include_final_newline: true))
    end

    unless walker.has_extend_t_sig || rbs?
      indent = offset(node)
      corrector.insert_after(node.identifier, "\n#{indent}  extend T::Sig\n")
    end

    first_prop = walker.props.first
    walker.props.each do |prop|
      node = prop.node
      indent = offset(node)
      line_range = range_by_whole_lines(prop.node.source_range)
      new_line = prop != first_prop && !previous_line_blank?(node)
      trailing_comments = processed_source.each_comment_in_lines(line_range.line..line_range.line)

      corrector.replace(
        line_range,
        "#{new_line ? "\n" : ""}" \
          "#{trailing_comments.map { |comment| "#{indent}#{comment.text}\n" }.join}" \
          "#{indent}#{prop.attr_sig}\n#{indent}#{prop.attr_accessor}",
      )
    end

    last_prop = walker.props.last
    if last_prop
      indent = offset(last_prop.node)
      line_range = range_by_whole_lines(last_prop.node.source_range, include_final_newline: true)
      corrector.insert_after(line_range, initialize_method(indent, walker.props))
    end
  end
end

#on_send(node) ⇒ Object



359
360
361
362
363
# File 'lib/rubocop/cop/sorbet/forbid_t_struct.rb', line 359

def on_send(node)
  return unless t_props?(node)

  add_offense(node, message: MSG_PROPS)
end

#t_props?(node) ⇒ Object



312
# File 'lib/rubocop/cop/sorbet/forbid_t_struct.rb', line 312

def_node_matcher(:t_props?, "(send nil? {:include :prepend :extend} `(const (const {nil? cbase} :T) :Props))")

#t_struct?(node) ⇒ Object



307
308
309
# File 'lib/rubocop/cop/sorbet/forbid_t_struct.rb', line 307

def_node_matcher(:t_struct?, <<~PATTERN)
  (const (const {nil? cbase} :T) {:Struct :ImmutableStruct :InexactStruct})
PATTERN