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
3 changed files
with
113 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Discourse | ||
module Plugins | ||
class NamespaceMethods < Base | ||
MSG = "Don’t define methods outside a class or a module." | ||
|
||
def on_def(node) | ||
return if inside_namespace?(node) | ||
add_offense(node, message: MSG) | ||
end | ||
|
||
private | ||
|
||
def inside_namespace?(node) | ||
node.each_ancestor.detect { _1.class_type? || _1.module_type? } | ||
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,86 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe RuboCop::Cop::Discourse::Plugins::NamespaceMethods, :config do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
let(:config) { RuboCop::Config.new } | ||
|
||
context "when defining a method outside any namespace" do | ||
it "registers an offense" do | ||
expect_offense(<<~RUBY) | ||
def my_method | ||
^^^^^^^^^^^^^ Discourse/Plugins/NamespaceMethods: Don’t define methods outside a class or a module. | ||
"my_value" | ||
end | ||
class MyClass | ||
def my_method | ||
"my_method" | ||
end | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context "when defining a method inside a class" do | ||
context "when defining an instance method" do | ||
it "does not register an offense" do | ||
expect_no_offenses(<<~RUBY) | ||
class MyClass | ||
def my_method | ||
"my_value" | ||
end | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context "when defining a class method" do | ||
it "does not register an offense" do | ||
expect_no_offenses(<<~RUBY) | ||
class MyClass | ||
class << self | ||
def my_method | ||
"my_value" | ||
end | ||
def another_method | ||
"plop" | ||
end | ||
end | ||
end | ||
RUBY | ||
end | ||
end | ||
end | ||
|
||
context "when defining a method inside a module" do | ||
context "when defining an instance method" do | ||
it "does not register an offense" do | ||
expect_no_offenses(<<~RUBY) | ||
module MyModule | ||
def my_method | ||
"my_value" | ||
end | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context "when defining a class method" do | ||
it "does not register an offense" do | ||
expect_no_offenses(<<~RUBY) | ||
module MyModule | ||
class << self | ||
def my_method | ||
"my_value" | ||
end | ||
end | ||
end | ||
RUBY | ||
end | ||
end | ||
end | ||
end |