Class: RuboCop::Cop::Cask::Url Private

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
OnUrlStanza, UrlHelper
Defined in:
rubocops/cask/url.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 checks that a cask's url stanza is formatted correctly.

Direct Known Subclasses

UrlLegacyCommaSeparators

Instance Method Summary collapse

Methods included from UrlHelper

#audit_url, #audit_urls

Methods included from HelperFunctions

#block_method_called_in_block?, #check_precedence, #class_name, #component_precedes?, descendant_send_nodes, descendant_send_nodes_by_method_name, #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, #source_buffer, #start_column, #string_content

Methods included from OnUrlStanza

#on_cask, #toplevel_stanzas

Methods included from CaskHelp

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

Instance Method Details

#on_url_stanza(stanza) ⇒ void

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.

This method returns an undefined value.

Parameters:



17
18
19
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'rubocops/cask/url.rb', line 17

def on_url_stanza(stanza)
  if stanza.stanza_node.block_type?
    if cask_tap == "homebrew-cask"
      add_offense(stanza.stanza_node, message: 'Do not use `url "..." do` blocks in Homebrew/homebrew-cask.')
    end
    return
  end

  stanza_node = T.cast(stanza.stanza_node, RuboCop::AST::SendNode)
  url_stanza = stanza_node.first_argument
  hash_node = stanza_node.last_argument

  if url_stanza.nil? || url_stanza.hash_type?
    add_offense(stanza_node.source_range, message: "The `url` stanza requires a URL argument.")
    return
  end

  audit_url(:cask, [stanza_node], [], livecheck_urls: [])

  if cask_tap == "homebrew-cask" && !url_stanza.type?(:str, :dstr)
    add_offense(url_stanza.source_range, message: "Casks in homebrew/cask should use string literal URLs.")
  end

  # Check for http:// URLs in homebrew-cask (skip deprecated/disabled casks)
  # TODO: Remove the deprecated/disabled check after Homebrew/cask has no more
  # deprecated/disabled casks using http:// URLs
  deprecated_or_disabled = toplevel_stanzas.any? { |s| [:deprecate!, :disable!].include?(s.stanza_name) }
  if cask_tap == "homebrew-cask" && !deprecated_or_disabled && url_stanza.source.match?(%r{^"http://})
    add_offense(
      stanza_node.source_range,
      message: "Casks in homebrew/cask should not use http:// URLs",
    ) do |corrector|
      corrector.replace(stanza_node.source_range, stanza_node.source.sub("http://", "https://"))
    end
  end

  return unless hash_node.hash_type?

  # TODO: also enforce that each keyword parameter after the first
  #       starts on its own line.
  return if hash_node.first_line > url_stanza.last_line && hash_node.loc.column > stanza_node.loc.column

  add_offense(
    stanza_node.source_range,
    message: "Keyword URL parameter should be on a new indented line.",
  ) do |corrector|
    corrector.replace(
      range_between(url_stanza.source_range.end_pos, hash_node.source_range.begin_pos),
      ",\n#{" " * url_stanza.loc.column}",
    )
  end
end