diff options
| author | Gautham Goli | 2017-07-30 12:57:57 +0530 |
|---|---|---|
| committer | Gautham Goli | 2017-07-30 12:59:37 +0530 |
| commit | c575f34d5fe802962bfb2749a00053ad3561da55 (patch) | |
| tree | a92805f4a9ddf1496eef92d15e33b9987ba85fc4 /Library/Homebrew/rubocops | |
| parent | 627b1dae707b4b5ea5e299ff12dd8ac1abbfa056 (diff) | |
| download | brew-c575f34d5fe802962bfb2749a00053ad3561da55.tar.bz2 | |
audit: Port audit_urls strict rules to rubocop, add tests and autocorrect
Diffstat (limited to 'Library/Homebrew/rubocops')
| -rw-r--r-- | Library/Homebrew/rubocops/extend/formula_cop.rb | 14 | ||||
| -rw-r--r-- | Library/Homebrew/rubocops/urls_cop.rb | 31 |
2 files changed, 37 insertions, 8 deletions
diff --git a/Library/Homebrew/rubocops/extend/formula_cop.rb b/Library/Homebrew/rubocops/extend/formula_cop.rb index 7165ee354..169b1c34a 100644 --- a/Library/Homebrew/rubocops/extend/formula_cop.rb +++ b/Library/Homebrew/rubocops/extend/formula_cop.rb @@ -37,6 +37,20 @@ module RuboCop match_object end + # Yields to block when there is a match + # Parameters: urls : Array of url/mirror method call nodes + # regex: regex pattern to match urls + def audit_urls(urls, regex) + urls.each do |url_node| + url_string_node = parameters(url_node).first + url_string = string_content(url_string_node) + match_object = regex_match_group(url_string_node, regex) + next unless match_object + offending_node(url_string_node.parent) + yield match_object, url_string + end + end + # Returns all string nodes among the descendants of given node def find_strings(node) return [] if node.nil? diff --git a/Library/Homebrew/rubocops/urls_cop.rb b/Library/Homebrew/rubocops/urls_cop.rb index 94f049aed..676e73523 100644 --- a/Library/Homebrew/rubocops/urls_cop.rb +++ b/Library/Homebrew/rubocops/urls_cop.rb @@ -188,17 +188,32 @@ module RuboCop problem "#{url} should be `https://search.maven.org/remotecontent?filepath=#{match[1]}`" end end + end + end + module FormulaAuditStrict + class PyPiUrls < FormulaCop + def audit_formula(_node, _class_node, _parent_class_node, body_node) + urls = find_every_method_call_by_name(body_node, :url) + mirrors = find_every_method_call_by_name(body_node, :mirror) + urls += mirrors + + # Check pypi urls + @pypi_pattern = %r{^https?://pypi.python.org/(.*)} + audit_urls(urls, @pypi_pattern) do |match, url| + problem "#{url} should be `https://files.pythonhosted.org/#{match[1]}`" + end + end private - def audit_urls(urls, regex) - urls.each do |url_node| - url_string_node = parameters(url_node).first - url_string = string_content(url_string_node) - match_object = regex_match_group(url_string_node, regex) - next unless match_object - offending_node(url_string_node.parent) - yield match_object, url_string + def autocorrect(node) + lambda do |corrector| + url_string_node = parameters(node).first + url = string_content(url_string_node) + match = regex_match_group(url_string_node, @pypi_pattern) + correction = node.source.sub(url, "https://files.pythonhosted.org/#{match[1]}") + corrector.insert_before(node.source_range, correction) + corrector.remove(node.source_range) end end end |
