Class: RuboCop::Cop::Cask::HomepageUrlStyling Private

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
OnHomepageStanza, HelperFunctions, HomepageHelper
Defined in:
rubocops/cask/homepage_url_styling.rb

Overview

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

This cop audits the homepage URL in casks.

Constant Summary collapse

MSG_NO_SLASH =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

"'%<url>s' must have a slash after the domain."

Instance Method Summary collapse

Methods included from HomepageHelper

#audit_homepage

Methods included from HelperFunctions

#block_method_called_in_block?, #block_size, #check_precedence, #class_name, #component_precedes?, #end_column, #expression_negated?, #find_all_blocks, #find_block, #find_blocks, #find_const, #find_every_func_call_by_name, #find_every_method_call_by_name, #find_instance_call, #find_instance_method_call, #find_method_calls_by_name, #find_method_def, #find_method_with_args, #find_node_method_by_name, #find_strings, #format_component, #line_number, #line_start_column, #method_called?, #method_called_ever?, #method_name, #node_equals?, #offending_node, #parameters, #parameters_passed?, #problem, #regex_match_group, #size, #source_buffer, #start_column, #string_content

Methods included from OnHomepageStanza

#on_cask

Methods included from CaskHelp

#inner_stanzas, #on_block, #on_cask, #on_cask_stanza_block, #on_system_methods

Instance Method Details

#on_homepage_stanza(stanza) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'rubocops/cask/homepage_url_styling.rb', line 20

def on_homepage_stanza(stanza)
  @name = cask_block.header.cask_token
  desc_call = stanza.stanza_node
  url_node = desc_call.first_argument

  url = if url_node.dstr_type?
    # Remove quotes from interpolated string.
    url_node.source[1..-2]
  else
    url_node.str_content
  end

  audit_homepage(:cask, url, desc_call, url_node)

  return unless url&.match?(%r{^.+://[^/]+$})

  domain = URI(string_content(url_node, strip_dynamic: true)).host
  return if domain.blank?

  # This also takes URLs like 'https://example.org?path'
  # and 'https://example.org#path' into account.
  corrected_source = url_node.source.sub("://#{domain}", "://#{domain}/")

  add_offense(url_node.loc.expression, message: format(MSG_NO_SLASH, url:)) do |corrector|
    corrector.replace(url_node.source_range, corrected_source)
  end
end