Class: RuboCop::Cop::Cask::HomepageUrlTrailingSlash

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

Overview

This cop checks that a cask’s homepage ends with a slash if it does not have a path component.

Constant Summary collapse

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

Instance Method Summary collapse

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 Kernel

#disk_usage_readable, #ensure_executable!, #ensure_formula_installed!, #exec_browser, #exec_editor, #ignore_interrupts, #interactive_shell, #number_readable, #odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #paths, #pretty_duration, #pretty_installed, #pretty_outdated, #pretty_uninstalled, #quiet_system, #redact_secrets, #redirect_stdout, #require?, #safe_system, #tap_and_name_comparison, #truncate_text_to_approximate_size, #which, #which_all, #which_editor, #with_custom_locale, #with_env, #with_homebrew_path

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



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

def on_homepage_stanza(stanza)
  url_node = stanza.stanza_node.first_argument

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

  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