class RuboCop::Cop::Cask::HomepageMatchesUrl

This cop checks that a cask's homepage matches the download url, or if it doesn't, checks if a comment in the form `# example.com was verified as official when first introduced to the cask` is present.

Constants

COMMENT_FORMAT
MSG_MISSING
MSG_NO_MATCH
MSG_UNNECESSARY
MSG_WRONG_FORMAT
REFERENCE_URL

Attributes

cask_block[R]

Public Instance Methods

on_cask(cask_block) click to toggle source
# File lib/rubocop/cop/cask/homepage_matches_url.rb, line 30
def on_cask(cask_block)
  @cask_block = cask_block
  return unless homepage_stanza

  add_offenses
end

Private Instance Methods

add_offense_missing_comment(stanza) click to toggle source
# File lib/rubocop/cop/cask/homepage_matches_url.rb, line 64
def add_offense_missing_comment(stanza)
  return if url_match_homepage?(stanza)
  return if !url_match_homepage?(stanza) && comment?(stanza)

  range = stanza.source_range
  url_domain = domain(stanza)
  add_offense(range, location: range, message: format(MSG_MISSING, domain: url_domain, homepage: homepage))
end
add_offense_no_match(stanza) click to toggle source
# File lib/rubocop/cop/cask/homepage_matches_url.rb, line 73
def add_offense_no_match(stanza)
  return if url_match_homepage?(stanza)
  return unless comment?(stanza)
  return if !url_match_homepage?(stanza) && comment_matches_url?(stanza)

  comment = comment(stanza).loc.expression
  add_offense(comment,
              location: comment,
              message: format(MSG_NO_MATCH, url: url_from_comment(stanza), full_url: full_url(stanza)))
end
add_offense_unnecessary_comment(stanza) click to toggle source
# File lib/rubocop/cop/cask/homepage_matches_url.rb, line 52
def add_offense_unnecessary_comment(stanza)
  return unless comment?(stanza)
  return unless url_match_homepage?(stanza)
  return unless comment_matches_format?(stanza)
  return unless comment_matches_url?(stanza)

  comment = comment(stanza).loc.expression
  add_offense(comment,
              location: comment,
              message: format(MSG_UNNECESSARY, domain: domain(stanza), homepage: homepage))
end
add_offense_wrong_format(stanza) click to toggle source
# File lib/rubocop/cop/cask/homepage_matches_url.rb, line 84
def add_offense_wrong_format(stanza)
  return if url_match_homepage?(stanza)
  return unless comment?(stanza)
  return if comment_matches_format?(stanza)

  comment = comment(stanza).loc.expression
  add_offense(comment,
              location: comment,
              message: format(MSG_WRONG_FORMAT, comment: comment(stanza).text))
end
add_offenses() click to toggle source
# File lib/rubocop/cop/cask/homepage_matches_url.rb, line 43
def add_offenses
  toplevel_stanzas.select(&:url?).each do |url|
    next if add_offense_unnecessary_comment(url)
    next if add_offense_missing_comment(url)
    next if add_offense_no_match(url)
    next if add_offense_wrong_format(url)
  end
end
comment(stanza) click to toggle source
# File lib/rubocop/cop/cask/homepage_matches_url.rb, line 99
def comment(stanza)
  stanza.comments.last
end
comment?(stanza) click to toggle source
# File lib/rubocop/cop/cask/homepage_matches_url.rb, line 95
def comment?(stanza)
  !stanza.comments.empty?
end
comment_matches_format?(stanza) click to toggle source
# File lib/rubocop/cop/cask/homepage_matches_url.rb, line 103
def comment_matches_format?(stanza)
  comment(stanza).text =~ COMMENT_FORMAT
end
comment_matches_url?(stanza) click to toggle source
# File lib/rubocop/cop/cask/homepage_matches_url.rb, line 112
def comment_matches_url?(stanza)
  full_url(stanza).include?(url_from_comment(stanza))
end
domain(stanza) click to toggle source
# File lib/rubocop/cop/cask/homepage_matches_url.rb, line 120
def domain(stanza)
  strip_url_scheme(extract_url(stanza)).gsub(%r{^([^/]+).*}, '\1')
end
extract_url(stanza) click to toggle source
# File lib/rubocop/cop/cask/homepage_matches_url.rb, line 124
def extract_url(stanza)
  string = stanza.stanza_node.children[2]
  return string.str_content if string.str_type?

  string.to_s.gsub(%r{.*"([a-z0-9]+\:\/\/[^"]+)".*}m, '\1')
end
full_url(stanza) click to toggle source
# File lib/rubocop/cop/cask/homepage_matches_url.rb, line 135
def full_url(stanza)
  strip_url_scheme(extract_url(stanza))
end
homepage() click to toggle source
# File lib/rubocop/cop/cask/homepage_matches_url.rb, line 139
def homepage
  PublicSuffix.domain(domain(homepage_stanza))
end
homepage_stanza() click to toggle source
# File lib/rubocop/cop/cask/homepage_matches_url.rb, line 143
def homepage_stanza
  toplevel_stanzas.find(&:homepage?)
end
strip_url_scheme(url) click to toggle source
# File lib/rubocop/cop/cask/homepage_matches_url.rb, line 116
def strip_url_scheme(url)
  url.sub(%r{^.*://(www\.)?}, '')
end
url_from_comment(stanza) click to toggle source
# File lib/rubocop/cop/cask/homepage_matches_url.rb, line 107
def url_from_comment(stanza)
  comment(stanza).text
    .sub(/[^ ]*# ([^ ]+) .*/, '\1')
end
url_match_homepage?(stanza) click to toggle source
# File lib/rubocop/cop/cask/homepage_matches_url.rb, line 131
def url_match_homepage?(stanza)
  PublicSuffix.domain(domain(stanza)) == homepage
end