Class: RuboCop::Cop::Style::RbsInline::DataDefineWithBlock

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/style/rbs_inline/data_define_with_block.rb,
sig/rubocop/cop/style/rbs_inline/data_define_with_block.rbs

Overview

Checks for Data.define calls with a block.

RBS::Inline does not parse the contents of Data.define blocks, so any methods defined inside will not be recognized for type checking. Instead, call Data.define without a block and define additional methods by reopening the class separately.

NOTE: This is a known limitation of RBS::Inline. See https://github.com/soutaro/rbs-inline/pull/183 for the upstream fix.

Examples:

# bad
User = Data.define(:name, :role) do
  def admin? = role == :admin #: bool
end

# good
User = Data.define(:name, :role)

class User
  def admin? = role == :admin #: bool
end

Constant Summary collapse

MSG =

Returns:

  • (::String)
'Do not use `Data.define` with a block. RBS::Inline does not parse block contents, ' \
'so methods defined in the block will not be recognized. ' \
'Use a separate class definition instead.'

Instance Method Summary collapse

Instance Method Details

#data_define?(node) ⇒ Boolean

Parameters:

  • node (RuboCop::AST::SendNode)

Returns:

  • (Boolean)


48
49
50
51
52
53
# File 'lib/rubocop/cop/style/rbs_inline/data_define_with_block.rb', line 48

def data_define?(node) #: bool
  return false unless node.method_name == :define

  receiver = node.receiver
  receiver.is_a?(RuboCop::AST::ConstNode) && receiver.short_name == :Data
end

#on_send(node) ⇒ void

This method returns an undefined value.

Parameters:

  • node (RuboCop::AST::SendNode)


36
37
38
39
40
41
42
43
# File 'lib/rubocop/cop/style/rbs_inline/data_define_with_block.rb', line 36

def on_send(node) #: void
  return unless data_define?(node)

  block_node = node.parent
  return unless block_node&.block_type?

  add_offense(node)
end