String interpolations in regexps should also be allowed

- This came up in Cask `simply-fortran`:

```
Scanning /opt/homebrew/Library/Taps/homebrew/homebrew-cask/Casks/simply-fortran.rb
send_node: s(:send, nil, :arch), send_node.parent: s(:begin,
  s(:send, nil, :arch)), send_node.parent.parent: (regexp
  (str "href=.*?simplyfortran[._-]v?(\\d+(?:\\.\\d+)+)")
  (begin
    (send nil :arch))
  (str "\\.dmg")
  (regopt :i))
Casks/simply-fortran.rb:2:3: C: Cask/NoOverrides: Do not use a top-level arch stanza as the default. Add it to an on_{system} block instead.
Use :or_older or :or_newer to specify a range of macOS versions.

  arch arm: "-arm64", intel: "-x86_64"
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected
```
This commit is contained in:
Issy Long 2023-03-23 13:54:55 +00:00
parent c1de3dfb90
commit d4d413db62
No known key found for this signature in database
GPG Key ID: 8247C390DADC67D4
2 changed files with 26 additions and 1 deletions

View File

@ -43,7 +43,9 @@ module RuboCop
# Skip (nested) livecheck blocks as its `url` is different to a download `url`.
next if send_node.method_name == :livecheck || inside_livecheck_block?(send_node)
# Skip string interpolations.
next if send_node.ancestors.drop_while { |a| !a.begin_type? }.any?(&:dstr_type?)
if send_node.ancestors.drop_while { |a| !a.begin_type? }.any? { |a| a.dstr_type? || a.regexp_type? }
next
end
next if ON_SYSTEM_METHODS.include?(send_node.method_name)
names.add(send_node.method_name)

View File

@ -71,6 +71,29 @@ describe RuboCop::Cop::Cask::NoOverrides do
include_examples "does not report any offenses"
end
context "when there are `arch` interpolations in regexps in `on_*` blocks" do
let(:source) do
<<~CASK
cask 'foo' do
arch arm: "arm64", intel: "x86"
version 0.99,123.3
on_mojave :or_later do
url "https://brew.sh/foo-\#{arch}-\#{version.csv.first}-\#{version.csv.last}.pkg"
livecheck do
url "https://brew.sh/foo/releases.html"
regex(/href=.*?foo[._-]v?(\d+(?:.\d+)+)-\#{arch}.pkg/i)
end
end
end
CASK
end
include_examples "does not report any offenses"
end
context "when there are single-line livecheck blocks within `on_*` blocks, ignore their contents" do
let(:source) do
<<~CASK