From 9ad342eba04cfa994746b43e87b7151b59d5a362 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Sun, 5 Jul 2020 13:50:48 -0400 Subject: [PATCH] style: don't concatenate in string interpolation --- Library/Homebrew/dev-cmd/audit.rb | 6 ------ Library/Homebrew/rubocops/text.rb | 9 +++++++++ Library/Homebrew/test/rubocops/text_spec.rb | 11 +++++++++++ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/Library/Homebrew/dev-cmd/audit.rb b/Library/Homebrew/dev-cmd/audit.rb index 5f3bfe1876..b6507d56f6 100644 --- a/Library/Homebrew/dev-cmd/audit.rb +++ b/Library/Homebrew/dev-cmd/audit.rb @@ -843,12 +843,6 @@ module Homebrew problem "Don't need to interpolate \"#{Regexp.last_match(2)}\" with #{Regexp.last_match(1)}" end - # Check for string concatenation; prefer interpolation - if line =~ /(#\{\w+\s*\+\s*['"][^}]+\})/ - # TODO: check could be in RuboCop - problem "Try not to concatenate paths in string interpolation:\n #{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 diff --git a/Library/Homebrew/rubocops/text.rb b/Library/Homebrew/rubocops/text.rb index 77d25c77a7..8aa2b9ab2a 100644 --- a/Library/Homebrew/rubocops/text.rb +++ b/Library/Homebrew/rubocops/text.rb @@ -69,6 +69,15 @@ module RuboCop offending_node(m) problem "Use separate `make` calls" 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*['"][^}]+\}/) + + offending_node(interpolation_node) + problem "Do not concatenate paths in string interpolation" + end + end end end end diff --git a/Library/Homebrew/test/rubocops/text_spec.rb b/Library/Homebrew/test/rubocops/text_spec.rb index 8a6668ae46..f915daafcd 100644 --- a/Library/Homebrew/test/rubocops/text_spec.rb +++ b/Library/Homebrew/test/rubocops/text_spec.rb @@ -226,6 +226,17 @@ describe RuboCop::Cop::FormulaAudit::Text do end RUBY end + + it "When concatenating in string interpolation" do + expect_offense(<<~RUBY) + class Foo < Formula + def install + ohai "foo \#{bar + "baz"}" + ^^^^^^^^^^^^^^ Do not concatenate paths in string interpolation + end + end + RUBY + end end end