mirror of
https://github.com/Homebrew/brew.git
synced 2025-07-14 16:09:03 +08:00
Add cop to police os checks
The methods `OS.linux?` and `OS.mac?` should only be used in `extend/os` and this cop makes sure of that.
This commit is contained in:
parent
759ab2d0bd
commit
c7d53a2d9c
@ -55,6 +55,11 @@ FormulaAudit:
|
||||
FormulaAuditStrict:
|
||||
Enabled: true
|
||||
|
||||
Homebrew/MoveToExtendOS:
|
||||
Exclude:
|
||||
- "Homebrew/{extend,test}/**/*"
|
||||
- "Taps/**/*"
|
||||
|
||||
# enable all Homebrew custom cops
|
||||
Homebrew:
|
||||
Enabled: true
|
||||
|
@ -14,6 +14,7 @@ end
|
||||
|
||||
require_relative "io_read"
|
||||
require_relative "shell_commands"
|
||||
require_relative "platform"
|
||||
|
||||
require_relative "formula_desc"
|
||||
require_relative "components_order"
|
||||
|
25
Library/Homebrew/rubocops/platform.rb
Normal file
25
Library/Homebrew/rubocops/platform.rb
Normal file
@ -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
|
22
Library/Homebrew/test/rubocops/platform_spec.rb
Normal file
22
Library/Homebrew/test/rubocops/platform_spec.rb
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user