Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Checks that `include`, `extend` and `prepend` exists at the top level. Using these at the top level affects the behavior of `Object`. ```console % cat app/models/example.rb include M class Example < ApplicationRecord end ``` ```console % rubocop app/models/example.rb Inspecting 1 file C Offenses: /tmp/example.rb:3:1: C: include is used at the top level. Use inside class or module. include M ^^^^^^^^^ 1 file inspected, 1 offense detected ``` Since it is difficult to automatically determine the position of `include` for class including multiple modules, autocorrect is not provided.
- Loading branch information
Showing
7 changed files
with
204 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
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,73 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# This cop checks that `include`, `extend` and `prepend` exists at | ||
# the top level. | ||
# Using these at the top level affects the behavior of `Object`. | ||
# There will not be using `include`, `extend` and `prepend` at | ||
# the top level. Let's use it inside `class` or `module`. | ||
# | ||
# @example | ||
# # bad | ||
# include M | ||
# | ||
# class C | ||
# end | ||
# | ||
# # bad | ||
# extend M | ||
# | ||
# class C | ||
# end | ||
# | ||
# # bad | ||
# prepend M | ||
# | ||
# class C | ||
# end | ||
# | ||
# # good | ||
# class C | ||
# include M | ||
# end | ||
# | ||
# # good | ||
# class C | ||
# extend M | ||
# end | ||
# | ||
# # good | ||
# class C | ||
# prepend M | ||
# end | ||
class MixinUsage < Cop | ||
MSG = '`%<statement>s` is used at the top level. Use inside `class` ' \ | ||
'or `module`.'.freeze | ||
|
||
def_node_matcher :include_statement, <<-PATTERN | ||
(send nil? ${:include :extend :prepend} | ||
(const nil? _)) | ||
PATTERN | ||
|
||
def on_send(node) | ||
return unless (statement = include_statement(node)) | ||
return unless top_level_node?(node) | ||
|
||
add_offense(node, message: format(MSG, statement: statement)) | ||
end | ||
|
||
private | ||
|
||
def top_level_node?(node) | ||
if node.parent.parent.nil? | ||
node.sibling_index.zero? | ||
else | ||
top_level_node?(node.parent) | ||
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
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,75 @@ | ||
# frozen_string_literal: true | ||
|
||
describe RuboCop::Cop::Style::MixinUsage do | ||
let(:config) { RuboCop::Config.new } | ||
subject(:cop) { described_class.new(config) } | ||
|
||
context 'include' do | ||
it 'registers an offense when using outside class' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
include M | ||
^^^^^^^^^ `include` is used at the top level. Use inside `class` or `module`. | ||
class C | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using inside class' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
class C | ||
include M | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'extend' do | ||
it 'registers an offense when using outside class' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
extend M | ||
^^^^^^^^ `extend` is used at the top level. Use inside `class` or `module`. | ||
class C | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using inside class' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
class C | ||
extend M | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'prepend' do | ||
it 'registers an offense when using outside class' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
prepend M | ||
^^^^^^^^^ `prepend` is used at the top level. Use inside `class` or `module`. | ||
class C | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using inside class' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
class C | ||
prepend M | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
it 'does not register an offense when using inside nested module' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
module M1 | ||
include M2 | ||
class C | ||
include M3 | ||
end | ||
end | ||
RUBY | ||
end | ||
end |