summaryrefslogtreecommitdiff
path: root/rubocop/cop/inject_enterprise_edition_module.rb
blob: 1d37b1bd12d41c3a74cac266a604fa6f23310294 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
# frozen_string_literal: true

module RuboCop
  module Cop
    # Cop that blacklists the injecting of EE specific modules anywhere but on
    # the last line of a file. Injecting a module in the middle of a file will
    # cause merge conflicts, while placing it on the last line will not.
    class InjectEnterpriseEditionModule < RuboCop::Cop::Cop
      MSG = 'Injecting EE modules must be done on the last line of this file' \
        ', outside of any class or module definitions'

      METHODS = Set.new(%i[include extend prepend]).freeze

      def ee_const?(node)
        line = node.location.expression.source_line

        # We use `match?` here instead of RuboCop's AST matching, as this makes
        # it far easier to handle nested constants such as `EE::Foo::Bar::Baz`.
        line.match?(/(\s|\()(::)?EE::/)
      end

      def on_send(node)
        return unless METHODS.include?(node.children[1])
        return unless ee_const?(node.children[2])

        line = node.location.line
        buffer = node.location.expression.source_buffer
        last_line = buffer.last_line

        # Parser treats the final newline (if present) as a separate line,
        # meaning that a simple `line < last_line` would yield true even though
        # the expression is the last line _of code_.
        last_line -= 1 if buffer.source.end_with?("\n")

        add_offense(node) if line < last_line
      end

      # Automatically correcting these offenses is not always possible, as
      # sometimes code needs to be refactored to make this work. As such, we
      # only allow developers to easily blacklist existing offenses.
      def autocorrect(node)
        lambda do |corrector|
          corrector.insert_after(
            node.source_range,
            " # rubocop: disable #{cop_name}"
          )
        end
      end
    end
  end
end