Skip to content

Commit

Permalink
Allow definitions within a task that is within a namespace.
Browse files Browse the repository at this point in the history
This is similar to definitions within a task (without a namespace).
  • Loading branch information
Thong Kuah committed Jan 23, 2024
1 parent 2915482 commit 000d0e2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/rubocop/cop/rake/class_definition_in_namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class ClassDefinitionInNamespace < Base

def on_class(node)
return if Helper::ClassDefinition.in_class_definition?(node)
return if Helper::TaskDefinition.in_task?(node)

return unless Helper::TaskDefinition.in_namespace?(node)

add_offense(node, message: format(MSG, type: node.type))
Expand Down
14 changes: 14 additions & 0 deletions lib/rubocop/cop/rake/helper/task_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,25 @@ module TaskDefinition
)
PATTERN

def_node_matcher :task?, <<-PATTERN
(block
(send _ {:task} ...)
args
_
)
PATTERN

def in_namespace?(node)
node.each_ancestor(:block).any? do |a|
namespace?(a)
end
end

def in_task?(node)
node.each_ancestor(:block).any? do |a|
task?(a)
end
end
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cop/rake/method_definition_in_namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class MethodDefinitionInNamespace < Base

def on_def(node)
return if Helper::ClassDefinition.in_class_definition?(node)
return if Helper::TaskDefinition.in_task?(node)

return unless Helper::TaskDefinition.in_namespace?(node)

add_offense(node)
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/rake/class_definition_in_namespace_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ module M
RUBY
end

it 'allows class definition in a task when wrapped in a namespace' do
expect_no_offenses(<<~RUBY)
namespace :bar do
task :foo do
class C
end
end
end
RUBY
end

it 'registers an offense to a class definition in namespace' do
expect_offense(<<~RUBY)
namespace 'foo' do
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/rake/method_definition_in_namespace_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ def self.foo
RUBY
end

it 'does not register an offense to `def` in a task when wrapped in a namespace' do
expect_no_offenses(<<~RUBY)
namespace :bar do
task :foo do
def helper_method
do_something
end
end
end
RUBY
end

it 'registers an offense to `def` in a namespace' do
expect_offense(<<~RUBY)
namespace 'foo' do
Expand Down

0 comments on commit 000d0e2

Please sign in to comment.