diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-23 18:10:06 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-23 18:10:06 +0000 |
commit | 7ab026e2a20cd76e86929a4102a809542055897f (patch) | |
tree | 7b2e662fc3fb96033c82cf0d77f30e4adc14f3eb /rubocop | |
parent | 14fb5a922285d71fea67de59164ee4bb81ee3486 (diff) | |
download | gitlab-ce-7ab026e2a20cd76e86929a4102a809542055897f.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'rubocop')
-rw-r--r-- | rubocop/cop/put_group_routes_under_scope.rb | 32 | ||||
-rw-r--r-- | rubocop/cop/put_project_routes_under_scope.rb | 32 | ||||
-rw-r--r-- | rubocop/routes_under_scope.rb | 29 |
3 files changed, 37 insertions, 56 deletions
diff --git a/rubocop/cop/put_group_routes_under_scope.rb b/rubocop/cop/put_group_routes_under_scope.rb index bcdde01fdb5..9adec044da8 100644 --- a/rubocop/cop/put_group_routes_under_scope.rb +++ b/rubocop/cop/put_group_routes_under_scope.rb @@ -1,43 +1,19 @@ # frozen_string_literal: true +require_relative '../routes_under_scope' + module RuboCop module Cop # Checks for a group routes outside '/-/' scope. # For more information see: https://gitlab.com/gitlab-org/gitlab/issues/29572 class PutGroupRoutesUnderScope < RuboCop::Cop::Cop + include RoutesUnderScope + MSG = 'Put new group routes under /-/ scope' def_node_matcher :dash_scope?, <<~PATTERN (:send nil? :scope (hash <(pair (sym :path)(str "groups/*group_id/-")) ...>)) PATTERN - - def on_send(node) - return unless in_group_routes?(node) - return unless resource?(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 in_group_routes?(node) - path = node.location.expression.source_buffer.name - dirname = File.dirname(path) - filename = File.basename(path) - - dirname.end_with?('config/routes') && - filename.end_with?('group.rb') - end - - def resource?(node) - node.method_name == :resource || - node.method_name == :resources - end end end end diff --git a/rubocop/cop/put_project_routes_under_scope.rb b/rubocop/cop/put_project_routes_under_scope.rb index 02189f43ea0..cddb147324f 100644 --- a/rubocop/cop/put_project_routes_under_scope.rb +++ b/rubocop/cop/put_project_routes_under_scope.rb @@ -1,43 +1,19 @@ # frozen_string_literal: true +require_relative '../routes_under_scope' + module RuboCop module Cop # Checks for a project routes outside '/-/' scope. # For more information see: https://gitlab.com/gitlab-org/gitlab/issues/29572 class PutProjectRoutesUnderScope < RuboCop::Cop::Cop + include RoutesUnderScope + MSG = 'Put new project routes under /-/ scope' def_node_matcher :dash_scope?, <<~PATTERN (:send nil? :scope (:str "-")) PATTERN - - def on_send(node) - return unless in_project_routes?(node) - return unless resource?(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 in_project_routes?(node) - path = node.location.expression.source_buffer.name - dirname = File.dirname(path) - filename = File.basename(path) - - dirname.end_with?('config/routes') && - filename.end_with?('project.rb') - end - - def resource?(node) - node.method_name == :resource || - node.method_name == :resources - end end end end 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 |