Skip to content

Commit

Permalink
Support ruby 3.1 (#189)
Browse files Browse the repository at this point in the history
**BREAKING**: Drop support for ruby 2.4

* Upgrade rubocop to version 1.22.0
* InvalidPredicateMatcher and CustomIncludeMethods are removed
* Add dependencies on `rubocop-rails` and `rubocop-performance`

Thanks @bmulholland and @RenzoMinelli
  • Loading branch information
RenzoMinelli authored Feb 10, 2023
1 parent 1939a2f commit 762cd62
Show file tree
Hide file tree
Showing 43 changed files with 834 additions and 1,258 deletions.
11 changes: 4 additions & 7 deletions .github/workflows/rspec_rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,16 @@ jobs:
strategy:
matrix:
include: # use bundler 2.3 for ruby versions < 2.6 (https://bundler.io/compatibility.html)
- ruby-version: 2.4
bundler-version: 2.3
- ruby-version: 2.5
bundler-version: 2.3
- ruby-version: 2.6
bundler-version: latest
- ruby-version: 2.7
bundler-version: latest
# TODO: reenable tests against ruby 3.0 after upgrading base Rubocop version above 1.22.0,
# thus correcting the syntax errors when running the Layout/BlockAlignment cop
# https://github.com/airbnb/ruby/pull/189#discussion_r1101793307
# - ruby-version: 3.0
# bundler-version: latest
- ruby-version: 3.0
bundler-version: latest
- ruby-version: 3.1
bundler-version: latest
steps:
- uses: actions/checkout@v3
- name: Set up Ruby
Expand Down
17 changes: 9 additions & 8 deletions rubocop-airbnb/config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ AllCops:
- 'vendor/**/*'
- Vagrantfile
- Guardfile
RSpec:
Patterns:
- _spec.rb
- "(?:^|/)spec/"
RSpec/FactoryBot:
Patterns:
- spec/factories/**/*.rb
- features/support/factories/**/*.rb

# While Rubocop has released a bunch of new cops, not all of these cops have been evaluated as
# part of this styleguide. To prevent new, unevaluated cops from imposing on this styleguide, we
Expand All @@ -33,6 +25,15 @@ AllCops:
# https://github.com/rubocop/rubocop/blob/1e55b1aa5e4c5eaeccad5d61f08b7930ed6bc341/config/default.yml#L89-L101
DisabledByDefault: true

RSpec:
Include:
- _spec.rb
- "(?:^|/)spec/"
RSpec/FactoryBot:
Include:
- spec/factories/**/*.rb
- features/support/factories/**/*.rb

inherit_from:
- './rubocop-airbnb.yml'
- './rubocop-bundler.yml'
Expand Down
3 changes: 3 additions & 0 deletions rubocop-airbnb/config/rubocop-performance.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require:
- rubocop-performance

Performance/Caller:
Enabled: false

Expand Down
3 changes: 3 additions & 0 deletions rubocop-airbnb/config/rubocop-rails.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require:
- rubocop-rails

# before_action doesn't seem to exist, so this doesn't make sense.
Rails/ActionFilter:
Enabled: false
Expand Down
13 changes: 4 additions & 9 deletions rubocop-airbnb/config/rubocop-rspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ RSpec/DescribedClass:
RSpec/EmptyExampleGroup:
Description: Checks if an example group does not include any tests.
Enabled: true
CustomIncludeMethods: []

RSpec/EmptyLineAfterExampleGroup:
Description: Checks if there is an empty line after example group blocks.
Expand Down Expand Up @@ -155,10 +154,6 @@ RSpec/InstanceVariable:
Description: 'Checks for the usage of instance variables.'
Enabled: false

RSpec/InvalidPredicateMatcher:
Description: Checks invalid usage for predicate matcher.
Enabled: false

RSpec/ItBehavesLike:
Description: Checks that only one `it_behaves_like` style is used.
Enabled: false
Expand Down Expand Up @@ -314,18 +309,18 @@ RSpec/VoidExpect:
Description: This cop checks void `expect()`.
Enabled: false

Capybara/CurrentPathExpectation:
RSpec/Capybara/CurrentPathExpectation:
Description: Checks that no expectations are set on Capybara's `current_path`.
Enabled: false

Capybara/FeatureMethods:
RSpec/Capybara/FeatureMethods:
Description: Checks for consistent method usage in feature specs.
Enabled: false

FactoryBot/AttributeDefinedStatically:
RSpec/FactoryBot/AttributeDefinedStatically:
Description: Always declare attribute values as blocks.
Enabled: false

FactoryBot/CreateList:
RSpec/FactoryBot/CreateList:
Description: Checks for create_list usage.
Enabled: true
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module Airbnb
# class Bar # nested class
# end
# end
class ClassOrModuleDeclaredInWrongFile < Cop
class ClassOrModuleDeclaredInWrongFile < Base
include Inflections
include RailsAutoloading

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module Airbnb
# module Foo
# BAZ = 42
# end
class ConstAssignedInWrongFile < Cop
class ConstAssignedInWrongFile < Base
include Inflections
include RailsAutoloading

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module RuboCop
module Cop
module Airbnb
class ContinuationSlash < Cop
class ContinuationSlash < Base
MSG = 'Slash continuation should be reserved for closed string continuation. ' \
'Many times it is used to get around other existing rules.'.freeze

Expand Down
2 changes: 1 addition & 1 deletion rubocop-airbnb/lib/rubocop/cop/airbnb/default_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Cop
module Airbnb
# Cop to help prevent the scorge of Default Scopes from ActiveRecord.
# Once in place they are almost impossible to remove.
class DefaultScope < Cop
class DefaultScope < Base
MSG = 'Avoid `default_scope`. Default scopes make it difficult to '\
'refactor data access patterns since the scope becomes part '\
'of every query unless explicitly excluded, even when it is '\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Airbnb
# Cop to enforce "attr { CONST }" instead of "attr CONST" in factories,
# because the latter forces autoload, which slows down spec startup time and
# Zeus reload time after touching a model.
class FactoryAttrReferencesClass < Cop
class FactoryAttrReferencesClass < Base
MSG = "Instead of attr_name MyClass::MY_CONST, use attr_name { MyClass::MY_CONST }. " \
"This enables faster spec startup time and Zeus reload time.".freeze

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Cop
module Airbnb
# Cop to tell developers to use :class => "MyClass" instead of :class => MyClass,
# because the latter slows down reloading zeus.
class FactoryClassUseString < Cop
class FactoryClassUseString < Base
MSG = 'Instead of :class => MyClass, use :class => "MyClass". ' \
"This enables faster spec startup time and faster Zeus reload time.".freeze

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Cop
module Airbnb
# Modifying Mass assignment restrictions eliminates the entire point of disabling
# mass assignment. It's a lazy, potentially dangerous approach that should be discouraged.
class MassAssignmentAccessibleModifier < Cop
class MassAssignmentAccessibleModifier < Base
MSG = 'Do no override and objects mass assignment restrictions.'.freeze

def on_send(node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module Airbnb
# end
# end
# end
class ModuleMethodInWrongFile < Cop
class ModuleMethodInWrongFile < Base
include Inflections
include RailsAutoloading

Expand Down
2 changes: 1 addition & 1 deletion rubocop-airbnb/lib/rubocop/cop/airbnb/no_timeout.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module RuboCop
module Cop
module Airbnb
class NoTimeout < Cop
class NoTimeout < Base
MSG =
'Do not use Timeout.timeout. In combination with Rails autoloading, ' \
'timeout can cause Segmentation Faults in version of Ruby we use. ' \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Cop
module Airbnb
# Cop to enforce use of options hash over default arguments
# https://github.com/airbnb/ruby#no-default-args
class OptArgParameters < Cop
class OptArgParameters < Base
MSG =
'Do not use default positional arguments. '\
'Use keyword arguments or an options hash instead.'.freeze
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module Airbnb
# ),
# }
# end
class PhraseBundleKeys < Cop
class PhraseBundleKeys < Base
MESSAGE =
'Phrase bundle keys should match their translation keys.'.freeze

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module RuboCop
module Cop
module Airbnb
# Disallow ActiveRecord calls that pass interpolated or added strings as an argument.
class RiskyActiverecordInvocation < Cop
class RiskyActiverecordInvocation < Base
VULNERABLE_AR_METHODS = [
:delete_all,
:destroy_all,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Airbnb
#
# describe Foo::Bar
# end
class RspecDescribeOrContextUnderNamespace < Cop
class RspecDescribeOrContextUnderNamespace < Base
DESCRIBE_OR_CONTEXT_UNDER_NAMESPACE_MSG =
'Declaring a `module` in a spec can break autoloading because subsequent references ' \
'to it will not cause it to be loaded from the app. This could cause flaky tests.'.freeze
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module Airbnb
# before(:each) do
# stub_env(:production)
# end
class RspecEnvironmentModification < Cop
class RspecEnvironmentModification < Base
def_node_matcher :allow_or_expect_rails_env, <<-PATTERN
(send (send nil? {:expect :allow} (send (const nil? :Rails) :env)) :to ...)
PATTERN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Cop
module Airbnb
# Cop to tackle prevent more complicated modifier if/unless statements
# https://github.com/airbnb/ruby#only-simple-if-unless
class SimpleModifierConditional < Cop
class SimpleModifierConditional < Base
MSG = 'Modifier if/unless usage is okay when the body is simple, ' \
'the condition is simple, and the whole thing fits on one line. ' \
'Otherwise, avoid modifier if/unless.'.freeze
Expand Down
2 changes: 1 addition & 1 deletion rubocop-airbnb/lib/rubocop/cop/airbnb/simple_unless.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Cop
module Airbnb
# Cop to tackle prevent unless statements with multiple conditions
# https://github.com/airbnb/ruby#unless-with-multiple-conditions
class SimpleUnless < Cop
class SimpleUnless < Base
MSG = 'Unless usage is okay when there is only one conditional'.freeze

def_node_matcher :multiple_conditionals?, '(if ({and or :^} ...) ...)'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@ module Airbnb
# describe Something do
# before { stub_const('MyClass::PAYLOAD', [1, 2, 3])
# end
class SpecConstantAssignment < Cop
include RuboCop::RSpec::TopLevelDescribe
class SpecConstantAssignment < Base
MESSAGE = "Defining constants inside of specs can cause spurious behavior. " \
"It is almost always preferable to use `let` statements, "\
"It is almost always preferable to use `let` statements, " \
"anonymous class/module definitions, or stub_const".freeze

def on_casgn(node)
return unless in_spec_file?(node)
parent_module_name = node.parent_module_name
if node.parent_module_name && parent_module_name != 'Object'
if parent_module_name && parent_module_name != "Object"
return
end
add_offense(node, message: MESSAGE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module RuboCop
module Cop
module Airbnb
# Disallow use of YAML/Marshal methods that can trigger RCE on untrusted input
class UnsafeYamlMarshal < Cop
class UnsafeYamlMarshal < Base
MSG = 'Using unsafe YAML parsing methods on untrusted input can lead ' \
'to remote code execution. Use `safe_load`, `parse`, `parse_file`, or ' \
'`parse_stream` instead'.freeze
Expand Down
6 changes: 3 additions & 3 deletions rubocop-airbnb/rubocop-airbnb.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Gem::Specification.new do |spec|
spec.license = 'MIT'
spec.version = RuboCop::Airbnb::VERSION
spec.platform = Gem::Platform::RUBY
spec.required_ruby_version = '>= 2.4'
spec.required_ruby_version = '>= 2.5'

spec.require_paths = ['lib']
spec.files = Dir[
Expand All @@ -25,9 +25,9 @@ Gem::Specification.new do |spec|
'Gemfile',
]

spec.add_dependency('rubocop', '~> 0.93.1')
spec.add_dependency('rubocop', '~> 1.22.0')
spec.add_dependency('rubocop-performance', '~> 1.10.2')
spec.add_dependency('rubocop-rails', '~> 2.9.1')
spec.add_dependency('rubocop-rspec', '~> 1.44.1')
spec.add_dependency('rubocop-rspec', '~> 2.0.0')
spec.add_development_dependency('rspec', '~> 3.5')
end
Loading

0 comments on commit 762cd62

Please sign in to comment.