Skip to content

Commit 780e869

Browse files
authored
Merge pull request #3518 from GauthamGoli/audit_bug_fix
lines_cop: Convert ARGV audit to negative look ahead
2 parents e3a8f81 + fb85ed0 commit 780e869

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

Library/Homebrew/rubocops/extend/formula_cop.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,22 @@ def find_instance_method_call(node, instance, method_name)
127127
end
128128
end
129129

130+
# Matches receiver part of method,
131+
# EX: to match `ARGV.<whatever>()`
132+
# call `find_instance_call(node, "ARGV")`
133+
# yields to a block with parent node of receiver
134+
def find_instance_call(node, name)
135+
node.each_descendant(:send) do |method_node|
136+
next if method_node.receiver.nil?
137+
next if method_node.receiver.const_name != name &&
138+
method_node.receiver.method_name != name
139+
@offense_source_range = method_node.receiver.source_range
140+
@offensive_node = method_node.receiver
141+
return true unless block_given?
142+
yield method_node
143+
end
144+
end
145+
130146
# Returns nil if does not depend on dependency_name
131147
# args: node - dependency_name - dependency's name
132148
def depends_on?(dependency_name, *types)

Library/Homebrew/rubocops/lines_cop.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,9 @@ def audit_formula(_node, _class_node, _parent_class_node, body_node)
162162
end
163163
end
164164

165-
[:debug?, :verbose?, :value].each do |method_name|
166-
find_instance_method_call(body_node, "ARGV", method_name) do
167-
problem "Use build instead of ARGV to check options"
168-
end
165+
find_instance_call(body_node, "ARGV") do |method_node|
166+
next if [:debug?, :verbose?, :value].index(method_node.method_name)
167+
problem "Use build instead of ARGV to check options"
169168
end
170169

171170
find_instance_method_call(body_node, :man, :+) do |method|

Library/Homebrew/test/rubocops/lines_cop_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,13 +541,12 @@ class Foo < Formula
541541
end
542542

543543
it "Using ARGV to check options" do
544-
expect_offense(<<~RUBY)
544+
expect_no_offenses(<<~RUBY)
545545
class Foo < Formula
546546
desc "foo"
547547
url 'http://example.com/foo-1.0.tgz'
548548
def install
549549
verbose = ARGV.verbose?
550-
^^^^^^^^^^^^^ Use build instead of ARGV to check options
551550
end
552551
end
553552
RUBY
@@ -739,6 +738,7 @@ class Foo < Formula
739738
test do
740739
head = ARGV.include? "--HEAD"
741740
^^^^^^ Use "if build.head?" instead
741+
^^^^ Use build instead of ARGV to check options
742742
end
743743
end
744744
RUBY

0 commit comments

Comments
 (0)