-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
176 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rake | ||
# This cop detects class or module definition in a task or namespace, | ||
# because it is defined to the top level. | ||
# It is confusing because the scope looks in the task or namespace, | ||
# but actually it is defined to the top level. | ||
# | ||
# @example | ||
# # bad | ||
# task :foo do | ||
# class C | ||
# end | ||
# end | ||
# | ||
# # bad | ||
# namespace :foo do | ||
# module M | ||
# end | ||
# end | ||
# | ||
# # good - It is also defined to the top level, | ||
# # but it looks expected behavior. | ||
# class C | ||
# end | ||
# task :foo do | ||
# end | ||
# | ||
class ClassDefinitionInTask < Cop | ||
MSG = 'Do not define a %<type>s in rake task, because it will be defined to the top level.' | ||
|
||
def on_class(node) | ||
return if Helper::ClassDefinition.in_class_definition?(node) | ||
return unless Helper::TaskDefinition.in_task_or_namespace?(node) | ||
|
||
add_offense(node) | ||
end | ||
|
||
def message(node) | ||
format(MSG, type: node.type) | ||
end | ||
|
||
alias on_module on_class | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rake | ||
module Helper | ||
module ClassDefinition | ||
extend NodePattern::Macros | ||
extend self | ||
|
||
def_node_matcher :class_definition?, <<~PATTERN | ||
{ | ||
class module sclass | ||
(block | ||
(send (const {nil? cbase} {:Class :Module}) :new) | ||
args | ||
_ | ||
) | ||
} | ||
PATTERN | ||
|
||
def in_class_definition?(node) | ||
node.each_ancestor(:class, :module, :sclass, :block).any? do |a| | ||
class_definition?(a) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rake | ||
module Helper | ||
module TaskDefinition | ||
extend NodePattern::Macros | ||
extend self | ||
|
||
def_node_matcher :task_or_namespace?, <<-PATTERN | ||
(block | ||
(send _ {:task :namespace} ...) | ||
args | ||
_ | ||
) | ||
PATTERN | ||
|
||
def in_task_or_namespace?(node) | ||
node.each_ancestor(:block).any? do |a| | ||
task_or_namespace?(a) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'rake/class_definition_in_task' | ||
require_relative 'rake/desc' | ||
require_relative 'rake/method_definition_in_task' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rake::ClassDefinitionInTask do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
let(:config) { RuboCop::Config.new } | ||
|
||
it 'registers an offense to a class definition in task' do | ||
expect_offense(<<~RUBY) | ||
task :foo do | ||
class C | ||
^^^^^^^ Do not define a class in rake task, because it will be defined to the top level. | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense to a module definition in task' do | ||
expect_offense(<<~RUBY) | ||
task :foo do | ||
module M | ||
^^^^^^^^ Do not define a module in rake task, because it will be defined to the top level. | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'allows class definition in Class.new' do | ||
expect_no_offenses(<<~RUBY) | ||
task :foo do | ||
Class.new do | ||
class C | ||
end | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'allows class definition outside task' do | ||
expect_no_offenses(<<~RUBY) | ||
class C | ||
end | ||
task :foo do | ||
end | ||
RUBY | ||
end | ||
end |
Seems you forgot to scope this to Rake files.