Class: RuboCop::Cop::Cask::OnSystemConditionals Private

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector, Forwardable
Includes:
CaskHelp, OnSystemConditionalsHelper
Defined in:
rubocops/cask/on_system_conditionals.rb,
rubocops/cask/on_system_conditionals.rbi,
sorbet/rbi/dsl/rubo_cop/cop/cask/on_system_conditionals.rbi

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.

This cop makes sure that OS conditionals are consistent.

Example

# bad
cask 'foo' do
  if MacOS.version == :tahoe
    sha256 "..."
  end
end

# good
cask 'foo' do
  on_tahoe do
    sha256 "..."
  end
end

Constant Summary collapse

FLIGHT_STANZA_NAMES =

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.

[:preflight, :postflight, :uninstall_preflight, :uninstall_postflight].freeze
ON_SYSTEM_BLOCK_METHODS =

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::ON_SYSTEM_METHODS, :on_system].freeze,
  T::Array[Symbol],
)
SHA256_KEYS =

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(
  {
    macos: { arm64: :arm, intel: :intel, x86_64: :intel },
    linux: { arm64: :arm64_linux, intel: :x86_64_linux, x86_64: :x86_64_linux },
  }.freeze,
  T::Hash[Symbol, T::Hash[Symbol, Symbol]],
)
OS_SHA256_MESSAGE =

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.

"Don't nest `sha256` stanzas in `on_macos` and `on_linux` blocks"

Constants included from OnSystemConditionalsHelper

OnSystemConditionalsHelper::ARCH_OPTIONS, OnSystemConditionalsHelper::BASE_OS_OPTIONS, OnSystemConditionalsHelper::MACOS_MODULE_NAMES, OnSystemConditionalsHelper::MACOS_VERSION_CONDITIONALS, OnSystemConditionalsHelper::MACOS_VERSION_OPTIONS, OnSystemConditionalsHelper::ON_SYSTEM_OPTIONS

Instance Method Summary collapse

Methods included from CaskHelp

#cask_tap, #inner_stanzas, #on_block, #on_cask_stanza_block, #on_system_methods

Methods included from OnSystemConditionalsHelper

#audit_arch_conditionals, #audit_base_os_conditionals, #audit_macos_references, #audit_macos_version_conditionals, #audit_on_system_blocks, #hardware_cpu_search, #if_arch_node_search, #if_base_os_node_search, #if_macos_version_node_search, #macos_version_comparison_search, #on_macos_version_method_call, #on_system_method_call

Methods included from HelperFunctions

#block_method_called_in_block?, #check_precedence, #class_name, #component_precedes?, descendant_send_nodes, descendant_send_nodes_by_method_name, #end_column, #expression_negated?, #find_all_blocks, #find_block, #find_blocks, #find_const, #find_every_func_call_by_name, #find_every_method_call_by_name, #find_instance_call, #find_instance_method_call, #find_method_calls_by_name, #find_method_def, #find_method_with_args, #find_node_method_by_name, #find_strings, #format_component, #line_number, #line_start_column, #method_called?, #method_called_ever?, #method_name, #node_equals?, #offending_node, #parameters, #parameters_passed?, #problem, #regex_match_group, #source_buffer, #start_column, #string_content

Instance Method Details

#cask_body(*args, &block) ⇒ T.untyped

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.

Parameters:

  • args (T.untyped)
  • block (T.untyped)

Returns:

  • (T.untyped)


10
# File 'sorbet/rbi/dsl/rubo_cop/cop/cask/on_system_conditionals.rbi', line 10

def cask_body(*args, &block); end

#on_cask(cask_block) ⇒ 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:



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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'rubocops/cask/on_system_conditionals.rb', line 50

