-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add RuboCop cop to enforce no strict loading model or association config #69
Changes from all commits
98706ef
e2784ab
ce87479
ffeb167
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) | |
|
||
Gem::Specification.new do |s| | ||
s.name = "betterlint" | ||
s.version = "1.16.0" | ||
s.version = "1.17.0" | ||
s.authors = ["Development"] | ||
s.email = ["[email protected]"] | ||
s.summary = "Betterment rubocop configuration" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rubocop' | ||
require 'rubocop/cop/betterment/utils/parser' | ||
require 'rubocop/cop/betterment/utils/method_return_table' | ||
require 'rubocop/cop/betterment/utils/hardcoded_attribute' | ||
require 'rubocop/cop/betterment/active_job_performable' | ||
require 'rubocop/cop/betterment/allowlist_blocklist' | ||
require 'rubocop/cop/betterment/authorization_in_controller' | ||
require 'rubocop/cop/betterment/direct_delayed_enqueue' | ||
require 'rubocop/cop/betterment/dynamic_params' | ||
require 'rubocop/cop/betterment/unscoped_find' | ||
require 'rubocop/cop/betterment/unsafe_job' | ||
require 'rubocop/cop/betterment/timeout' | ||
require 'rubocop/cop/betterment/fetch_boolean' | ||
require 'rubocop/cop/betterment/hardcoded_id' | ||
require 'rubocop/cop/betterment/implicit_redirect_type' | ||
require 'rubocop/cop/betterment/internals_protection' | ||
require 'rubocop/cop/betterment/memoization_with_arguments' | ||
require 'rubocop/cop/betterment/non_standard_actions' | ||
require 'rubocop/cop/betterment/non_standard_controller' | ||
require 'rubocop/cop/betterment/redirect_status' | ||
require 'rubocop/cop/betterment/render_status' | ||
require 'rubocop/cop/betterment/server_error_assertion' | ||
require 'rubocop/cop/betterment/site_prism_loaded' | ||
require 'rubocop/cop/betterment/spec_helper_required_outside_spec_dir' | ||
require 'rubocop/cop/betterment/implicit_redirect_type' | ||
require 'rubocop/cop/betterment/active_job_performable' | ||
require 'rubocop/cop/betterment/allowlist_blocklist' | ||
require 'rubocop/cop/betterment/server_error_assertion' | ||
require 'rubocop/cop/betterment/hardcoded_id' | ||
require 'rubocop/cop/betterment/timeout' | ||
require 'rubocop/cop/betterment/unsafe_job' | ||
require 'rubocop/cop/betterment/unscoped_find' | ||
require 'rubocop/cop/betterment/use_global_strict_loading' | ||
require 'rubocop/cop/betterment/utils/hardcoded_attribute' | ||
require 'rubocop/cop/betterment/utils/method_return_table' | ||
require 'rubocop/cop/betterment/utils/parser' | ||
require 'rubocop/cop/betterment/vague_serialize' | ||
require 'rubocop/cop/betterment/fetch_boolean' | ||
require 'rubocop/cop/betterment/render_status' | ||
require 'rubocop/cop/betterment/redirect_status' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Betterment | ||
module UseGlobalStrictLoading | ||
# This cop ensures that `self.strict_loading_by_default = <any value>` is not set in ActiveRecord models. | ||
class ByDefaultForModels < Base | ||
extend AutoCorrector | ||
include RangeHelp | ||
|
||
MSG = 'Do not set `self.strict_loading_by_default` in ActiveRecord models.' | ||
|
||
# @!method strict_loading_by_default_set?(node) | ||
def_node_matcher :strict_loading_by_default_set?, <<~PATTERN | ||
$(send self :strict_loading_by_default= _) | ||
PATTERN | ||
|
||
def on_send(node) | ||
strict_loading_by_default_set?(node) do |method_call| | ||
add_offense(method_call) do |corrector| | ||
corrector.remove(method_call) | ||
end | ||
end | ||
end | ||
end | ||
|
||
# This cop ensures that `strict_loading: <any value>` is not set in ActiveRecord associations. | ||
class ForAssociations < Base | ||
extend AutoCorrector | ||
include RangeHelp | ||
|
||
MSG = 'Do not set `:strict_loading` in ActiveRecord associations.' | ||
|
||
ASSOCIATION_METHODS = %i(belongs_to has_and_belongs_to_many has_many has_one).freeze | ||
|
||
# @!method association_with_strict_loading?(node) | ||
def_node_matcher :association_with_strict_loading?, <<~PATTERN | ||
(send nil? {#{ASSOCIATION_METHODS.map(&:inspect).join(' ')}} ... (hash <$(pair (sym :strict_loading) ...) ...>)) | ||
PATTERN | ||
|
||
def on_send(node) | ||
association_with_strict_loading?(node) do |pair| | ||
add_offense(node) do |corrector| | ||
corrector.remove(range_with_surrounding_comma(range_with_surrounding_space(range: pair.source_range, side: :left), :left)) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe RuboCop::Cop::Betterment::UseGlobalStrictLoading::ByDefaultForModels, :config do | ||
context 'when `self.strict_loading_by_default` is set' do | ||
context 'when the class has no code before or after self.strict_loading_by_default' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
class MyModel < ApplicationRecord | ||
self.strict_loading_by_default = true | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not set `self.strict_loading_by_default` in ActiveRecord models. | ||
end | ||
RUBY | ||
|
||
# Add explicit spaces to prevent editor from stripping them on-save | ||
expect_correction(<<~RUBY) | ||
class MyModel < ApplicationRecord | ||
#{' '} | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when the class has a comment after self.strict_loading_by_default' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
class MyModel < ApplicationRecord | ||
self.strict_loading_by_default = true # Set to true to enable strict_loading_by_default | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not set `self.strict_loading_by_default` in ActiveRecord models. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
class MyModel < ApplicationRecord | ||
# Set to true to enable strict_loading_by_default | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when the class has code before self.strict_loading_by_default' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
class MyModel < ApplicationRecord | ||
self.table_name = :my_models | ||
self.strict_loading_by_default = true | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not set `self.strict_loading_by_default` in ActiveRecord models. | ||
end | ||
RUBY | ||
|
||
# Add explicit spaces to prevent editor from stripping them on-save | ||
expect_correction(<<~RUBY) | ||
class MyModel < ApplicationRecord | ||
self.table_name = :my_models | ||
#{' '} | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when the class has code after self.strict_loading_by_default' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
class MyModel < ApplicationRecord | ||
self.strict_loading_by_default = true | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not set `self.strict_loading_by_default` in ActiveRecord models. | ||
|
||
belongs_to :user | ||
end | ||
RUBY | ||
|
||
# Add explicit spaces to prevent editor from stripping them on-save | ||
expect_correction(<<~RUBY) | ||
class MyModel < ApplicationRecord | ||
#{' '} | ||
|
||
belongs_to :user | ||
end | ||
RUBY | ||
end | ||
end | ||
end | ||
|
||
it 'does not register an offense when `self.strict_loading_by_default` is not set' do | ||
expect_no_offenses(<<~RUBY) | ||
class MyModel < ApplicationRecord | ||
end | ||
RUBY | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe RuboCop::Cop::Betterment::UseGlobalStrictLoading::ForAssociations, :config do | ||
context 'when `strict_loading` is set in an association' do | ||
it 'registers an offense for belongs_to' do | ||
expect_offense(<<~RUBY) | ||
class MyModel < ApplicationRecord | ||
belongs_to :user, strict_loading: false # preserve this comment | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not set `:strict_loading` in ActiveRecord associations. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
class MyModel < ApplicationRecord | ||
belongs_to :user # preserve this comment | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for has_and_belongs_to_many' do | ||
expect_offense(<<~RUBY) | ||
class MyModel < ApplicationRecord | ||
has_and_belongs_to_many :tags, strict_loading: true # preserve this comment | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not set `:strict_loading` in ActiveRecord associations. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
class MyModel < ApplicationRecord | ||
has_and_belongs_to_many :tags # preserve this comment | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for has_many' do | ||
expect_offense(<<~RUBY) | ||
class MyModel < ApplicationRecord | ||
has_many :posts, strict_loading: true # preserve this comment | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not set `:strict_loading` in ActiveRecord associations. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
class MyModel < ApplicationRecord | ||
has_many :posts # preserve this comment | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for has_one' do | ||
expect_offense(<<~RUBY) | ||
class MyModel < ApplicationRecord | ||
has_one :account, strict_loading: true # preserve this comment | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not set `:strict_loading` in ActiveRecord associations. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
class MyModel < ApplicationRecord | ||
has_one :account # preserve this comment | ||
end | ||
RUBY | ||
end | ||
|
||
it 'preserves other options' do | ||
expect_offense(<<~RUBY) | ||
class MyModel < ApplicationRecord | ||
has_many :posts, strict_loading: true, dependent: :destroy # preserve this comment | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not set `:strict_loading` in ActiveRecord associations. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
class MyModel < ApplicationRecord | ||
has_many :posts, dependent: :destroy # preserve this comment | ||
end | ||
RUBY | ||
end | ||
|
||
it 'preserves multiline options' do | ||
expect_offense(<<~RUBY) | ||
class MyModel < ApplicationRecord | ||
has_many :posts, # preserve this comment | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not set `:strict_loading` in ActiveRecord associations. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comment: This was a bit annoying that I couldn't figure out how to make it also highlight the actual option on the next line. However, I feel it's probably "close enough" that anyone could figure out that the problem is associated with this specific For this cop to work the auto-correct is not strictly required; and I should note the autocorrect does work in this case, the problem is mostly wrt what parts are highlighted, which is a secondary concern. |
||
strict_loading: true, # preserve this comment | ||
dependent: :destroy # preserve this comment | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
class MyModel < ApplicationRecord | ||
has_many :posts, # preserve this comment, # preserve this comment | ||
dependent: :destroy # preserve this comment | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when `strict_loading` is not set in an association' do | ||
it 'does not register an offense' do | ||
expect_no_offenses(<<~RUBY) | ||
class MyModel < ApplicationRecord | ||
belongs_to :user # comment | ||
has_many :posts # comment | ||
has_and_belongs_to_many :tags # comment | ||
has_one :account # comment | ||
end | ||
RUBY | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment: This fixes a known issue with GitHub Actions.