Module: RuboCop::Cop::Style::RbsInline::StructClassMatcher

Included in:
MissingStructClassAnnotation, StructClassCommentAlignment, StructNewWithBlock
Defined in:
lib/rubocop/cop/style/rbs_inline/mixin/struct_class_matcher.rb,
sig/rubocop/cop/style/rbs_inline/mixin/struct_class_matcher.rbs

Overview

Matcher for Struct.new calls, shared by the cops that check Struct.new class definitions.

Also overrides DataStructHelper#attr_argument? for Struct.new semantics, so cops that include both must include this module last.

Instance Method Summary collapse

Instance Method Details

#attr_argument?(arg) ⇒ Boolean

Struct.new treats a leading string argument as the struct name, so only symbol arguments are attributes.

Parameters:

  • arg (RuboCop::AST::Node)

Returns:

  • (Boolean)


25
26
27
# File 'lib/rubocop/cop/style/rbs_inline/mixin/struct_class_matcher.rb', line 25

def attr_argument?(arg) #: bool
  arg.sym_type?
end

#struct_like_class?(node) ⇒ Boolean

Parameters:

  • node (RuboCop::AST::SendNode)

Returns:

  • (Boolean)


16
17
18
19
20
# File 'lib/rubocop/cop/style/rbs_inline/mixin/struct_class_matcher.rb', line 16

def struct_like_class?(node) #: bool
  return false unless node.method_name == :new

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