audit: avoid unnecessary regex
Regex is way slower than normal String#include? and String#start_with?. Also, we often forget to proper escape them. So avoid using them if it is not necessary. Closes #503. Signed-off-by: Xu Cheng <xucheng@me.com>
This commit is contained in:
parent
de1049f1f1
commit
12c505c093
@ -114,6 +114,10 @@ class FormulaText
|
|||||||
regex =~ @text
|
regex =~ @text
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def include?(s)
|
||||||
|
@text.include? s
|
||||||
|
end
|
||||||
|
|
||||||
def line_number(regex)
|
def line_number(regex)
|
||||||
index = @lines.index { |line| line =~ regex }
|
index = @lines.index { |line| line =~ regex }
|
||||||
index ? index + 1 : nil
|
index ? index + 1 : nil
|
||||||
@ -458,13 +462,13 @@ class FormulaAuditor
|
|||||||
|
|
||||||
# Check for http:// GitHub homepage urls, https:// is preferred.
|
# Check for http:// GitHub homepage urls, https:// is preferred.
|
||||||
# Note: only check homepages that are repo pages, not *.github.com hosts
|
# Note: only check homepages that are repo pages, not *.github.com hosts
|
||||||
if homepage =~ %r{^http://github\.com/}
|
if homepage.start_with? "http://github.com/"
|
||||||
problem "Please use https:// for #{homepage}"
|
problem "Please use https:// for #{homepage}"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Savannah has full SSL/TLS support but no auto-redirect.
|
# Savannah has full SSL/TLS support but no auto-redirect.
|
||||||
# Doesn't apply to the download URLs, only the homepage.
|
# Doesn't apply to the download URLs, only the homepage.
|
||||||
if homepage =~ %r{^http://savannah\.nongnu\.org/}
|
if homepage.start_with? "http://savannah.nongnu.org/"
|
||||||
problem "Please use https:// for #{homepage}"
|
problem "Please use https:// for #{homepage}"
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -671,19 +675,19 @@ class FormulaAuditor
|
|||||||
problem %(use "xcodebuild *args" instead of "system 'xcodebuild', *args")
|
problem %(use "xcodebuild *args" instead of "system 'xcodebuild', *args")
|
||||||
end
|
end
|
||||||
|
|
||||||
if text =~ /xcodebuild[ (]["'*]/ && text !~ /SYMROOT=/
|
if text =~ /xcodebuild[ (]["'*]/ && !text.include?("SYMROOT=")
|
||||||
problem %(xcodebuild should be passed an explicit "SYMROOT")
|
problem %(xcodebuild should be passed an explicit "SYMROOT")
|
||||||
end
|
end
|
||||||
|
|
||||||
if text =~ /Formula\.factory\(/
|
if text.include? "Formula.factory("
|
||||||
problem "\"Formula.factory(name)\" is deprecated in favor of \"Formula[name]\""
|
problem "\"Formula.factory(name)\" is deprecated in favor of \"Formula[name]\""
|
||||||
end
|
end
|
||||||
|
|
||||||
if text =~ /def plist/ && text !~ /plist_options/
|
if text.include?("def plist") && !text.include?("plist_options")
|
||||||
problem "Please set plist_options when using a formula-defined plist."
|
problem "Please set plist_options when using a formula-defined plist."
|
||||||
end
|
end
|
||||||
|
|
||||||
if text =~ %r{require "language/go"} && text !~ /go_resource/
|
if text.include?('require "language/go"') && !text.include?("go_resource")
|
||||||
problem "require \"language/go\" is unnecessary unless using `go_resource`s"
|
problem "require \"language/go\" is unnecessary unless using `go_resource`s"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -694,7 +698,7 @@ class FormulaAuditor
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Commented-out cmake support from default template
|
# Commented-out cmake support from default template
|
||||||
if line =~ /# system "cmake/
|
if line.include?('# system "cmake')
|
||||||
problem "Commented cmake call found"
|
problem "Commented cmake call found"
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -779,7 +783,7 @@ class FormulaAuditor
|
|||||||
problem "Use \"if build.#{$1.downcase}?\" instead"
|
problem "Use \"if build.#{$1.downcase}?\" instead"
|
||||||
end
|
end
|
||||||
|
|
||||||
if line =~ /make && make/
|
if line.include?("make && make")
|
||||||
problem "Use separate make calls"
|
problem "Use separate make calls"
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -787,7 +791,7 @@ class FormulaAuditor
|
|||||||
problem "Use spaces instead of tabs for indentation"
|
problem "Use spaces instead of tabs for indentation"
|
||||||
end
|
end
|
||||||
|
|
||||||
if line =~ /ENV\.x11/
|
if line.include?("ENV.x11")
|
||||||
problem "Use \"depends_on :x11\" instead of \"ENV.x11\""
|
problem "Use \"depends_on :x11\" instead of \"ENV.x11\""
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -844,19 +848,19 @@ class FormulaAuditor
|
|||||||
problem "Use build instead of ARGV to check options"
|
problem "Use build instead of ARGV to check options"
|
||||||
end
|
end
|
||||||
|
|
||||||
if line =~ /def options/
|
if line.include?("def options")
|
||||||
problem "Use new-style option definitions"
|
problem "Use new-style option definitions"
|
||||||
end
|
end
|
||||||
|
|
||||||
if line =~ /def test$/
|
if line.end_with?("def test")
|
||||||
problem "Use new-style test definitions (test do)"
|
problem "Use new-style test definitions (test do)"
|
||||||
end
|
end
|
||||||
|
|
||||||
if line =~ /MACOS_VERSION/
|
if line.include?("MACOS_VERSION")
|
||||||
problem "Use MacOS.version instead of MACOS_VERSION"
|
problem "Use MacOS.version instead of MACOS_VERSION"
|
||||||
end
|
end
|
||||||
|
|
||||||
if line =~ /MACOS_FULL_VERSION/
|
if line.include?("MACOS_FULL_VERSION")
|
||||||
problem "Use MacOS.full_version instead of MACOS_FULL_VERSION"
|
problem "Use MacOS.full_version instead of MACOS_FULL_VERSION"
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -878,7 +882,7 @@ class FormulaAuditor
|
|||||||
problem "Define method #{$1.inspect} in the class body, not at the top-level"
|
problem "Define method #{$1.inspect} in the class body, not at the top-level"
|
||||||
end
|
end
|
||||||
|
|
||||||
if line =~ /ENV.fortran/ && !formula.requirements.map(&:class).include?(FortranRequirement)
|
if line.include?("ENV.fortran") && !formula.requirements.map(&:class).include?(FortranRequirement)
|
||||||
problem "Use `depends_on :fortran` instead of `ENV.fortran`"
|
problem "Use `depends_on :fortran` instead of `ENV.fortran`"
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -908,7 +912,7 @@ class FormulaAuditor
|
|||||||
problem "Use `assert_match` instead of `assert ...include?`"
|
problem "Use `assert_match` instead of `assert ...include?`"
|
||||||
end
|
end
|
||||||
|
|
||||||
if line =~ /system "npm", "install"/ && line !~ /Language::Node/ && formula.name !~ /^kibana(\d{2})?$/
|
if line.include?('system "npm", "install"') && !line.include?("Language::Node") && formula.name !~ /^kibana(\d{2})?$/
|
||||||
problem "Use Language::Node for npm install args"
|
problem "Use Language::Node for npm install args"
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -936,9 +940,9 @@ class FormulaAuditor
|
|||||||
end
|
end
|
||||||
|
|
||||||
def audit_caveats
|
def audit_caveats
|
||||||
caveats = formula.caveats
|
caveats = formula.caveats.to_s
|
||||||
|
|
||||||
if caveats =~ /setuid/
|
if caveats.include?("setuid")
|
||||||
problem "Don't recommend setuid in the caveats, suggest sudo instead."
|
problem "Don't recommend setuid in the caveats, suggest sudo instead."
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -1063,7 +1067,7 @@ class ResourceAuditor
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if version.to_s =~ /^v/
|
if version.to_s.start_with?("v")
|
||||||
problem "version #{version} should not have a leading 'v'"
|
problem "version #{version} should not have a leading 'v'"
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -1196,8 +1200,8 @@ class ResourceAuditor
|
|||||||
# Check SourceForge urls
|
# Check SourceForge urls
|
||||||
urls.each do |p|
|
urls.each do |p|
|
||||||
# Skip if the URL looks like a SVN repo
|
# Skip if the URL looks like a SVN repo
|
||||||
next if p =~ %r{/svnroot/}
|
next if p.include? "/svnroot/"
|
||||||
next if p =~ /svn\.sourceforge/
|
next if p.include? "svn.sourceforge"
|
||||||
|
|
||||||
# Is it a sourceforge http(s) URL?
|
# Is it a sourceforge http(s) URL?
|
||||||
next unless p =~ %r{^https?://.*\b(sourceforge|sf)\.(com|net)}
|
next unless p =~ %r{^https?://.*\b(sourceforge|sf)\.(com|net)}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user