Skip to content

Commit

Permalink
Merge pull request #148 from Shopify/refactor-forbid-untyped-struct-p…
Browse files Browse the repository at this point in the history
…rops
  • Loading branch information
sambostock authored Feb 21, 2023
2 parents c1a3b71 + 758bf9e commit f234d4e
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions lib/rubocop/cop/sorbet/forbid_untyped_struct_props.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ class ForbidUntypedStructProps < RuboCop::Cop::Base

# @!method t_struct(node)
def_node_matcher :t_struct, <<~PATTERN
(const (const nil? :T) :Struct)
PATTERN

# @!method t_immutable_struct(node)
def_node_matcher :t_immutable_struct, <<~PATTERN
(const (const nil? :T) :ImmutableStruct)
(const (const nil? :T) {:Struct :ImmutableStruct})
PATTERN

# @!method t_untyped(node)
Expand All @@ -47,19 +42,20 @@ class ForbidUntypedStructProps < RuboCop::Cop::Base

# @!method subclass_of_t_struct?(node)
def_node_matcher :subclass_of_t_struct?, <<~PATTERN
(class (const ...) {#t_struct #t_immutable_struct} ...)
(class (const ...) #t_struct ...)
PATTERN

# @!method untyped_props(node)
# Search for untyped prop/const declarations and capture their types
def_node_search :untyped_props, <<~PATTERN
(send nil? {:prop :const} _ {#t_untyped #t_nilable_untyped} ...)
(send nil? {:prop :const} _ ${#t_untyped #t_nilable_untyped} ...)
PATTERN

def on_class(node)
return unless subclass_of_t_struct?(node)

untyped_props(node).each do |untyped_prop|
add_offense(untyped_prop.child_nodes[1])
untyped_props(node).each do |prop_type|
add_offense(prop_type)
end
end
end
Expand Down

0 comments on commit f234d4e

Please sign in to comment.