Skip to content
代码片段 群组 项目
未验证 提交 ac614e1b 编辑于 作者: Doug Stull's avatar Doug Stull 提交者: GitLab
浏览文件

Merge branch 'pl-rubocop-rake-require-namespace' into 'master'

Rake/Require: Flag use of `require` in `namespace` blocks

See merge request https://gitlab.com/gitlab-org/gitlab/-/merge_requests/149555



Merged-by: default avatarDoug Stull <dstull@gitlab.com>
Approved-by: default avatarIvane Gkomarteli <igkomarteli@gitlab.com>
Approved-by: default avatarDoug Stull <dstull@gitlab.com>
Reviewed-by: default avatarDoug Stull <dstull@gitlab.com>
Co-authored-by: default avatarPeter Leitzen <pleitzen@gitlab.com>
No related branches found
No related tags found
无相关合并请求
---
Rake/Require:
Details: grace period
Exclude:
- 'ee/lib/tasks/contracts/merge_requests.rake'
- 'lib/tasks/contracts/merge_requests.rake'
- 'lib/tasks/contracts/pipeline_schedules.rake'
- 'lib/tasks/contracts/pipelines.rake'
- 'lib/tasks/gitlab/artifacts/migrate.rake'
- 'lib/tasks/gitlab/backup.rake'
- 'lib/tasks/gitlab/cleanup.rake'
- 'lib/tasks/gitlab/docs/redirect.rake'
- 'lib/tasks/gitlab/graphql.rake'
- 'lib/tasks/gitlab/lfs/migrate.rake'
- 'lib/tasks/gitlab/openapi.rake'
- 'lib/tasks/import.rake'
- 'lib/tasks/tokens.rake'
- 'qa/tasks/ci.rake'
...@@ -17,6 +17,14 @@ module Rake ...@@ -17,6 +17,14 @@ module Rake
# Gitlab::Json.parse(...) # Gitlab::Json.parse(...)
# end # end
# #
# namespace :json do
# require_relative 'gitlab/json'
# require 'json'
#
# task :parse_json do
# end
# end
#
# # good # # good
# #
# task :parse_json do # task :parse_json do
...@@ -26,6 +34,15 @@ module Rake ...@@ -26,6 +34,15 @@ module Rake
# Gitlab::Json.parse(...) # Gitlab::Json.parse(...)
# end # end
# #
# namespace :json do
# task :parse_json do
# require_relative 'gitlab/json'
# require 'json'
#
# Gitlab::Json.parse(...)
# end
# end
#
# RSpec::Core::RakeTask.new(:parse_json) do |t, args| # RSpec::Core::RakeTask.new(:parse_json) do |t, args|
# require_relative 'gitlab/json' # require_relative 'gitlab/json'
# require 'json' # require 'json'
...@@ -54,6 +71,8 @@ class Require < RuboCop::Cop::Base ...@@ -54,6 +71,8 @@ class Require < RuboCop::Cop::Base
METHODS = %i[require require_relative].freeze METHODS = %i[require require_relative].freeze
RESTRICT_ON_SEND = METHODS RESTRICT_ON_SEND = METHODS
EAGER_EVALUATED_BLOCKS = %i[namespace].freeze
def_node_matcher :require_method, <<~PATTERN def_node_matcher :require_method, <<~PATTERN
(send nil? ${#{METHODS.map(&:inspect).join(' ')}} $_) (send nil? ${#{METHODS.map(&:inspect).join(' ')}} $_)
PATTERN PATTERN
...@@ -65,7 +84,8 @@ def on_send(node) ...@@ -65,7 +84,8 @@ def on_send(node)
return unless method return unless method
return if requires_task?(file) return if requires_task?(file)
return if inside_block_or_method?(node) return if inside_block(node, skip: EAGER_EVALUATED_BLOCKS)
return if inside_method?(node)
add_offense(node) add_offense(node)
end end
...@@ -85,8 +105,14 @@ def requires_task?(file) ...@@ -85,8 +105,14 @@ def requires_task?(file)
file.source.include?('task') file.source.include?('task')
end end
def inside_block_or_method?(node) def inside_block(node, skip:)
node.each_ancestor(:block, :def).any? node.each_ancestor(:block).any? do |block|
!skip.include?(block.method_name) # rubocop:disable Rails/NegateInclude -- This is not Rails
end
end
def inside_method?(node)
node.each_ancestor(:def, :defs).any?
end end
end end
end end
......
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
allow(cop).to receive(:in_rake_file?).and_return(true) allow(cop).to receive(:in_rake_file?).and_return(true)
end end
it 'registers an offenses for require methods' do it 'registers offenses for require methods' do
expect_offense(<<~RUBY) expect_offense(<<~RUBY)
require 'json' require 'json'
^^^^^^^^^^^^^^ #{msg} ^^^^^^^^^^^^^^ #{msg}
...@@ -35,6 +35,26 @@ ...@@ -35,6 +35,26 @@
RUBY RUBY
end end
it 'registers offenses for require methods inside `namespace` definitions' do
expect_offense(<<~RUBY)
namespace :foo do
require 'json'
^^^^^^^^^^^^^^ #{msg}
task :parse do
end
end
namespace :bar do
require_relative 'gitlab/json'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
task :parse do
end
end
RUBY
end
it 'does not register offense inside `task` definition' do it 'does not register offense inside `task` definition' do
expect_no_offenses(<<~RUBY) expect_no_offenses(<<~RUBY)
task :parse do task :parse do
...@@ -63,8 +83,13 @@ def load_deps ...@@ -63,8 +83,13 @@ def load_deps
require 'json' require 'json'
end end
def self.run
require 'yaml'
end
task :parse do task :parse do
load_deps load_deps
run
end end
RUBY RUBY
end end
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册