Class: RuboCop::Cop::Homebrew::PublicApiCookbook Private

Inherits:
Base
  • Object
show all
Defined in:
rubocops/public_api_cookbook.rb

Overview

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

Ensures that methods and DSL calls documented in the Formula Cookbook or Cask Cookbook are annotated with @api public in their source definitions.

Both cookbook method lists live in ApiAnnotationHelper and are validated by this cop.

Constant Summary collapse

MSG =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

"Method `%<method>s` is referenced in the %<cookbook>s but is not annotated with `@api public`."
MISSING_FORMULA_LIST_MSG =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

"Formula Cookbook references methods missing from " \
"`FORMULA_COOKBOOK_METHODS`: %<methods>s."
MISSING_CASK_LIST_MSG =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

"Method `%<method>s` is annotated with `@api public` in `%<file>s` but is " \
"missing from `CASK_COOKBOOK_METHODS`."

Instance Method Summary collapse

Instance Method Details

#on_new_investigationvoid

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'rubocops/public_api_cookbook.rb', line 23

def on_new_investigation
  super

  file_path = processed_source.file_path
  return if file_path.nil?

  relative_path = file_path.sub(%r{.*/Library/Homebrew/}, "")

  if relative_path == "rubocops/shared/api_annotation_helper.rb"
    missing_formula = (HOMEBREW_LIBRARY_PATH.parent.parent/"docs/Formula-Cookbook.md").read
                      .scan(
                        %r{/rubydoc/\w+(?:/\w+)*\.html#(\w+[!?]?)-(?:class|instance)_method},
                      )
                      .flatten -
                      ApiAnnotationHelper::FORMULA_COOKBOOK_METHODS.keys
    missing_formula.sort!

    if missing_formula.any?
      add_offense(
        processed_source.ast&.each_descendant(:casgn)&.find do |node|
          node.const_name == "FORMULA_COOKBOOK_METHODS"
        end || processed_source.ast || processed_source.buffer.source_range,
        message: format(
          MISSING_FORMULA_LIST_MSG,
          methods: missing_formula.map { |method| "`#{method}`" }.join(", "),
        ),
      )
    end

    return
  end

  api_public_targets = build_api_public_targets

  check_cookbook_methods(ApiAnnotationHelper::FORMULA_COOKBOOK_METHODS,
                         "Formula Cookbook", relative_path, api_public_targets)
  check_cookbook_methods(ApiAnnotationHelper::CASK_COOKBOOK_METHODS,
                         "Cask Cookbook", relative_path, api_public_targets)

  return unless %w[cask/dsl.rb cask/cask.rb cask/dsl/version.rb].include?(relative_path)

  cookbook_methods = ApiAnnotationHelper::CASK_COOKBOOK_METHODS.keys.to_set
  lines = processed_source.lines

  processed_source.comments.each do |comment|
    next unless ["# @api public", "@api public"].include?(comment.text.strip)

    (1..5).each do |offset|
      target_line = lines[comment.loc.line - 1 + offset]&.strip
      break if target_line.blank?

      match = target_line.match(/\A(?:def\s+(?:self\.)?|attr_reader\s+:|attr_accessor\s+:)(\w+[!?]?)/) ||
              target_line.match(/\Adelegate\s+(\w+[!?]?):/)
      next if match.nil?

      method_name = match[1].to_s
      break if cookbook_methods.include?(method_name)

      add_offense(comment, message: format(MISSING_CASK_LIST_MSG, method: method_name, file: relative_path))
      break
    end
  end
end