summaryrefslogtreecommitdiff
path: root/rubocop/routes_under_scope.rb
blob: 7ffb0fd36f232a4b1e204f8a966c9efa86b34cfd (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
# frozen_string_literal: true

module RuboCop
  # Common code used to implement cops checking routes outside of /-/ scope.
  #
  # Examples:
  # * RuboCop::Cop::PutProjectRoutesUnderScope
  # * RuboCop::Cop::PutGroupRoutesUnderScope
  module RoutesUnderScope
    ROUTE_METHODS = Set.new(%i[resource resources get post put patch delete]).freeze

    def on_send(node)
      return unless route_method?(node)
      return unless outside_scope?(node)
      return if root_route?(node)

      add_offense(node)
    end

    def outside_scope?(node)
      node.each_ancestor(:block).none? do |parent|
        dash_scope?(parent.to_a.first)
      end
    end

    def route_method?(node)
      ROUTE_METHODS.include?(node.method_name)
    end

    def root_route?(node)
      first_argument = node.arguments.first

      if first_argument.respond_to?(:value)
        first_argument.value == '/'
      end
    end
  end
end