Skip to content

Commit

Permalink
on_macos/on_linux block: improve rubocop message
Browse files Browse the repository at this point in the history
Co-authored-by: Markus Reiter <me@reitermark.us>
  • Loading branch information
SeekingMeaning and reitermarkus committed Dec 10, 2020
1 parent fc921bb commit c4da302
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Library/Homebrew/rubocops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

require_relative "load_path"

require "active_support/core_ext/array/conversions"

require "utils/sorbet"

require "rubocop-performance"
Expand Down
17 changes: 8 additions & 9 deletions Library/Homebrew/rubocops/components_order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def audit_formula(_node, _class_node, _parent_class_node, body_node)
end

def check_on_os_block_content(component_precedence_list, on_os_block)
on_os_allowed_methods = %w[depends_on patch resource deprecate! disable!]
_, offensive_node = check_order(component_precedence_list, on_os_block.body)
component_problem(*offensive_node) if offensive_node
child_nodes = on_os_block.body.begin_type? ? on_os_block.body.child_nodes : [on_os_block.body]
Expand All @@ -145,16 +146,14 @@ def check_on_os_block_content(component_precedence_list, on_os_block)
method_type = child.send_type? || child.block_type?
next unless method_type

valid_node ||= child.method_name.to_s == "patch"
valid_node ||= child.method_name.to_s == "resource"
valid_node ||= child.method_name.to_s == "deprecate!"
valid_node ||= child.method_name.to_s == "disable!"
valid_node ||= on_os_allowed_methods.include? child.method_name.to_s

@offensive_node = on_os_block
@offense_source_range = on_os_block.source_range
unless valid_node
problem "`#{on_os_block.method_name}` can only include `depends_on`, `patch` and `resource` nodes."
end
@offensive_node = child
@offense_source_range = child.source_range
next if valid_node

problem "`#{on_os_block.method_name}` cannot include `#{child.method_name}`. " \
"Only #{on_os_allowed_methods.map { |m| "`#{m}`" }.to_sentence} are allowed."
end
end

Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/test/rubocops/components_order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,9 @@ class Foo < Formula
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"
on_macos do
^^^^^^^^^^^ `on_macos` can only include `depends_on`, `patch` and `resource` nodes.
depends_on "readline"
uses_from_macos "ncurses"
^^^^^^^^^^^^^^^^^^^^^^^^^ `on_macos` cannot include `uses_from_macos`. [...]
end
end
RUBY
Expand All @@ -403,9 +403,9 @@ class Foo < Formula
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"
on_linux do
^^^^^^^^^^^ `on_linux` can only include `depends_on`, `patch` and `resource` nodes.
depends_on "readline"
uses_from_macos "ncurses"
^^^^^^^^^^^^^^^^^^^^^^^^^ `on_linux` cannot include `uses_from_macos`. [...]
end
end
RUBY
Expand Down

0 comments on commit c4da302

Please sign in to comment.