Class: RuboCop::Cop::Cask::StanzaOrder

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector, Forwardable
Includes:
IgnoredNode, CaskHelp
Defined in:
rubocops/cask/stanza_order.rb

Overview

This cop checks that a cask’s stanzas are ordered correctly, including nested within on_* blocks.

Constant Summary collapse

MESSAGE =
"`%<stanza>s` stanza out of order"

Instance Method Summary collapse

Methods included from CaskHelp

#inner_stanzas, #on_block, #on_cask, #on_system_methods

Instance Method Details

#on_cask_stanza_block(stanza_block) ⇒ Object



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
# File 'rubocops/cask/stanza_order.rb', line 19

def on_cask_stanza_block(stanza_block)
  stanzas = stanza_block.stanzas
  ordered_stanzas = sort_stanzas(stanzas)

  return if stanzas == ordered_stanzas

  stanzas.zip(ordered_stanzas).each do |stanza_before, stanza_after|
    next if stanza_before == stanza_after

    add_offense(
      stanza_before.method_node,
      message: format(MESSAGE, stanza: stanza_before.stanza_name),
    ) do |corrector|
      next if part_of_ignored_node?(stanza_before.method_node)

      corrector.replace(
        stanza_before.source_range_with_comments,
        stanza_after.source_with_comments,
      )

      # Ignore node so that nested content is not auto-corrected and clobbered.
      ignore_node(stanza_before.method_node)
    end
  end
end

#on_new_investigationObject



45
46
47
48
49
# File 'rubocops/cask/stanza_order.rb', line 45

def on_new_investigation
  super

  ignored_nodes.clear
end