diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-23 21:10:06 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-23 21:10:06 +0300 |
commit | 7ab026e2a20cd76e86929a4102a809542055897f (patch) | |
tree | 7b2e662fc3fb96033c82cf0d77f30e4adc14f3eb /rubocop/routes_under_scope.rb | |
parent | 14fb5a922285d71fea67de59164ee4bb81ee3486 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'rubocop/routes_under_scope.rb')
-rw-r--r-- | rubocop/routes_under_scope.rb | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/rubocop/routes_under_scope.rb b/rubocop/routes_under_scope.rb new file mode 100644 index 00000000000..3f049bb09fa --- /dev/null +++ b/rubocop/routes_under_scope.rb @@ -0,0 +1,29 @@ +# 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) + + 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 + end +end |