diff --git a/.travis.yml b/.travis.yml index de8b8d1..3826c47 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,10 +3,10 @@ cache: bundler: true sudo: false rvm: - - 2.2 - 2.3 - 2.4 - 2.5 + - 2.6 install: - cd rubocop-airbnb - bundle install diff --git a/rubocop-airbnb/.rubocop.yml b/rubocop-airbnb/.rubocop.yml index e72544e..d3c7896 100644 --- a/rubocop-airbnb/.rubocop.yml +++ b/rubocop-airbnb/.rubocop.yml @@ -1,3 +1,6 @@ +require: + - rubocop-performance + - rubocop-rails inherit_from: - .rubocop_airbnb.yml - ./config/default.yml diff --git a/rubocop-airbnb/CHANGELOG.md b/rubocop-airbnb/CHANGELOG.md index f45b6cc..7d7fc95 100644 --- a/rubocop-airbnb/CHANGELOG.md +++ b/rubocop-airbnb/CHANGELOG.md @@ -1,3 +1,11 @@ +# 3.0.0 +* Update to rubocop 0.76 +* Enable Rails/IgnoredSkipActionFilterOption +* Enable Rails/ReflectionClassName +* Disable and delete Airbnb/ClassName +* Enable Layout/IndentFirstParameter +* Drop support for Ruby 2.2 + # 2.0.0 * Upgrade to rubocop-rspec 1.30.0, use ~> to allow for PATCH version flexibility * Upgrade to rubocop 0.58.0, use ~> to allow for PATCH version flexibility diff --git a/rubocop-airbnb/README.md b/rubocop-airbnb/README.md index d9b3be1..9a3cbe5 100644 --- a/rubocop-airbnb/README.md +++ b/rubocop-airbnb/README.md @@ -15,6 +15,11 @@ Just put this in your `Gemfile` it depends on the appropriate version of rubocop gem 'rubocop-airbnb' ``` +Note: If you want to run with Ruby 2.2 you will need to set your version to 2 +``` +gem 'rubocop-airbnb', '~> 2' +``` + ## Usage You need to tell RuboCop to load the Airbnb extension. There are three diff --git a/rubocop-airbnb/config/rubocop-airbnb.yml b/rubocop-airbnb/config/rubocop-airbnb.yml index 01d342e..dbe7170 100644 --- a/rubocop-airbnb/config/rubocop-airbnb.yml +++ b/rubocop-airbnb/config/rubocop-airbnb.yml @@ -2,11 +2,6 @@ # They are custom built for use inside Airbnb and address issues that we have experienced in # testing and production. -Airbnb/ClassName: - Description: Use :class_name => "Model" instead of :class_name => Model.name - to avoid a long cascade of autoloading. - Enabled: true - Airbnb/ClassOrModuleDeclaredInWrongFile: Description: Declare a class or module in the file that matches its namespace and name. Enabled: true diff --git a/rubocop-airbnb/config/rubocop-gemspec.yml b/rubocop-airbnb/config/rubocop-gemspec.yml index 631a131..17cb6d8 100644 --- a/rubocop-airbnb/config/rubocop-gemspec.yml +++ b/rubocop-airbnb/config/rubocop-gemspec.yml @@ -7,3 +7,6 @@ Gemspec/RequiredRubyVersion: Enabled: false Include: - '**/*.gemspec' + +Gemspec/RubyVersionGlobalsUsage: + Enabled: true diff --git a/rubocop-airbnb/config/rubocop-layout.yml b/rubocop-airbnb/config/rubocop-layout.yml index cb6eb34..c41c16a 100644 --- a/rubocop-airbnb/config/rubocop-layout.yml +++ b/rubocop-airbnb/config/rubocop-layout.yml @@ -227,7 +227,7 @@ Layout/FirstMethodParameterLineBreak: Enabled: false # Supports --auto-correct -Layout/FirstParameterIndentation: +Layout/IndentFirstArgument: Description: Checks the indentation of the first parameter in a method call. Enabled: true EnforcedStyle: consistent @@ -238,7 +238,7 @@ Layout/FirstParameterIndentation: - special_for_inner_method_call_in_parentheses # Supports --auto-correct -Layout/IndentArray: +Layout/IndentFirstArrayElement: Description: Checks the indentation of the first element in an array literal. Enabled: true EnforcedStyle: consistent @@ -248,10 +248,9 @@ Layout/IndentAssignment: Description: Checks the indentation of the first line of the right-hand-side of a multi-line assignment. Enabled: true - IndentationWidth: # Supports --auto-correct -Layout/IndentHash: +Layout/IndentFirstHashElement: Description: Checks the indentation of the first key in a hash literal. Enabled: true EnforcedStyle: consistent @@ -344,7 +343,6 @@ Layout/MultilineMethodCallIndentation: SupportedStyles: - aligned - indented - IndentationWidth: Layout/MultilineMethodDefinitionBraceLayout: Description: >- @@ -474,11 +472,6 @@ Layout/SpaceInsideBlockBraces: EnforcedStyleForEmptyBraces: no_space SpaceBeforeBlockParameters: true -Layout/SpaceInsideParens: - Description: 'No spaces after ( or before ).' - StyleGuide: '#spaces-braces' - Enabled: true - Layout/SpaceInsideArrayLiteralBrackets: EnforcedStyle: no_space SupportedStyles: @@ -558,3 +551,6 @@ Layout/TrailingWhitespace: Description: Avoid trailing whitespace. StyleGuide: https://github.com/rubocop-hq/ruby-style-guide#no-trailing-whitespace Enabled: true + +Layout/IndentFirstParameter: + Enabled: true diff --git a/rubocop-airbnb/config/rubocop-lint.yml b/rubocop-airbnb/config/rubocop-lint.yml index 30863fe..db48dd6 100644 --- a/rubocop-airbnb/config/rubocop-lint.yml +++ b/rubocop-airbnb/config/rubocop-lint.yml @@ -80,6 +80,11 @@ Lint/EnsureReturn: Lint/ErbNewArguments: Enabled: false +Lint/FlipFlop: + Description: Checks for flip flops + StyleGuide: https://github.com/rubocop-hq/ruby-style-guide#no-flip-flops + Enabled: false + Lint/FloatOutOfRange: Description: Catches floating-point literals too large or small for Ruby to represent. Enabled: false @@ -237,20 +242,20 @@ Lint/UnderscorePrefixedVariableName: Lint/UnifiedInteger: Enabled: false -Lint/UnneededCopDisableDirective: +Lint/RedundantCopDisableDirective: Description: >- Checks for rubocop:disable comments that can be removed. Note: this cop is not disabled when disabling all cops. It must be explicitly disabled. Enabled: true -Lint/UnneededCopEnableDirective: +Lint/RedundantCopEnableDirective: Description: Checks for rubocop:enable comments that can be removed. Enabled: true -Lint/UnneededRequireStatement: +Lint/RedundantRequireStatement: Enabled: false -Lint/UnneededSplatExpansion: +Lint/RedundantSplatExpansion: Enabled: false Lint/UnreachableCode: diff --git a/rubocop-airbnb/config/rubocop-naming.yml b/rubocop-airbnb/config/rubocop-naming.yml index b447018..937cace 100644 --- a/rubocop-airbnb/config/rubocop-naming.yml +++ b/rubocop-airbnb/config/rubocop-naming.yml @@ -83,3 +83,6 @@ Naming/VariableName: Naming/VariableNumber: Enabled: false + +Naming/RescuedExceptionsVariableName: + Enabled: false diff --git a/rubocop-airbnb/config/rubocop-performance.yml b/rubocop-airbnb/config/rubocop-performance.yml index c372ab5..067f3ba 100644 --- a/rubocop-airbnb/config/rubocop-performance.yml +++ b/rubocop-airbnb/config/rubocop-performance.yml @@ -56,11 +56,6 @@ Performance/FlatMap: Performance/InefficientHashSearch: Enabled: false -# Supports --auto-correct -Performance/LstripRstrip: - Description: Use `strip` instead of `lstrip.rstrip`. - Enabled: false - # Supports --auto-correct Performance/RangeInclude: Description: Use `Range#cover?` instead of `Range#include?`. @@ -85,11 +80,6 @@ Performance/RedundantMerge: Reference: https://github.com/JuanitoFatas/fast-ruby#hashmerge-vs-hash-code Enabled: false -# Supports --auto-correct -Performance/RedundantSortBy: - Description: Use `sort` instead of `sort_by { |x| x }`. - Enabled: false - Performance/RegexpMatch: Enabled: false @@ -99,12 +89,6 @@ Performance/ReverseEach: Reference: https://github.com/JuanitoFatas/fast-ruby#enumerablereverseeach-vs-enumerablereverse_each-code Enabled: false -# Supports --auto-correct -Performance/Sample: - Description: Use `sample` instead of `shuffle.first`, `shuffle.last`, and `shuffle[Fixnum]`. - Reference: https://github.com/JuanitoFatas/fast-ruby#arrayshufflefirst-vs-arraysample-code - Enabled: false - # Supports --auto-correct Performance/Size: Description: Use `size` instead of `count` for counting the number of elements in @@ -133,9 +117,6 @@ Performance/TimesMap: Performance/UnfreezeString: Enabled: false -Performance/UnneededSort: - Enabled: false - Performance/UriDefaultParser: Enabled: false diff --git a/rubocop-airbnb/config/rubocop-rails.yml b/rubocop-airbnb/config/rubocop-rails.yml index 1ba76e8..d19e331 100644 --- a/rubocop-airbnb/config/rubocop-rails.yml +++ b/rubocop-airbnb/config/rubocop-rails.yml @@ -206,3 +206,9 @@ Rails/Validation: Enabled: false Include: - app/models/**/*.rb + +Rails/IgnoredSkipActionFilterOption: + Enabled: true + +Rails/ReflectionClassName: + Enabled: true diff --git a/rubocop-airbnb/config/rubocop-style.yml b/rubocop-airbnb/config/rubocop-style.yml index 43f0844..c1f8a9c 100644 --- a/rubocop-airbnb/config/rubocop-style.yml +++ b/rubocop-airbnb/config/rubocop-style.yml @@ -328,11 +328,6 @@ Style/ExpandPathArguments: Description: "Use `expand_path(__dir__)` instead of `expand_path('..', __FILE__)`." Enabled: false -Style/FlipFlop: - Description: Checks for flip flops - StyleGuide: https://github.com/rubocop-hq/ruby-style-guide#no-flip-flops - Enabled: false - Style/For: Description: Checks use of for or each in multiline loops. StyleGuide: https://github.com/rubocop-hq/ruby-style-guide#no-for-loops @@ -360,10 +355,10 @@ Style/FrozenStringLiteralComment: Description: Add the frozen_string_literal comment to the top of files to help transition from Ruby 2.3.0 to Ruby 3.0. Enabled: false - EnforcedStyle: when_needed SupportedStyles: - - when_needed - always + - never + EnforcedStyle: always Style/GlobalVars: Description: Do not introduce global variables. @@ -735,6 +730,11 @@ Style/RedundantSelf: StyleGuide: https://github.com/rubocop-hq/ruby-style-guide#no-self-unless-required Enabled: true +# Supports --auto-correct +Style/RedundantSortBy: + Description: Use `sort` instead of `sort_by { |x| x }`. + Enabled: false + # Supports --auto-correct Style/RegexpLiteral: Description: Use / or %r around regular expressions. @@ -763,6 +763,12 @@ Style/ReturnNil: Style/SafeNavigation: Enabled: false +# Supports --auto-correct +Style/Sample: + Description: Use `sample` instead of `shuffle.first`, `shuffle.last`, and `shuffle[Fixnum]`. + Reference: https://github.com/JuanitoFatas/fast-ruby#arrayshufflefirst-vs-arraysample-code + Enabled: false + # Supports --auto-correct Style/SelfAssignment: Description: Checks for places where self-assignment shorthand should have been used. @@ -856,6 +862,11 @@ Style/StringMethods: PreferredMethods: intern: to_sym +# Supports --auto-correct +Style/Strip: + Description: Use `strip` instead of `lstrip.rstrip`. + Enabled: false + Style/StructInheritance: Description: Checks for inheritance from Struct.new. StyleGuide: https://github.com/rubocop-hq/ruby-style-guide#no-extend-struct-new @@ -922,24 +933,27 @@ Style/UnlessElse: Enabled: true # Supports --auto-correct -Style/UnneededCapitalW: +Style/RedundantCapitalW: Description: Checks for %W when interpolation is not needed. Enabled: false -Style/UnneededCondition: +Style/RedundantCondition: Enabled: false # Supports --auto-correct -Style/UnneededInterpolation: +Style/RedundantInterpolation: Description: Checks for strings that are just an interpolated expression. Enabled: false # Supports --auto-correct -Style/UnneededPercentQ: +Style/RedundantPercentQ: Description: Checks for %q/%Q when single quotes or double quotes would do. StyleGuide: https://github.com/rubocop-hq/ruby-style-guide#percent-q Enabled: false +Style/RedundantSort: + Enabled: false + Style/UnpackFirst: Enabled: false diff --git a/rubocop-airbnb/lib/rubocop/airbnb/version.rb b/rubocop-airbnb/lib/rubocop/airbnb/version.rb index 33db4c3..30c7ca6 100644 --- a/rubocop-airbnb/lib/rubocop/airbnb/version.rb +++ b/rubocop-airbnb/lib/rubocop/airbnb/version.rb @@ -3,6 +3,6 @@ module RuboCop module Airbnb # Version information for the the Airbnb RuboCop plugin. - VERSION = '2.0.0'.freeze + VERSION = '3.0.0' end end diff --git a/rubocop-airbnb/lib/rubocop/cop/airbnb/class_name.rb b/rubocop-airbnb/lib/rubocop/cop/airbnb/class_name.rb deleted file mode 100644 index bf2dd65..0000000 --- a/rubocop-airbnb/lib/rubocop/cop/airbnb/class_name.rb +++ /dev/null @@ -1,47 +0,0 @@ -module RuboCop - module Cop - module Airbnb - # Cop to prevent cross-model references, which result in a cascade of autoloads. E.g., - # belongs_to :user, :class_name => User.name - class ClassName < Cop - MSG = 'Use "Model" instead of Model.name at class scope to avoid cross-model references. ' \ - 'They cause a long cascade of autoloading, slowing down app startup and slowing down ' \ - 'reloading of zeus after changing a model.'.freeze - - # Is this a has_many, has_one, or belongs_to with a :class_name arg? Make sure the - # class name is a hardcoded string. If not, add an offense and return true. - def on_send(node) - association_statement = - node.command?(:has_many) || - node.command?(:has_one) || - node.command?(:belongs_to) - - return unless association_statement - - class_pair = class_name_node(node) - - if class_pair && !string_class_name?(class_pair) - add_offense(class_pair) - end - end - - private - - # Return the descendant node that is a hash pair (:key => value) whose key - # is :class_name. - def class_name_node(node) - node.descendants.detect do |e| - e.is_a?(Parser::AST::Node) && - e.pair_type? && - e.children[0].children[0] == :class_name - end - end - - # Given a hash pair :class_name => value, is the value a hardcoded string? - def string_class_name?(class_pair) - class_pair.children[1].str_type? - end - end - end - end -end diff --git a/rubocop-airbnb/lib/rubocop/cop/airbnb/continuation_slash.rb b/rubocop-airbnb/lib/rubocop/cop/airbnb/continuation_slash.rb index 2d7ae1e..e5fdd85 100644 --- a/rubocop-airbnb/lib/rubocop/cop/airbnb/continuation_slash.rb +++ b/rubocop-airbnb/lib/rubocop/cop/airbnb/continuation_slash.rb @@ -14,7 +14,7 @@ def enforce_violation(node) alias on_send enforce_violation alias on_if enforce_violation - Util::ASGN_NODES.each do |type| + RuboCop::AST::Node::ASSIGNMENTS.each do |type| define_method("on_#{type}") do |node| enforce_violation(node) end diff --git a/rubocop-airbnb/rubocop-airbnb.gemspec b/rubocop-airbnb/rubocop-airbnb.gemspec index 569c8aa..9a4b2c3 100644 --- a/rubocop-airbnb/rubocop-airbnb.gemspec +++ b/rubocop-airbnb/rubocop-airbnb.gemspec @@ -25,7 +25,9 @@ Gem::Specification.new do |spec| 'Gemfile', ] - spec.add_dependency('rubocop', '~> 0.58.0') + spec.add_dependency('rubocop', '~> 0.76.0') + spec.add_dependency('rubocop-performance', '~> 1.5.0') + spec.add_dependency('rubocop-rails', '~> 2.3.2') spec.add_dependency('rubocop-rspec', '~> 1.30.0') spec.add_development_dependency('rspec', '~> 3.5') end diff --git a/rubocop-airbnb/spec/rubocop/cop/airbnb/class_name_spec.rb b/rubocop-airbnb/spec/rubocop/cop/airbnb/class_name_spec.rb deleted file mode 100644 index 789ddea..0000000 --- a/rubocop-airbnb/spec/rubocop/cop/airbnb/class_name_spec.rb +++ /dev/null @@ -1,78 +0,0 @@ -describe RuboCop::Cop::Airbnb::ClassName do - subject(:cop) { described_class.new } - - describe "belongs_to" do - it 'rejects with Model.name' do - source = [ - 'class Coupon', - ' belongs_to :user, :class_name => User.name', - 'end', - ].join("\n") - inspect_source(source) - - expect(cop.offenses.size).to eq(1) - expect(cop.offenses.map(&:line).sort).to eq([2]) - end - - it 'passes with "Model"' do - source = [ - 'class Coupon', - ' belongs_to :user, :class_name => "User"', - 'end', - ].join("\n") - inspect_source(source) - - expect(cop.offenses).to be_empty - end - end - - describe "has_many" do - it 'rejects with Model.name' do - source = [ - 'class Coupon', - ' has_many :reservations, :class_name => Reservation2.name', - 'end', - ].join("\n") - inspect_source(source) - - expect(cop.offenses.size).to eq(1) - expect(cop.offenses.map(&:line)).to eq([2]) - end - - it 'passes with "Model"' do - source = [ - 'class Coupon', - ' has_many :reservations, :class_name => "Reservation2"', - 'end', - ].join("\n") - inspect_source(source) - - expect(cop.offenses).to be_empty - end - end - - describe "has_one" do - it 'rejects with Model.name' do - source = [ - 'class Coupon', - ' has_one :loss, :class_name => Payments::Loss.name', - 'end', - ].join("\n") - inspect_source(source) - - expect(cop.offenses.size).to eq(1) - expect(cop.offenses.map(&:line)).to eq([2]) - end - - it 'passes with "Model"' do - source = [ - 'class Coupon', - ' has_one :loss, :class_name => "Payments::Loss"', - 'end', - ].join("\n") - inspect_source(source) - - expect(cop.offenses).to be_empty - end - end -end