From e69d0ab2f2a36da749f0f5745d58af963874dcc3 Mon Sep 17 00:00:00 2001 From: Brian Hawley Date: Tue, 13 Dec 2022 08:51:11 -0800 Subject: [PATCH] Update rubocop to 1.40.0 - General bundle update. - Update the docs too. --- Gemfile | 2 +- Gemfile.lock | 26 +++++++------- .../contents/lint/assignment_in_condition.md | 5 +++ config/contents/lint/deprecated_constants.md | 9 ++++- config/contents/lint/interpolation_check.md | 7 ++-- config/contents/lint/void.md | 2 +- config/contents/metrics/block_length.md | 13 ++++--- config/contents/metrics/class_length.md | 13 ++++--- config/contents/metrics/method_length.md | 13 ++++--- config/contents/metrics/module_length.md | 13 ++++--- config/contents/style/array_intersect.md | 35 +++++++++++++++++++ config/contents/style/redundant_argument.md | 3 ++ .../contents/style/redundant_constant_base.md | 33 +++++++++++++++++ config/contents/style/require_order.md | 34 ++++++++++++++++++ config/contents/style/safe_navigation.md | 5 +++ config/contents/style/symbol_proc.md | 6 ++-- 16 files changed, 180 insertions(+), 39 deletions(-) create mode 100644 config/contents/style/array_intersect.md create mode 100644 config/contents/style/redundant_constant_base.md create mode 100644 config/contents/style/require_order.md diff --git a/Gemfile b/Gemfile index 86aa5831..a5c75326 100644 --- a/Gemfile +++ b/Gemfile @@ -5,7 +5,7 @@ source 'https://rubygems.org' gem "activesupport", require: false gem "parser" gem "pry", require: false -gem "rubocop", "1.39.0", require: false +gem "rubocop", "1.40.0", require: false gem "rubocop-i18n", require: false gem "rubocop-graphql", require: false gem "rubocop-minitest", require: false diff --git a/Gemfile.lock b/Gemfile.lock index 0102eb60..d30fcaf1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -12,19 +12,19 @@ GEM diff-lcs (1.5.0) i18n (1.12.0) concurrent-ruby (~> 1.0) - json (2.6.2) + json (2.6.3) method_source (1.0.0) minitest (5.16.3) parallel (1.22.1) - parser (3.1.2.1) + parser (3.1.3.0) ast (~> 2.4.1) pry (0.14.1) coderay (~> 1.1) method_source (~> 1.0) - rack (3.0.0) + rack (3.0.2) rainbow (3.1.1) rake (13.0.6) - regexp_parser (2.6.0) + regexp_parser (2.6.1) rexml (3.2.5) rspec (3.12.0) rspec-core (~> 3.12.0) @@ -35,11 +35,11 @@ GEM rspec-expectations (3.12.0) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.12.0) - rspec-mocks (3.12.0) + rspec-mocks (3.12.1) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.12.0) rspec-support (3.12.0) - rubocop (1.39.0) + rubocop (1.40.0) json (~> 2.3) parallel (~> 1.10) parser (>= 3.1.2.1) @@ -49,24 +49,24 @@ GEM rubocop-ast (>= 1.23.0, < 2.0) ruby-progressbar (~> 1.7) unicode-display_width (>= 1.4.0, < 3.0) - rubocop-ast (1.23.0) + rubocop-ast (1.24.0) parser (>= 3.1.1.0) rubocop-graphql (0.18.0) rubocop (>= 0.87, < 2) rubocop-i18n (3.0.0) rubocop (~> 1.0) - rubocop-minitest (0.23.2) + rubocop-minitest (0.25.0) rubocop (>= 0.90, < 2.0) - rubocop-performance (1.15.0) + rubocop-performance (1.15.1) rubocop (>= 1.7.0, < 2.0) rubocop-ast (>= 0.4.0) - rubocop-rails (2.17.2) + rubocop-rails (2.17.3) activesupport (>= 4.2.0) rack (>= 1.1) rubocop (>= 1.33.0, < 2.0) rubocop-rake (0.6.0) rubocop (~> 1.0) - rubocop-rspec (2.15.0) + rubocop-rspec (2.16.0) rubocop (~> 1.33) rubocop-sequel (0.3.4) rubocop (~> 1.0) @@ -77,7 +77,7 @@ GEM rubocop-thread_safety (0.4.4) rubocop (>= 0.53.0) ruby-progressbar (1.11.0) - test-prof (1.0.11) + test-prof (1.1.0) tzinfo (2.0.5) concurrent-ruby (~> 1.0) unicode-display_width (2.3.0) @@ -91,7 +91,7 @@ DEPENDENCIES pry rake rspec - rubocop (= 1.39.0) + rubocop (= 1.40.0) rubocop-graphql rubocop-i18n rubocop-minitest diff --git a/config/contents/lint/assignment_in_condition.md b/config/contents/lint/assignment_in_condition.md index 3955dc97..44a39ac5 100644 --- a/config/contents/lint/assignment_in_condition.md +++ b/config/contents/lint/assignment_in_condition.md @@ -6,6 +6,11 @@ By safe assignment we mean putting parentheses around an assignment to indicate "I know I'm using an assignment as a condition. It's not a mistake." +### Safety: + +This cop's autocorrection is unsafe because it assumes that +the author meant to use an assignment result as a condition. + ### Example: # bad if some_var = true diff --git a/config/contents/lint/deprecated_constants.md b/config/contents/lint/deprecated_constants.md index 36c4daa5..ad4afa87 100644 --- a/config/contents/lint/deprecated_constants.md +++ b/config/contents/lint/deprecated_constants.md @@ -9,7 +9,8 @@ alternative value as `Alternative`. And you can set the deprecated version as Alternative: 'alternative_value' DeprecatedVersion: 'deprecated_version' -By default, `NIL`, `TRUE`, `FALSE` and `Random::DEFAULT` are configured. +By default, `NIL`, `TRUE`, `FALSE`, `Net::HTTPServerException, `Random::DEFAULT`, +`Struct::Group`, and `Struct::Passwd` are configured. ### Example: @@ -17,10 +18,16 @@ By default, `NIL`, `TRUE`, `FALSE` and `Random::DEFAULT` are configured. NIL TRUE FALSE + Net::HTTPServerException Random::DEFAULT # Return value of Ruby 2 is `Random` instance, Ruby 3.0 is `Random` class. + Struct::Group + Struct::Passwd # good nil true false + Net::HTTPClientException Random.new # `::DEFAULT` has been deprecated in Ruby 3, `.new` is compatible with Ruby 2. + Etc::Group + Etc::Passwd diff --git a/config/contents/lint/interpolation_check.md b/config/contents/lint/interpolation_check.md index 4a31d0ff..a07ddfaf 100644 --- a/config/contents/lint/interpolation_check.md +++ b/config/contents/lint/interpolation_check.md @@ -2,9 +2,10 @@ Checks for interpolation in a single quoted string. ### Safety: -This cop is generally safe, but is marked as unsafe because -it is possible to actually intentionally have text inside -`#{...}` in a single quoted string. +This cop's autocorrection is unsafe because although it always replaces single quotes as +if it were miswritten double quotes, it is not always the case. For example, +`'#{foo} bar'` would be replaced by `"#{foo} bar"`, so the replaced code would evaluate +the expression `foo`. ### Example: diff --git a/config/contents/lint/void.md b/config/contents/lint/void.md index f1a7a22e..60fc0b84 100644 --- a/config/contents/lint/void.md +++ b/config/contents/lint/void.md @@ -1,4 +1,4 @@ -Checks for operators, variables, literals, and nonmutating +Checks for operators, variables, literals, lambda, proc and nonmutating methods used in void context. ### Example: CheckForMethodsWithNoSideEffects: false (default) diff --git a/config/contents/metrics/block_length.md b/config/contents/metrics/block_length.md index 493e3f90..8ec3bd2b 100644 --- a/config/contents/metrics/block_length.md +++ b/config/contents/metrics/block_length.md @@ -3,8 +3,8 @@ Comment lines can optionally be ignored. The maximum allowed length is configurable. The cop can be configured to ignore blocks passed to certain methods. -You can set literals you want to fold with `CountAsOne`. -Available are: 'array', 'hash', and 'heredoc'. Each literal +You can set constructs you want to fold with `CountAsOne`. +Available are: 'array', 'hash', 'heredoc', and 'method_call'. Each construct will be counted as one line regardless of its actual size. @@ -12,7 +12,7 @@ NOTE: The `ExcludedMethods` configuration is deprecated and only kept for backwards compatibility. Please use `AllowedMethods` and `AllowedPatterns` instead. By default, there are no methods to allowed. -### Example: CountAsOne: ['array', 'heredoc'] +### Example: CountAsOne: ['array', 'heredoc', 'method_call'] something do array = [ # +1 @@ -28,6 +28,11 @@ instead. By default, there are no methods to allowed. Heredoc content. HEREDOC - end # 5 points + + foo( # +1 + 1, + 2 + ) + end # 6 points NOTE: This cop does not apply for `Struct` definitions. \ No newline at end of file diff --git a/config/contents/metrics/class_length.md b/config/contents/metrics/class_length.md index 0bf736a3..45f232b6 100644 --- a/config/contents/metrics/class_length.md +++ b/config/contents/metrics/class_length.md @@ -2,11 +2,11 @@ Checks if the length a class exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable. -You can set literals you want to fold with `CountAsOne`. -Available are: 'array', 'hash', and 'heredoc'. Each literal +You can set constructs you want to fold with `CountAsOne`. +Available are: 'array', 'hash', 'heredoc', and 'method_call'. Each construct will be counted as one line regardless of its actual size. -### Example: CountAsOne: ['array', 'heredoc'] +### Example: CountAsOne: ['array', 'heredoc', 'method_call'] class Foo ARRAY = [ # +1 @@ -22,7 +22,12 @@ will be counted as one line regardless of its actual size. Heredoc content. HEREDOC - end # 5 points + + foo( # +1 + 1, + 2 + ) + end # 6 points NOTE: This cop also applies for `Struct` definitions. \ No newline at end of file diff --git a/config/contents/metrics/method_length.md b/config/contents/metrics/method_length.md index 0c6529f9..281adfee 100644 --- a/config/contents/metrics/method_length.md +++ b/config/contents/metrics/method_length.md @@ -2,8 +2,8 @@ Checks if the length of a method exceeds some maximum value. Comment lines can optionally be allowed. The maximum allowed length is configurable. -You can set literals you want to fold with `CountAsOne`. -Available are: 'array', 'hash', and 'heredoc'. Each literal +You can set constructs you want to fold with `CountAsOne`. +Available are: 'array', 'hash', 'heredoc', and 'method_call'. Each construct will be counted as one line regardless of its actual size. NOTE: The `ExcludedMethods` and `IgnoredMethods` configuration is @@ -11,7 +11,7 @@ deprecated and only kept for backwards compatibility. Please use `AllowedMethods` and `AllowedPatterns` instead. By default, there are no methods to allowed. -### Example: CountAsOne: ['array', 'heredoc'] +### Example: CountAsOne: ['array', 'heredoc', 'method_call'] def m array = [ # +1 @@ -27,4 +27,9 @@ By default, there are no methods to allowed. Heredoc content. HEREDOC - end # 5 points + + foo( # +1 + 1, + 2 + ) + end # 6 points diff --git a/config/contents/metrics/module_length.md b/config/contents/metrics/module_length.md index 58732eec..05e68fb7 100644 --- a/config/contents/metrics/module_length.md +++ b/config/contents/metrics/module_length.md @@ -2,11 +2,11 @@ Checks if the length a module exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable. -You can set literals you want to fold with `CountAsOne`. -Available are: 'array', 'hash', and 'heredoc'. Each literal +You can set constructs you want to fold with `CountAsOne`. +Available are: 'array', 'hash', 'heredoc', and 'method_call'. Each construct will be counted as one line regardless of its actual size. -### Example: CountAsOne: ['array', 'heredoc'] +### Example: CountAsOne: ['array', 'heredoc', 'method_call'] module M ARRAY = [ # +1 @@ -22,4 +22,9 @@ will be counted as one line regardless of its actual size. Heredoc content. HEREDOC - end # 5 points + + foo( # +1 + 1, + 2 + ) + end # 6 points diff --git a/config/contents/style/array_intersect.md b/config/contents/style/array_intersect.md new file mode 100644 index 00000000..38516889 --- /dev/null +++ b/config/contents/style/array_intersect.md @@ -0,0 +1,35 @@ +In Ruby 3.1, `Array#intersect?` has been added. + +This cop identifies places where `(array1 & array2).any?` +can be replaced by `array1.intersect?(array2)`. + +The `array1.intersect?(array2)` method is faster than +`(array1 & array2).any?` and is more readable. + +### Safety: + +This cop cannot guarantee that array1 and array2 are +actually arrays while method `intersect?` is for arrays only. + +### Example: + # bad + (array1 & array2).any? + (array1 & array2).empty? + + # good + array1.intersect?(array2) + !array1.intersect?(array2) + +### Example: AllCops:ActiveSupportExtensionsEnabled: false (default) + # good + (array1 & array2).present? + (array1 & array2).blank? + +### Example: AllCops:ActiveSupportExtensionsEnabled: true + # bad + (array1 & array2).present? + (array1 & array2).blank? + + # good + array1.intersect?(array2) + !array1.intersect?(array2) \ No newline at end of file diff --git a/config/contents/style/redundant_argument.md b/config/contents/style/redundant_argument.md index 125e11e1..f75c7c4e 100644 --- a/config/contents/style/redundant_argument.md +++ b/config/contents/style/redundant_argument.md @@ -7,6 +7,7 @@ Method names and their redundant arguments can be configured like this: ```yaml Methods: join: '' + sum: 0 split: ' ' chomp: "\n" chomp!: "\n" @@ -28,6 +29,7 @@ This cop is unsafe because of the following limitations: # bad array.join('') [1, 2, 3].join("") + array.sum(0) string.split(" ") "first\nsecond".split(" ") string.chomp("\n") @@ -37,6 +39,7 @@ This cop is unsafe because of the following limitations: # good array.join [1, 2, 3].join + array.sum string.split "first second".split string.chomp diff --git a/config/contents/style/redundant_constant_base.md b/config/contents/style/redundant_constant_base.md new file mode 100644 index 00000000..27b0c211 --- /dev/null +++ b/config/contents/style/redundant_constant_base.md @@ -0,0 +1,33 @@ +Avoid redundant `::` prefix on constant. + +How Ruby searches constant is a bit complicated, and it can often be difficult to +understand from the code whether the `::` is intended or not. Where `Module.nesting` +is empty, there is no need to prepend `::`, so it would be nice to consistently +avoid such meaningless `::` prefix to avoid confusion. + +### Example: + # bad + ::Const + + # good + Const + + # bad + class << self + ::Const + end + + # good + class << self + Const + end + + # good + class A + ::Const + end + + # good + module A + ::Const + end \ No newline at end of file diff --git a/config/contents/style/require_order.md b/config/contents/style/require_order.md new file mode 100644 index 00000000..f50af2a3 --- /dev/null +++ b/config/contents/style/require_order.md @@ -0,0 +1,34 @@ +Sort `require` and `require_relative` in alphabetical order. + +### Safety: + +This cop's autocorrection is unsafe because it will obviously change the execution order. + +### Example: + # bad + require 'b' + require 'a' + + # good + require 'a' + require 'b' + + # bad + require_relative 'b' + require_relative 'a' + + # good + require_relative 'a' + require_relative 'b' + + # good (sorted within each section separated by a blank line) + require 'a' + require 'd' + + require 'b' + require 'c' + + # good + require 'b' + require_relative 'c' + require 'a' \ No newline at end of file diff --git a/config/contents/style/safe_navigation.md b/config/contents/style/safe_navigation.md index 1ff0a25f..e8f50ad5 100644 --- a/config/contents/style/safe_navigation.md +++ b/config/contents/style/safe_navigation.md @@ -45,6 +45,11 @@ x&.foo # raises NoMethodError foo && foo.bar { |e| e.something } foo && foo.bar(param) { |e| e.something } + foo ? foo.bar : nil + foo.nil? ? nil : foo.bar + !foo.nil? ? foo.bar : nil + !foo ? nil : foo.bar + # good foo&.bar foo&.bar&.baz diff --git a/config/contents/style/symbol_proc.md b/config/contents/style/symbol_proc.md index 52e70076..d33f4fc9 100644 --- a/config/contents/style/symbol_proc.md +++ b/config/contents/style/symbol_proc.md @@ -2,7 +2,7 @@ Use symbols as procs when possible. If you prefer a style that allows block for method with arguments, please set `true` to `AllowMethodsWithArguments`. -respond_to , and `define_method?` methods are allowed by default. +`define_method?` methods are allowed by default. These are customizable with `AllowedMethods` option. ### Safety: @@ -67,12 +67,10 @@ call(&:bar) # some comment end -### Example: AllowedMethods: [respond_to, define_method] (default) +### Example: AllowedMethods: [define_method] (default) # good - respond_to { |foo| foo.bar } define_method(:foo) { |foo| foo.bar } - ### Example: AllowedPatterns: [] (default) # bad something.map { |s| s.upcase }