diff --git a/Library/.rubocop.yml b/Library/.rubocop.yml index 091daf83ec..5d4dcfa749 100644 --- a/Library/.rubocop.yml +++ b/Library/.rubocop.yml @@ -55,6 +55,11 @@ FormulaAudit: FormulaAuditStrict: Enabled: true +Homebrew/MoveToExtendOS: + Exclude: + - "Homebrew/{extend,test}/**/*" + - "Taps/**/*" + # enable all Homebrew custom cops Homebrew: Enabled: true diff --git a/Library/Homebrew/rubocops/all.rb b/Library/Homebrew/rubocops/all.rb index 0f0ce7e0e1..86cb5ab8e0 100644 --- a/Library/Homebrew/rubocops/all.rb +++ b/Library/Homebrew/rubocops/all.rb @@ -14,6 +14,7 @@ end require_relative "io_read" require_relative "shell_commands" +require_relative "platform" require_relative "formula_desc" require_relative "components_order" diff --git a/Library/Homebrew/rubocops/platform.rb b/Library/Homebrew/rubocops/platform.rb new file mode 100644 index 0000000000..0cc65e9344 --- /dev/null +++ b/Library/Homebrew/rubocops/platform.rb @@ -0,0 +1,25 @@ +# typed: false +# frozen_string_literal: true + +module RuboCop + module Cop + module Homebrew + # This cop ensures that platform specific code ends up in `extend/os`. + # + # @api private + class MoveToExtendOS < Base + MSG = "Move calls to `OS.linux?` and `OS.mac?` to `extend/os`." + + def_node_matcher :os_check?, <<~PATTERN + (send (const nil? :OS) {:mac? | :linux?}) + PATTERN + + def on_send(node) + return unless os_check?(node) + + add_offense(node) + end + end + end + end +end diff --git a/Library/Homebrew/test/rubocops/platform_spec.rb b/Library/Homebrew/test/rubocops/platform_spec.rb new file mode 100644 index 0000000000..8a8819d6a3 --- /dev/null +++ b/Library/Homebrew/test/rubocops/platform_spec.rb @@ -0,0 +1,22 @@ +# typed: false +# frozen_string_literal: true + +require "rubocops/platform" + +describe RuboCop::Cop::Homebrew::MoveToExtendOS do + subject(:cop) { described_class.new } + + it "registers an offense when using `OS.linux?`" do + expect_offense(<<~RUBY) + OS.linux? + ^^^^^^^^^ Move calls to `OS.linux?` and `OS.mac?` to `extend/os`. + RUBY + end + + it "registers an offense when using `OS.mac?`" do + expect_offense(<<~RUBY) + OS.mac? + ^^^^^^^ Move calls to `OS.linux?` and `OS.mac?` to `extend/os`. + RUBY + end +end