Class: RuboCop::Cop::Homebrew::OSDependsOn Private

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp
Defined in:
rubocops/os_depends_on.rb

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.

Constant Summary collapse

MACOS_ONLY_CASK_STANZAS =

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.

[
  :app,
  :audio_unit_plugin,
  :colorpicker,
  :dictionary,
  :input_method,
  :internet_plugin,
  :keyboard_layout,
  :mdimporter,
  :pkg,
  :prefpane,
  :qlplugin,
  :screen_saver,
  :service,
  :suite,
  :vst_plugin,
  :vst3_plugin,
].freeze
LINUX_ONLY_CASK_STANZAS =

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.

[:app_image].freeze
PLATFORM_BLOCKS =

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.

[:on_arm, :on_intel, :on_system].freeze
CASK_STANZA_ORDER =

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.

T.let(RuboCop::Cask::Constants::STANZA_ORDER, T::Array[Symbol])
MACOS_DEPENDENCY_STANZAS =

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.

[:macos, :maximum_macos].freeze
LINUX_DEPENDENCY_STANZAS =

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.

[:linux].freeze
RESTRICT_ON_SEND =

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.

[:depends_on].freeze

Instance Method Summary collapse

Instance Method Details

#on_block(node) ⇒ void

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.

Parameters:



42
43
44
45
46
47
48
49
# File 'rubocops/os_depends_on.rb', line 42

def on_block(node)
  send_node = node.children.first
  return unless send_node.is_a?(RuboCop::AST::SendNode)
  return if send_node.method_name != :cask

  add_missing_os_dependency(node, :macos)
  add_missing_os_dependency(node, :linux)
end

#on_send(node) ⇒ void

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.

Parameters:

  • node (RuboCop::AST::SendNode)


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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'rubocops/os_depends_on.rb', line 52

def on_send(node)
  autocorrect_macos_comparison_strings(node)
  check_redundant_bare_macos(node)
  check_conflicting_os_requirements(node)

  macos_pairs = depends_on_pairs(node).select { |pair| symbol_key(pair) == :macos }
  return if node.receiver || macos_pairs.empty?

  oldest_macos = MacOSVersion::SYMBOLS.values.map { |release| MacOSVersion.new(release) }.min
  return unless oldest_macos

  macos_pairs.each do |pair|
    value = pair.value
    if value.array_type? && (value.values.one? || node.each_ancestor(:class).any?)
      value = value.values.first
    end
    next unless value&.sym_type?

    version = MacOSVersion::RELEASES[value.value]
    next unless version
    next if MacOSVersion.new(version) > oldest_macos

    hash = pair.parent
    next unless hash&.hash_type?

    os_block = node.each_ancestor(:block).any? do |ancestor|
      method = ancestor.method_name
      method == :on_system ||
        (RuboCop::Cask::Constants::ON_SYSTEM_METHODS.include?(method) &&
         ![:on_arm, :on_intel].include?(method))
    end
    tagged_formula = pair.value.array_type? && !pair.value.values.one?
    message = if os_block
      "Remove the redundant minimum macOS dependency from this OS block."
    elsif !hash.pairs.one?
      "Remove the redundant `macos:` pair and add a separate `depends_on :macos`."
    else
      "Use `depends_on :macos` instead of a redundant minimum macOS version."
    end
    add_offense(pair, message:) do |corrector|
      next if os_block || tagged_formula
      next if !hash.pairs.one? || !node.arguments.one?
      next if sibling_depends_on_calls(node).any? { |sibling| bare_os_depends_on?(sibling, :macos) }
      next if processed_source.comments.any? do |comment|
        comment.source_range.line.between?(node.first_line, node.last_line)
      end

      corrector.replace(hash.source_range, ":macos")
    end
  end
end