style: don't use prefix + directory

This commit is contained in:
Rylan Polster 2020-07-05 15:02:47 -04:00
parent b4a9565b8b
commit 792533462a
3 changed files with 35 additions and 9 deletions

View File

@ -843,15 +843,6 @@ module Homebrew
problem "Don't need to interpolate \"#{Regexp.last_match(2)}\" with #{Regexp.last_match(1)}"
end
# Prefer formula path shortcuts in Pathname+
if line =~ %r{\(\s*(prefix\s*\+\s*(['"])(bin|include|libexec|lib|sbin|share|Frameworks)[/'"])}
# TODO: check could be in RuboCop
problem(
"\"(#{Regexp.last_match(1)}...#{Regexp.last_match(2)})\" should" \
" be \"(#{Regexp.last_match(3).downcase}+...)\"",
)
end
return unless @strict
# TODO: check could be in RuboCop

View File

@ -93,6 +93,19 @@ module RuboCop
offending_node(n)
problem "Use `depends_on :java` to set JAVA_HOME"
end
find_strings(body_node).each do |n|
# Skip strings that don't start with one of the keywords
next unless regex_match_group(n, %r{^(bin|include|libexec|lib|sbin|share|Frameworks)/?})
parent = n.parent
# Only look at keywords that have `prefix` before them
prefix_keyword_regex = %r{(prefix\s*\+\s*["'](bin|include|libexec|lib|sbin|share|Frameworks))["'/]}
if match = parent.source.match(prefix_keyword_regex)
offending_node(parent)
problem "Use `#{match[2].downcase}` instead of `#{match[1]}\"`"
end
end
end
end
end

View File

@ -292,6 +292,28 @@ describe RuboCop::Cop::FormulaAudit::Text do
end
RUBY
end
it "When using `prefix + \"bin\"` instead of `bin`" do
expect_offense(<<~RUBY)
class Foo < Formula
def install
ohai prefix + "bin"
^^^^^^^^^^^^^^ Use `bin` instead of `prefix + "bin"`
end
end
RUBY
end
it "When using `prefix + \"bin/foo\"` instead of `bin`" do
expect_offense(<<~RUBY)
class Foo < Formula
def install
ohai prefix + "bin/foo"
^^^^^^^^^^^^^^^^^^ Use `bin` instead of `prefix + "bin"`
end
end
RUBY
end
end
end