diff --git a/Library/Homebrew/rubocops/components_order.rb b/Library/Homebrew/rubocops/components_order.rb index 75eb87b6f8..27e5a55e86 100644 --- a/Library/Homebrew/rubocops/components_order.rb +++ b/Library/Homebrew/rubocops/components_order.rb @@ -65,11 +65,8 @@ module RuboCop (on_macos_blocks + on_linux_blocks).each do |on_os_block| on_os_body = on_os_block.body - if on_os_body.if_type? - on_os_bodies += on_os_body.branches.map { |branch| [on_os_block.method_name, branch] } - else - on_os_bodies << [on_os_block.method_name, on_os_body] - end + branches = on_os_body.if_type? ? on_os_body.branches : [on_os_body] + on_os_bodies += branches.map { |branch| [on_os_block, branch] } end message = nil @@ -79,16 +76,19 @@ module RuboCop [:url, :version, :sha256], [:url, :mirror, :version, :sha256], ] + minimum_methods = allowed_methods.first.map { |m| "`#{m}`" }.to_sentence + maximum_methods = allowed_methods.last.map { |m| "`#{m}`" }.to_sentence - # TODO: Refactor this to point to the actual offending methods rather than the entire block. - on_os_bodies.each do |method_name, on_os_body| + on_os_bodies.each do |on_os_block, on_os_body| + method_name = on_os_block.method_name child_nodes = on_os_body.begin_type? ? on_os_body.child_nodes : [on_os_body] if child_nodes.all? { |n| n.send_type? || n.block_type? } method_names = child_nodes.map(&:method_name) next if allowed_methods.include? method_names end + offending_node(on_os_block) message = "`#{method_name}` blocks within `resource` blocks must contain at least "\ - "`url` and `mirror` and at most `url`, `mirror`, `version`, `sha256` (in order)." + "#{minimum_methods} and at most #{maximum_methods} (in order)." break end diff --git a/Library/Homebrew/test/rubocops/components_order_spec.rb b/Library/Homebrew/test/rubocops/components_order_spec.rb index b95bda576f..a3ffb2c781 100644 --- a/Library/Homebrew/test/rubocops/components_order_spec.rb +++ b/Library/Homebrew/test/rubocops/components_order_spec.rb @@ -614,8 +614,8 @@ describe RuboCop::Cop::FormulaAudit::ComponentsOrder do url "https://brew.sh/foo-1.0.tgz" resource do - ^^^^^^^^^^^ `on_macos` blocks within `resource` blocks must contain at least `url` and `mirror` and at most `url`, `mirror`, `version`, `sha256` (in order). on_macos do + ^^^^^^^^^^^ `on_macos` blocks within `resource` blocks must contain at least `url` and `sha256` and at most `url`, `mirror`, `version` and `sha256` (in order). sha256 "586372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35" url "https://brew.sh/resource2.tar.gz" end @@ -681,8 +681,8 @@ describe RuboCop::Cop::FormulaAudit::ComponentsOrder do url "https://brew.sh/foo-1.0.tgz" resource do - ^^^^^^^^^^^ `on_macos` blocks within `resource` blocks must contain at least `url` and `mirror` and at most `url`, `mirror`, `version`, `sha256` (in order). on_macos do + ^^^^^^^^^^^ `on_macos` blocks within `resource` blocks must contain at least `url` and `sha256` and at most `url`, `mirror`, `version` and `sha256` (in order). if foo == :bar url "https://brew.sh/resource2.tar.gz" sha256 "586372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35" @@ -707,13 +707,13 @@ describe RuboCop::Cop::FormulaAudit::ComponentsOrder do url "https://brew.sh/foo-1.0.tgz" resource do - ^^^^^^^^^^^ `on_linux` blocks within `resource` blocks must contain at least `url` and `mirror` and at most `url`, `mirror`, `version`, `sha256` (in order). on_macos do url "https://brew.sh/resource2.tar.gz" sha256 "586372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35" end on_linux do + ^^^^^^^^^^^ `on_linux` blocks within `resource` blocks must contain at least `url` and `sha256` and at most `url`, `mirror`, `version` and `sha256` (in order). sha256 "586372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35" url "https://brew.sh/resource2.tar.gz" end @@ -753,13 +753,13 @@ describe RuboCop::Cop::FormulaAudit::ComponentsOrder do url "https://brew.sh/foo-1.0.tgz" resource do - ^^^^^^^^^^^ `on_linux` blocks within `resource` blocks must contain at least `url` and `mirror` and at most `url`, `mirror`, `version`, `sha256` (in order). on_macos do url "https://brew.sh/resource2.tar.gz" sha256 "586372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35" end on_linux do + ^^^^^^^^^^^ `on_linux` blocks within `resource` blocks must contain at least `url` and `sha256` and at most `url`, `mirror`, `version` and `sha256` (in order). if foo == :bar url "https://brew.sh/resource2.tar.gz" sha256 "586372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35"