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

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
OnHomepageStanza
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 OnHomepageStanza

#on_cask

Methods included from CaskHelp

#on_block

Instance Method Details

#on_homepage_stanza(stanza) ⇒ Object



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

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(url_node.str_content).host

  # 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: url)) do |corrector|
    corrector.replace(url_node.source_range, corrected_source)
  end
end