lines_cop: Update regex patterns to prevent false positives

This commit is contained in:
Gautham Goli 2017-10-29 23:40:04 +05:30
parent 7caca57073
commit f648dd03ff
2 changed files with 20 additions and 20 deletions

View File

@ -105,25 +105,25 @@ module RuboCop
find_instance_method_call(body_node, :build, :without?) do |method| find_instance_method_call(body_node, :build, :without?) do |method|
arg = parameters(method).first arg = parameters(method).first
next unless match = regex_match_group(arg, /-?-?without-(.*)/) next unless match = regex_match_group(arg, /^-?-?without-(.*)/)
problem "Don't duplicate 'without': Use `build.without? \"#{match[1]}\"` to check for \"--without-#{match[1]}\"" problem "Don't duplicate 'without': Use `build.without? \"#{match[1]}\"` to check for \"--without-#{match[1]}\""
end end
find_instance_method_call(body_node, :build, :with?) do |method| find_instance_method_call(body_node, :build, :with?) do |method|
arg = parameters(method).first arg = parameters(method).first
next unless match = regex_match_group(arg, /-?-?with-(.*)/) next unless match = regex_match_group(arg, /^-?-?with-(.*)/)
problem "Don't duplicate 'with': Use `build.with? \"#{match[1]}\"` to check for \"--with-#{match[1]}\"" problem "Don't duplicate 'with': Use `build.with? \"#{match[1]}\"` to check for \"--with-#{match[1]}\""
end end
find_instance_method_call(body_node, :build, :include?) do |method| find_instance_method_call(body_node, :build, :include?) do |method|
arg = parameters(method).first arg = parameters(method).first
next unless match = regex_match_group(arg, /with(out)?-(.*)/) next unless match = regex_match_group(arg, /^with(out)?-(.*)/)
problem "Use build.with#{match[1]}? \"#{match[2]}\" instead of build.include? 'with#{match[1]}-#{match[2]}'" problem "Use build.with#{match[1]}? \"#{match[2]}\" instead of build.include? 'with#{match[1]}-#{match[2]}'"
end end
find_instance_method_call(body_node, :build, :include?) do |method| find_instance_method_call(body_node, :build, :include?) do |method|
arg = parameters(method).first arg = parameters(method).first
next unless match = regex_match_group(arg, /\-\-(.*)/) next unless match = regex_match_group(arg, /^\-\-(.*)$/)
problem "Reference '#{match[1]}' without dashes" problem "Reference '#{match[1]}' without dashes"
end end
end end
@ -145,7 +145,7 @@ module RuboCop
# Check for long inreplace block vars # Check for long inreplace block vars
find_all_blocks(body_node, :inreplace) do |node| find_all_blocks(body_node, :inreplace) do |node|
block_arg = node.arguments.children.first block_arg = node.arguments.children.first
next unless block_arg.source.size>1 next unless block_arg.source.size > 1
problem "\"inreplace <filenames> do |s|\" is preferred over \"|#{block_arg.source}|\"." problem "\"inreplace <filenames> do |s|\" is preferred over \"|#{block_arg.source}|\"."
end end
@ -169,7 +169,7 @@ module RuboCop
end end
find_instance_method_call(body_node, :man, :+) do |method| find_instance_method_call(body_node, :man, :+) do |method|
next unless match = regex_match_group(parameters(method).first, /man[1-8]/) next unless match = regex_match_group(parameters(method).first, /^man[1-8]$/)
problem "\"#{method.source}\" should be \"#{match[0]}\"" problem "\"#{method.source}\" should be \"#{match[0]}\""
end end
@ -194,18 +194,18 @@ module RuboCop
# Prefer formula path shortcuts in strings # Prefer formula path shortcuts in strings
formula_path_strings(body_node, :share) do |p| formula_path_strings(body_node, :share) do |p|
next unless match = regex_match_group(p, %r{(/(man))/?}) next unless match = regex_match_group(p, %r{^(/(man))/?})
problem "\"\#{share}#{match[1]}\" should be \"\#{#{match[2]}}\"" problem "\"\#{share}#{match[1]}\" should be \"\#{#{match[2]}}\""
end end
formula_path_strings(body_node, :prefix) do |p| formula_path_strings(body_node, :prefix) do |p|
if match = regex_match_group(p, %r{(/share/(info|man))$}) if match = regex_match_group(p, %r{^(/share/(info|man))$})
problem "\"\#\{prefix}#{match[1]}\" should be \"\#{#{match[2]}}\"" problem "\"\#\{prefix}#{match[1]}\" should be \"\#{#{match[2]}}\""
end end
if match = regex_match_group(p, %r{((/share/man/)(man[1-8]))}) if match = regex_match_group(p, %r{^((/share/man/)(man[1-8]))})
problem "\"\#\{prefix}#{match[1]}\" should be \"\#{#{match[3]}}\"" problem "\"\#\{prefix}#{match[1]}\" should be \"\#{#{match[3]}}\""
end end
if match = regex_match_group(p, %r{(/(bin|include|libexec|lib|sbin|share|Frameworks))}i) if match = regex_match_group(p, %r{^(/(bin|include|libexec|lib|sbin|share|Frameworks))}i)
problem "\"\#\{prefix}#{match[1]}\" should be \"\#{#{match[2].downcase}}\"" problem "\"\#\{prefix}#{match[1]}\" should be \"\#{#{match[2].downcase}}\""
end end
end end
@ -213,12 +213,12 @@ module RuboCop
find_every_method_call_by_name(body_node, :depends_on).each do |method| find_every_method_call_by_name(body_node, :depends_on).each do |method|
key, value = destructure_hash(parameters(method).first) key, value = destructure_hash(parameters(method).first)
next if key.nil? || value.nil? next if key.nil? || value.nil?
next unless match = regex_match_group(value, /(lua|perl|python|ruby)(\d*)/) next unless match = regex_match_group(value, /^(lua|perl|python|ruby)(\d*)/)
problem "#{match[1]} modules should be vendored rather than use deprecated #{method.source}`" problem "#{match[1]} modules should be vendored rather than use deprecated #{method.source}`"
end end
find_every_method_call_by_name(body_node, :system).each do |method| find_every_method_call_by_name(body_node, :system).each do |method|
next unless match = regex_match_group(parameters(method).first, /(env|export)(\s+)?/) next unless match = regex_match_group(parameters(method).first, /^(env|export)(\s+)?/)
problem "Use ENV instead of invoking '#{match[1]}' to modify the environment" problem "Use ENV instead of invoking '#{match[1]}' to modify the environment"
end end
@ -243,7 +243,7 @@ module RuboCop
find_instance_method_call(body_node, "ARGV", :include?) do |method| find_instance_method_call(body_node, "ARGV", :include?) do |method|
param = parameters(method).first param = parameters(method).first
next unless match = regex_match_group(param, /--(HEAD|devel)/) next unless match = regex_match_group(param, /^--(HEAD|devel)/)
problem "Use \"if build.#{match[1].downcase}?\" instead" problem "Use \"if build.#{match[1].downcase}?\" instead"
end end
@ -258,14 +258,14 @@ module RuboCop
conditional_dependencies(body_node) do |node, method, param, dep_node| conditional_dependencies(body_node) do |node, method, param, dep_node|
dep = string_content(dep_node) dep = string_content(dep_node)
if node.if? if node.if?
if (method == :include? && regex_match_group(param, /with-#{dep}$/)) || if (method == :include? && regex_match_group(param, /^with-#{dep}$/)) ||
(method == :with? && regex_match_group(param, /#{dep}$/)) (method == :with? && regex_match_group(param, /^#{dep}$/))
offending_node(dep_node.parent) offending_node(dep_node.parent)
problem "Replace #{node.source} with #{dep_node.parent.source} => :optional" problem "Replace #{node.source} with #{dep_node.parent.source} => :optional"
end end
elsif node.unless? elsif node.unless?
if (method == :include? && regex_match_group(param, /without-#{dep}$/)) || if (method == :include? && regex_match_group(param, /^without-#{dep}$/)) ||
(method == :without? && regex_match_group(param, /#{dep}$/)) (method == :without? && regex_match_group(param, /^#{dep}$/))
offending_node(dep_node.parent) offending_node(dep_node.parent)
problem "Replace #{node.source} with #{dep_node.parent.source} => :recommended" problem "Replace #{node.source} with #{dep_node.parent.source} => :recommended"
end end
@ -276,7 +276,7 @@ module RuboCop
problem "'fails_with :llvm' is now a no-op so should be removed" problem "'fails_with :llvm' is now a no-op so should be removed"
end end
find_method_with_args(body_node, :system, /(otool|install_name_tool|lipo)/) do find_method_with_args(body_node, :system, /^(otool|install_name_tool|lipo)/) do
next if @formula_name == "cctools" next if @formula_name == "cctools"
problem "Use ruby-macho instead of calling #{@offensive_node.source}" problem "Use ruby-macho instead of calling #{@offensive_node.source}"
end end

View File

@ -762,8 +762,8 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do
class Foo < Formula class Foo < Formula
desc "foo" desc "foo"
url 'http://example.com/foo-1.0.tgz' url 'http://example.com/foo-1.0.tgz'
depends_on "foo" if build.with? "with-foo" depends_on "foo" if build.with? "foo"
^^^^^^^^^^^^^^^^ Replace depends_on "foo" if build.with? "with-foo" with depends_on "foo" => :optional ^^^^^^^^^^^^^^^^ Replace depends_on "foo" if build.with? "foo" with depends_on "foo" => :optional
end end
RUBY RUBY
end end