-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RuboCop cop to enforce no strict loading model or association con…
…fig (#69) ### Summary - [x] [Fix GitHub Action to quote ruby 3.0 version so it's not evaluated as 3](98706ef) - [x] [Fix rubocop/cop/betterment require list to be sorted](e2784ab) - [x] [Add RuboCop::Cop::Betterment::UseGlobalStrictLoading cops](ce87479) - [x] [Upgrade betterlint to 1.17.0](ffeb167) ### Description This branch adds two cops to ensure models and associations don't have strict loading configuration.
- Loading branch information
Showing
10 changed files
with
305 additions
and
17 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 |
---|---|---|
|
@@ -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" | ||
|
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,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' |
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,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 |
91 changes: 91 additions & 0 deletions
91
spec/rubocop/cop/betterment/use_global_strict_loading/by_default_for_models_spec.rb
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,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 |
113 changes: 113 additions & 0 deletions
113
spec/rubocop/cop/betterment/use_global_strict_loading/for_associations_spec.rb
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,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. | ||
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 |