def on_cask(cask_block)
  @cask_block = T.let(cask_block, T.nilable(RuboCop::Cask::AST::CaskBlock))

  toplevel_stanzas.each do |stanza|
    next unless FLIGHT_STANZA_NAMES.include? stanza.stanza_name

    audit_on_system_blocks(stanza.stanza_node, stanza.stanza_name)
  end

  cask_body.each_node(:block) do |block_node|
    next unless cask_on_system_conditional_block?(block_node)

    method_node = block_node.method_node
    next unless block_node.each_ancestor(:block).any? do |ancestor|
      cask_on_system_conditional_block?(ancestor) && ancestor.method_node == method_node
    end

    add_offense(method_node, message: "Remove the redundant nested `#{method_node.source}` block.")
  end

  audit_arch_conditionals(cask_body, allowed_blocks: FLIGHT_STANZA_NAMES)
  audit_macos_version_conditionals(cask_body, recommend_on_system: false, allowed_blocks: FLIGHT_STANZA_NAMES)

  sha256_nodes = Hash.new { |hash, key| hash[key] = {} }
  sha256_on_arch_stanzas(cask_body) do |node, method, value|
    arch = method.to_s.delete_prefix("on_").to_sym
    ast_node = T.cast(node, RuboCop::AST::Node)
    sha256_nodes[ast_node.parent][arch] = { node: ast_node, value: }
  end

  sha256_nodes.each_value do |nodes|
    next if !nodes.key?(:arm) || !nodes.key?(:intel)

    offending_node(nodes[:arm][:node])
    arm_value = nodes[:arm][:value].inspect
    intel_value = nodes[:intel][:value].inspect
    replacement_string = "sha256 arm: #{arm_value}, intel: #{intel_value}"
    if comments_in_node_ranges?(nodes[:arm][:node], nodes[:intel][:node])
      problem "Don't nest only the `sha256` stanzas in `on_intel` and `on_arm` blocks"
      next
    end

    problem "Don't nest only the `sha256` stanzas in `on_intel` and `on_arm` blocks" do |corrector|
      corrector.replace(nodes[:arm][:node].source_range, replacement_string)
      corrector.remove(range_by_whole_lines(nodes[:intel][:node].source_range, include_final_newline: true))
    end
  end

  simplify_os_sha256_stanzas

  version_nodes = Hash.new { |hash, key| hash[key] = {} }
  version_and_sha256_on_arch_stanzas(cask_body) do |block_node, arch_method, version_value, sha256_value|
    arch = arch_method.to_s.delete_prefix("on_").to_sym
    ast_block_node = T.cast(block_node, RuboCop::AST::Node)
    version_nodes[ast_block_node.parent][arch] = {
      node:          ast_block_node,
      version_value:,
      sha256_value:,
    }
  end

  version_nodes.each_value do |nodes|
    next if !nodes.key?(:arm) || !nodes.key?(:intel)

    arm_version = nodes[:arm][:version_value]
    intel_version = nodes[:intel][:version_value]

    next if arm_version != intel_version

    arm_sha = nodes[:arm][:sha256_value]
    intel_sha = nodes[:intel][:sha256_value]
    arm_node = nodes[:arm][:node]
    intel_node = nodes[:intel][:node]

    indent = " " * arm_node.loc.column
    version_str = "version #{arm_version.inspect}"
    sha256_str = if arm_sha == intel_sha
      "sha256 #{arm_sha.inspect}"
    else
      "sha256 arm: #{arm_sha.inspect}, intel: #{intel_sha.inspect}"
    end
    replacement = "#{version_str}\n#{indent}#{sha256_str}"

    offending_node(arm_node)
    if comments_in_node_ranges?(arm_node, intel_node)
      problem "Don't nest identical `version` stanzas in `on_intel` and `on_arm` blocks"
      next
    end

    problem "Don't nest identical `version` stanzas in `on_intel` and `on_arm` blocks" do |corrector|
      corrector.replace(arm_node.source_range, replacement)
      corrector.remove(range_by_whole_lines(intel_node.source_range, include_final_newline: true))
    end
  end

  toplevel_stanzas.select { |stanza| stanza.stanza_name == :sha256 }.each do |stanza|
    sha256_node = stanza.stanza_node
    next if sha256_node.arguments.count != 1
    next unless sha256_node.arguments.first.hash_type?

    hash_node = sha256_node.arguments.first
    values = hash_node.pairs.filter_map do |pair|
      next unless pair.key.sym_type?
      next unless pair.value.str_type?

      [pair.key.value, pair.value.value]
    end.to_h

    arm_sha = values[:arm]
    intel_sha = values[:intel] || values[:x86_64]

    next unless arm_sha
    next unless intel_sha
    next if arm_sha != intel_sha

    if values.keys.intersect?([:arm64_linux, :x86_64_linux])
      next unless values.values.uniq.one?

      # A scalar also covers omitted Linux architectures, unless dependencies exclude them.
      linux_arches = cask_body.each_node(:send).filter_map do |node|
        next if node.method_name != :depends_on || !node.receiver.nil?
        next unless (argument = node.first_argument)&.hash_type?

        arch_pair = argument.pairs.find { |pair| pair.key.sym_type? && pair.key.value == :arch }
        next unless arch_pair

        scope = node.each_ancestor(:block).take_while { |block| !block.cask_block? }
        next if scope.any? { |block| block.method_name == :on_macos }
        next :unknown unless scope.all? { |block| block.method_name == :on_linux }

        arch_pair.value.sym_type? ? arch_pair.value.value : :unknown
      end
      if linux_arches.empty? || (linux_arches - [:arm64, :intel, :x86_64]).any?
        linux_arches = [:arm64, :intel]
      end
      next unless linux_arches.all? do |arch|
        arm64 = arch == :arm64
        values.key?(arm64 ? :arm64_linux : :x86_64_linux)
      end
    end

    offending_node(sha256_node)
    problem "sha256 values for different architectures should not be identical."
  end
end

#sha256_on_arch_stanzas(base_node, &block) ⇒ 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:



13
# File 'rubocops/cask/on_system_conditionals.rbi', line 13

def sha256_on_arch_stanzas(base_node, &block); end

#toplevel_stanzas(*args, &block) ⇒ T.untyped

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.

Parameters:

  • args (T.untyped)
  • block (T.untyped)

Returns:

  • (T.untyped)


13
# File 'sorbet/rbi/dsl/rubo_cop/cop/cask/on_system_conditionals.rbi', line 13

def toplevel_stanzas(*args, &block); end

#version_and_sha256_on_arch_stanzas(base_node, &block) ⇒ 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:



22
# File 'rubocops/cask/on_system_conditionals.rbi', line 22

def version_and_sha256_on_arch_stanzas(base_node, &block); end