Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions Library/Homebrew/rubocops/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,30 @@ def audit_formula(formula_nodes)
problem "Use separate `make` calls"
end

find_every_method_call_by_name(body_node, :+).each do |plus_node|
next unless plus_node.receiver&.send_type?
next unless plus_node.first_argument&.str_type?

receiver_method = plus_node.receiver.method_name
path_arg = plus_node.first_argument.str_content

case receiver_method
when :prefix
next unless (match = path_arg.match(%r{^(bin|include|libexec|lib|sbin|share|Frameworks)(?:/| |$)}))

offending_node(plus_node)
problem "Use `#{match[1].downcase}` instead of `prefix + \"#{match[1]}\"`"
when :bin, :include, :libexec, :lib, :sbin, :share
next if path_arg.empty?

offending_node(plus_node)
good = "#{receiver_method}/\"#{path_arg}\""
problem "Use `#{good}` instead of `#{plus_node.source}`" do |corrector|
corrector.replace(plus_node.loc.expression, good)
end
end
end

body_node.each_descendant(:dstr) do |dstr_node|
dstr_node.each_descendant(:begin) do |interpolation_node|
next unless interpolation_node.source.match?(/#\{\w+\s*\+\s*['"][^}]+\}/)
Expand All @@ -98,19 +122,7 @@ def audit_formula(formula_nodes)
problem "Do not concatenate paths in string interpolation"
end
end

prefix_path(body_node) do |prefix_node, path|
next unless (match = path.match(%r{^(bin|include|libexec|lib|sbin|share|Frameworks)(?:/| |$)}))

offending_node(prefix_node)
problem "Use `#{match[1].downcase}` instead of `prefix + \"#{match[1]}\"`"
end
end

# Find: prefix + "foo"
def_node_search :prefix_path, <<~EOS
$(send (send nil? :prefix) :+ (str $_))
EOS
end
end

Expand Down
13 changes: 13 additions & 0 deletions Library/Homebrew/test/rubocops/text_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,19 @@ def install
RUBY
end

it 'reports offenses if eg `lib+"thing"` is present' do
expect_offense(<<~RUBY)
class Foo < Formula
def install
(lib+"foo").install
^^^^^^^^^ FormulaAudit/Text: Use `lib/"foo"` instead of `lib+"foo"`
(bin+"foobar").install
^^^^^^^^^^^^ FormulaAudit/Text: Use `bin/"foobar"` instead of `bin+"foobar"`
end
end
RUBY
end

it 'reports an offense if `prefix + "bin"` is present' do
expect_offense(<<~RUBY)
class Foo < Formula
Expand Down