Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

on_macos/on_linux block: improve rubocop message #9486

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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