Class: RuboCop::Cop::Cask::NoOverrides

Inherits:
Base
  • Object
show all
Includes:
CaskHelp
Defined in:
rubocops/cask/no_overrides.rb

Constant Summary collapse

OVERRIDEABLE_METHODS =

These stanzas can be overridden by on_* blocks, so take them into account.

[
  :appcast, :arch, :auto_updates, :conflicts_with, :container,
  :desc, :homepage, :sha256, :url, :version
].freeze
MESSAGE =
"Do not use a top-level `%<stanza>s` stanza as the default. " \
"Add it to an `on_{system}` block instead. " \
"Use `:or_older` or `:or_newer` to specify a range of macOS versions."

Instance Method Summary collapse

Methods included from CaskHelp

#inner_stanzas, #on_block, #on_cask_stanza_block, #on_system_methods

Instance Method Details

#inside_livecheck_block?(node) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'rubocops/cask/no_overrides.rb', line 58

def inside_livecheck_block?(node)
  single_stanza_livecheck_block?(node) || multi_stanza_livecheck_block?(node)
end

#multi_stanza_livecheck_block?(node) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
# File 'rubocops/cask/no_overrides.rb', line 66

def multi_stanza_livecheck_block?(node)
  grandparent_node = node.parent.parent
  node.parent.begin_type? && grandparent_node.block_type? && grandparent_node.method_name == :livecheck
end

#on_cask(cask_block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'rubocops/cask/no_overrides.rb', line 20

def on_cask(cask_block)
  cask_stanzas = cask_block.toplevel_stanzas

  return if (on_blocks = on_system_methods(cask_stanzas)).none?

  stanzas_in_blocks = on_system_stanzas(on_blocks)

  cask_stanzas.each do |stanza|
    # Skip if the stanza is not allowed to be overridden.
    next unless OVERRIDEABLE_METHODS.include?(stanza.stanza_name)
    # Skip if the stanza outside of a block is not also in an `on_*` block.
    next unless stanzas_in_blocks.include?(stanza.stanza_name)

    add_offense(stanza.source_range, message: format(MESSAGE, stanza: stanza.stanza_name))
  end
end

#on_system_stanzas(on_system) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'rubocops/cask/no_overrides.rb', line 37

def on_system_stanzas(on_system)
  names = Set.new
  method_nodes = on_system.map(&:method_node)
  method_nodes.select(&:block_type?).each do |node|
    node.child_nodes.each do |child|
      child.each_node(:send) do |send_node|
        # Skip (nested) livecheck blocks as its `url` is different to a download `url`.
        next if send_node.method_name == :livecheck || inside_livecheck_block?(send_node)
        # Skip string interpolations.
        if send_node.ancestors.drop_while { |a| !a.begin_type? }.any? { |a| a.dstr_type? || a.regexp_type? }
          next
        end
        next if RuboCop::Cask::Constants::ON_SYSTEM_METHODS.include?(send_node.method_name)

        names.add(send_node.method_name)
      end
    end
  end
  names
end

#single_stanza_livecheck_block?(node) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'rubocops/cask/no_overrides.rb', line 62

def single_stanza_livecheck_block?(node)
  node.parent.block_type? && node.parent.method_name == :livecheck
end