-
-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new Rails/ActiveSupportOnLoad cop.
This cop is extracted from Shopify's internal Rubocop repository. Many thanks to the original authors for their work. Julian Nadeau <[email protected]> Rafael Mendonça França <[email protected]> Francois Chagnon <[email protected]> Jean Boussier <[email protected]>
- Loading branch information
Bart de Water
committed
Jul 22, 2022
1 parent
3ad75cc
commit e3c173f
Showing
5 changed files
with
173 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# Use Active Support lazy load hooks to patch Rails core classes, so they are not forcible loaded early. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# ActiveRecord::Base.include(MyClass) | ||
# | ||
# # good | ||
# ActiveSupport.on_load(:active_record) { include MyClass } | ||
class ActiveSupportOnLoad < Base | ||
extend AutoCorrector | ||
|
||
AUTOCORRECTABLE_CLASSES = { | ||
'ActiveRecord::Base' => 'active_record', | ||
'ActionController::Base' => 'action_controller', | ||
'ActiveJob::Base' => 'active_job', | ||
'ActionView::Base' => 'action_view', | ||
'ActionMailer::Base' => 'action_mailer', | ||
'ActionController::TestCase' => 'action_controller_test_case', | ||
'ActiveSupport::TestCase' => 'active_support_test_case', | ||
'ActiveJob::TestCase' => 'active_job_test_case', | ||
'ActionDispatch::IntegrationTest' => 'action_dispatch_integration_test', | ||
'ActionMailer::TestCase' => 'action_mailer_test_case' | ||
}.freeze | ||
|
||
RESTRICT_ON_SEND = %i[prepend include extend].freeze | ||
|
||
MSG = | ||
"Don't use \x1b[32m%<source>s\x1b[0m. Use this form instead \x1b[32m%<preferred>s\x1b[0m" | ||
|
||
def on_send(node) | ||
receiver, method, arguments = *node | ||
return unless receiver && AUTOCORRECTABLE_CLASSES.key?(receiver.const_name) | ||
|
||
hook_name = AUTOCORRECTABLE_CLASSES[receiver.const_name] | ||
preferred = "ActiveSupport.on_load(:#{hook_name}) { #{method} #{arguments.source} }" | ||
|
||
add_offense(node, message: format(MSG, preferred: preferred, source: node.source)) do |corrector| | ||
corrector.replace(node, preferred) | ||
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,114 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe(RuboCop::Cop::Rails::ActiveSupportOnLoad, :config) do | ||
it 'adds offense when trying to extend a framework class with include' do | ||
expect_offense(<<~RUBY) | ||
ActiveRecord::Base.include(MyClass) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't use \e[32mActiveRecord::Base.include(MyClass)\e[0m. Use this form instead \e[32mActiveSupport.on_load(:active_record) { include MyClass }\e[0m | ||
RUBY | ||
end | ||
|
||
it 'adds offense when trying to extend a framework class with prepend' do | ||
expect_offense(<<~RUBY) | ||
ActiveRecord::Base.prepend(MyClass) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't use \e[32mActiveRecord::Base.prepend(MyClass)\e[0m. Use this form instead \e[32mActiveSupport.on_load(:active_record) { prepend MyClass }\e[0m | ||
RUBY | ||
end | ||
|
||
it 'adds offense when trying to extend a framework class with extend' do | ||
expect_offense(<<~RUBY) | ||
ActiveRecord::Base.extend(MyClass) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't use \e[32mActiveRecord::Base.extend(MyClass)\e[0m. Use this form instead \e[32mActiveSupport.on_load(:active_record) { extend MyClass }\e[0m | ||
RUBY | ||
end | ||
|
||
it 'adds offense when trying to extend a framework class with absolute name' do | ||
expect_offense(<<~RUBY) | ||
::ActiveRecord::Base.extend(MyClass) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't use \e[32m::ActiveRecord::Base.extend(MyClass)\e[0m. Use this form instead \e[32mActiveSupport.on_load(:active_record) { extend MyClass }\e[0m | ||
RUBY | ||
end | ||
|
||
it 'adds offense when trying to extend a framework class with a variable' do | ||
expect_offense(<<~RUBY) | ||
ActiveRecord::Base.extend(my_class) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't use \e[32mActiveRecord::Base.extend(my_class)\e[0m. Use this form instead \e[32mActiveSupport.on_load(:active_record) { extend my_class }\e[0m | ||
RUBY | ||
end | ||
|
||
it 'does not add offense when extending a variable' do | ||
expect_no_offenses(<<~RUBY) | ||
foo.extend(MyClass) | ||
RUBY | ||
end | ||
|
||
it 'does not add offense when extending the framework using on_load and include' do | ||
expect_no_offenses(<<~RUBY) | ||
ActiveSupport.on_load(:active_record) { include MyClass } | ||
RUBY | ||
end | ||
|
||
it 'does not add offense when extending the framework using on_load and include in a multi-line block' do | ||
expect_no_offenses(<<~RUBY) | ||
ActiveSupport.on_load(:active_record) do | ||
include MyClass | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not add offense when not including a class' do | ||
expect_no_offenses(<<~RUBY) | ||
ActiveRecord::Base.include_root_in_json = false | ||
RUBY | ||
end | ||
|
||
it 'does not add offense when using include?' do | ||
expect_no_offenses(<<~RUBY) | ||
name.include?('bob') | ||
RUBY | ||
end | ||
|
||
context 'autocorrect' do | ||
it 'autocorrects extension on supported classes' do | ||
source = <<~RUBY | ||
ActiveRecord::Base.prepend(MyClass) | ||
RUBY | ||
|
||
corrected_source = <<~RUBY | ||
ActiveSupport.on_load(:active_record) { prepend MyClass } | ||
RUBY | ||
|
||
corrected = autocorrect_source(source) | ||
|
||
expect(corrected).to(eq(corrected_source)) | ||
end | ||
|
||
it 'does not autocorrect extension on unsupported classes' do | ||
source = <<~RUBY | ||
MyClass1.prepend(MyClass) | ||
RUBY | ||
|
||
corrected = autocorrect_source(source) | ||
|
||
expect(corrected).to(eq(source)) | ||
|
||
source = <<~RUBY | ||
MyClass1.include(MyClass) | ||
RUBY | ||
|
||
corrected = autocorrect_source(source) | ||
|
||
expect(corrected).to(eq(source)) | ||
end | ||
|
||
it 'does not autocorrect when there is no extensio on the supported classes' do | ||
source = <<~RUBY | ||
ActiveRecord::Base.include_root_in_json = false | ||
RUBY | ||
|
||
corrected = autocorrect_source(source) | ||
|
||
expect(corrected).to(eq(source)) | ||
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