From 792533462a39362771f606b56cc968e31f9259ab Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Sun, 5 Jul 2020 15:02:47 -0400 Subject: [PATCH] style: don't use prefix + directory --- Library/Homebrew/dev-cmd/audit.rb | 9 --------- Library/Homebrew/rubocops/text.rb | 13 ++++++++++++ Library/Homebrew/test/rubocops/text_spec.rb | 22 +++++++++++++++++++++ 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/Library/Homebrew/dev-cmd/audit.rb b/Library/Homebrew/dev-cmd/audit.rb index 4e3b29dfbd..32927b085f 100644 --- a/Library/Homebrew/dev-cmd/audit.rb +++ b/Library/Homebrew/dev-cmd/audit.rb @@ -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 diff --git a/Library/Homebrew/rubocops/text.rb b/Library/Homebrew/rubocops/text.rb index 3559d12ce1..aa9ea3a726 100644 --- a/Library/Homebrew/rubocops/text.rb +++ b/Library/Homebrew/rubocops/text.rb @@ -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 diff --git a/Library/Homebrew/test/rubocops/text_spec.rb b/Library/Homebrew/test/rubocops/text_spec.rb index 7ab156c74d..82545b3fc1 100644 --- a/Library/Homebrew/test/rubocops/text_spec.rb +++ b/Library/Homebrew/test/rubocops/text_spec.rb @@ -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