Class: RuboCop::Cop::Packs::DocumentedPublicApis

Inherits:
Style::DocumentationMethod
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/rubocop/cop/packs/documented_public_apis.rb

Overview

This cop helps ensure that each pack has a documented public API The following examples assume this basic setup.

Examples:


# bad
# packs/foo/app/public/foo.rb
class Foo
  def bar; end
end

# good
# packs/foo/app/public/foo.rb
class Foo
  # This is a documentation comment.
  def bar; end
end

# good
# packs/foo/app/public/foo.rb
class Foo
  # This is a documentation comment.
  # It should appear above a sorbet type signature
  sig { void }
  def bar; end
end

Instance Method Summary collapse

Instance Method Details

#check(node) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rubocop/cop/packs/documented_public_apis.rb', line 51

def check(node)
  # This cop only applies for ruby files in `app/public`
  return if !processed_source.file_path.include?('app/public')
  return if non_public?(node) && !require_for_non_public_methods?

  left_sibling = node.left_sibling

  if left_sibling == :private_class_method
    if node_is_sorbet_signature?(node.parent.left_sibling)
      return if documentation_comment?(node.parent.left_sibling)
    elsif documentation_comment?(node.parent)
      return
    end
  elsif node_is_sorbet_signature?(left_sibling)
    return if documentation_comment?(node.left_sibling)
  elsif documentation_comment?(node)
    return
  end

  add_offense(node)
end

#node_is_sorbet_signature?(node) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
# File 'lib/rubocop/cop/packs/documented_public_apis.rb', line 74

def node_is_sorbet_signature?(node)
  # Is there a better way to check if a node is a sorbet signature? Probably!
  !!(node && (node.source.include?('sig do') || node.source.include?('sig {')))
end

#support_autocorrect?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/rubocop/cop/packs/documented_public_apis.rb', line 46

def support_autocorrect?
  false
end