From fa6662f0873933fa454b9b28debdfd2e9bb95af9 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 25 Feb 2023 17:53:01 +0900 Subject: [PATCH] Cut 2.18.0 --- CHANGELOG.md | 2 + config/default.yml | 18 +-- docs/antora.yml | 2 +- docs/modules/ROOT/pages/cops.adoc | 1 + docs/modules/ROOT/pages/cops_rails.adoc | 169 ++++++++++++++++++------ lib/rubocop/rails/version.rb | 2 +- relnotes/v2.18.0.md | 34 +++++ 7 files changed, 174 insertions(+), 54 deletions(-) create mode 100644 relnotes/v2.18.0.md diff --git a/CHANGELOG.md b/CHANGELOG.md index d66fe0d53a..317fd3ddd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ ## master (unreleased) +## 2.18.0 (2023-02-25) + ### New features * [#863](https://github.com/rubocop/rubocop-rails/pull/863): Add `Rails/ResponseParsedBody` cop. ([@r7kamura][]) diff --git a/config/default.yml b/config/default.yml index 8e11dd09a8..1e609a14f3 100644 --- a/config/default.yml +++ b/config/default.yml @@ -120,7 +120,7 @@ Rails/ActiveRecordOverride: Enabled: true Severity: warning VersionAdded: '0.67' - VersionChanged: '<>' + VersionChanged: '2.18' Include: - app/models/**/*.rb @@ -324,7 +324,7 @@ Rails/DeprecatedActiveModelErrorsMethods: Severity: warning Safe: false VersionAdded: '2.14' - VersionChanged: '<>' + VersionChanged: '2.18' Rails/DotSeparatedKeys: Description: 'Enforces the use of dot-separated keys instead of `:scope` options in `I18n` translation methods.' @@ -337,14 +337,14 @@ Rails/DuplicateAssociation: Enabled: pending Severity: warning VersionAdded: '2.14' - VersionChanged: '<>' + VersionChanged: '2.18' Rails/DuplicateScope: Description: 'Multiple scopes share this same where clause.' Enabled: pending Severity: warning VersionAdded: '2.14' - VersionChanged: '<>' + VersionChanged: '2.18' Rails/DurationArithmetic: Description: 'Do not use duration as arithmetic operand with `Time.current`.' @@ -689,7 +689,7 @@ Rails/Pluck: Enabled: 'pending' Safe: false VersionAdded: '2.7' - VersionChanged: '<>' + VersionChanged: '2.18' Rails/PluckId: Description: 'Use `ids` instead of `pluck(:id)` or `pluck(primary_key)`.' @@ -845,7 +845,7 @@ Rails/ResponseParsedBody: Description: Prefer `response.parsed_body` to `JSON.parse(response.body)`. Enabled: pending SafeAutoCorrect: false - VersionAdded: "<>" + VersionAdded: '2.18' Include: - spec/controllers/**/*.rb - spec/requests/**/*.rb @@ -1045,7 +1045,7 @@ Rails/TopLevelHashWithIndifferentAccess: Enabled: pending Severity: warning VersionAdded: '2.16' - VersionChanged: '<>' + VersionChanged: '2.18' Rails/TransactionExitStatement: Description: 'Avoid the usage of `return`, `break` and `throw` in transaction blocks.' @@ -1077,7 +1077,7 @@ Rails/UnknownEnv: Enabled: true Severity: warning VersionAdded: '0.51' - VersionChanged: '<>' + VersionChanged: '2.18' Environments: - development - test @@ -1134,7 +1134,7 @@ Rails/WhereNotWithMultipleConditions: Enabled: 'pending' Severity: warning VersionAdded: '2.17' - VersionChanged: '<>' + VersionChanged: '2.18' # Accept `redirect_to(...) and return` and similar cases. Style/AndOr: diff --git a/docs/antora.yml b/docs/antora.yml index 9e0ff48acb..ece73dc0ec 100644 --- a/docs/antora.yml +++ b/docs/antora.yml @@ -2,6 +2,6 @@ name: rubocop-rails title: RuboCop Rails # We always provide version without patch here (e.g. 1.1), # as patch versions should not appear in the docs. -version: ~ +version: '2.18' nav: - modules/ROOT/nav.adoc diff --git a/docs/modules/ROOT/pages/cops.adoc b/docs/modules/ROOT/pages/cops.adoc index c1a6a2a247..2775221eac 100644 --- a/docs/modules/ROOT/pages/cops.adoc +++ b/docs/modules/ROOT/pages/cops.adoc @@ -107,6 +107,7 @@ based on the https://rails.rubystyle.guide/[Rails Style Guide]. * xref:cops_rails.adoc#railsrenderplaintext[Rails/RenderPlainText] * xref:cops_rails.adoc#railsrequestreferer[Rails/RequestReferer] * xref:cops_rails.adoc#railsrequiredependency[Rails/RequireDependency] +* xref:cops_rails.adoc#railsresponseparsedbody[Rails/ResponseParsedBody] * xref:cops_rails.adoc#railsreversiblemigration[Rails/ReversibleMigration] * xref:cops_rails.adoc#railsreversiblemigrationmethoddefinition[Rails/ReversibleMigrationMethodDefinition] * xref:cops_rails.adoc#railsrootjoinchain[Rails/RootJoinChain] diff --git a/docs/modules/ROOT/pages/cops_rails.adoc b/docs/modules/ROOT/pages/cops_rails.adoc index ae60d4dda3..a5700b45a8 100644 --- a/docs/modules/ROOT/pages/cops_rails.adoc +++ b/docs/modules/ROOT/pages/cops_rails.adoc @@ -304,7 +304,7 @@ end | Yes | No | 0.67 -| - +| 2.18 |=== Checks for overriding built-in Active Record methods instead of using @@ -337,6 +337,10 @@ end |=== | Name | Default value | Configurable values +| Severity +| `warning` +| String + | Include | `+app/models/**/*.rb+` | Array @@ -1466,7 +1470,7 @@ delegate :foo, to: :bar, allow_nil: true | No | Yes (Unsafe) | 2.14 -| 2.15 +| 2.18 |=== Checks direct manipulation of ActiveModel#errors as hash. @@ -1503,6 +1507,16 @@ user.errors.keys.include?(:attr) user.errors.attribute_names.include?(:attr) ---- +=== Configurable attributes + +|=== +| Name | Default value | Configurable values + +| Severity +| `warning` +| String +|=== + == Rails/DotSeparatedKeys |=== @@ -1545,7 +1559,7 @@ I18n.t :record_invalid, scope: 'activerecord.errors.messages' | Yes | Yes | 2.14 -| - +| 2.18 |=== Looks for associations that have been defined multiple times in the same file. @@ -1568,6 +1582,16 @@ belongs_to :bar has_one :foo ---- +=== Configurable attributes + +|=== +| Name | Default value | Configurable values + +| Severity +| `warning` +| String +|=== + == Rails/DuplicateScope |=== @@ -1577,7 +1601,7 @@ has_one :foo | Yes | No | 2.14 -| - +| 2.18 |=== Checks for multiple scopes in a model that have the same `where` clause. This @@ -1596,6 +1620,16 @@ scope :visible, -> { where(visible: true) } scope :hidden, -> { where(visible: false) } ---- +=== Configurable attributes + +|=== +| Name | Default value | Configurable values + +| Severity +| `warning` +| String +|=== + == Rails/DurationArithmetic |=== @@ -2251,10 +2285,6 @@ User.order(:foo).each | AllowedPatterns | `[]` | Array - -| IgnoredMethods -| `order`, `limit`, `select`, `lock` -| Array |=== === References @@ -2854,7 +2884,6 @@ end === References -* https://api.rubyonrails.org/classes/AbstractController/Callbacks/ClassMethods.html#method-i-_normalize_callback_options * https://api.rubyonrails.org/classes/AbstractController/Callbacks/ClassMethods.html#method-i-_normalize_callback_options == Rails/IndexBy @@ -3722,10 +3751,10 @@ Model.pick(:a) | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed | Pending -| Yes -| Yes +| No +| Yes (Unsafe) | 2.7 -| - +| 2.18 |=== Enforces the use of `pluck` over `map`. @@ -3734,6 +3763,19 @@ Enforces the use of `pluck` over `map`. element in an enumerable. When called on an Active Record relation, it results in a more efficient query that only selects the necessary key. +=== Safety + +This cop is unsafe because model can use column aliases. + +[source,ruby] +---- +# Original code +User.select('name AS nickname').map { |user| user[:nickname] } # => array of nicknames + +# After autocorrection +User.select('name AS nickname').pluck(:nickname) # => raises ActiveRecord::StatementInvalid +---- + === Examples [source,ruby] @@ -4722,6 +4764,47 @@ require_dependency 'some_lib' * https://guides.rubyonrails.org/autoloading_and_reloading_constants.html +== Rails/ResponseParsedBody + +|=== +| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed + +| Pending +| Yes +| Yes (Unsafe) +| 2.18 +| - +|=== + +Prefer `response.parsed_body` to `JSON.parse(response.body)`. + +=== Safety + +This cop's autocorrection is unsafe because Content-Type may not be `application/json`. For example, the +proprietary Content-Type provided by corporate entities such as `application/vnd.github+json` is not +supported at `response.parsed_body` by default, so you still have to use `JSON.parse(response.body)` there. + +=== Examples + +[source,ruby] +---- +# bad +JSON.parse(response.body) + +# good +response.parsed_body +---- + +=== Configurable attributes + +|=== +| Name | Default value | Configurable values + +| Include +| `+spec/controllers/**/*.rb+`, `+spec/requests/**/*.rb+`, `+test/controllers/**/*.rb+`, `+test/integration/**/*.rb+` +| Array +|=== + == Rails/ReversibleMigration |=== @@ -4750,23 +4833,15 @@ end # good def change - create_table :users do |t| - t.string :name + change_table :users do |t| + t.remove :name, :string end end # good def change - reversible do |dir| - change_table :users do |t| - dir.up do - t.column :name, :string - end - - dir.down do - t.remove :name - end - end + create_table :users do |t| + t.string :name end end ---- @@ -4857,21 +4932,6 @@ def change t.string :name end end - -# good -def change - reversible do |dir| - change_table :users do |t| - dir.up do - t.change :price, :string - end - - dir.down do - t.change :price, :integer - end - end - end -end ---- [source,ruby] @@ -4937,7 +4997,6 @@ end * https://rails.rubystyle.guide#reversible-migration * https://api.rubyonrails.org/classes/ActiveRecord/Migration/CommandRecorder.html -* https://api.rubyonrails.org/classes/ActiveRecord/Migration/CommandRecorder.html == Rails/ReversibleMigrationMethodDefinition @@ -5960,7 +6019,7 @@ obj.to_formatted_s(:delimited) | Yes | Yes | 2.16 -| - +| 2.18 |=== Identifies top-level `HashWithIndifferentAccess`. @@ -5977,6 +6036,16 @@ HashWithIndifferentAccess.new(foo: 'bar') ActiveSupport::HashWithIndifferentAccess.new(foo: 'bar') ---- +=== Configurable attributes + +|=== +| Name | Default value | Configurable values + +| Severity +| `warning` +| String +|=== + === References * https://guides.rubyonrails.org/upgrading_ruby_on_rails.html#top-level-hashwithindifferentaccess-is-soft-deprecated @@ -6174,7 +6243,7 @@ validates :account, length: { minimum: MIN_LENGTH } | Yes | No | 0.51 -| - +| 2.18 |=== Checks that environments called with `Rails.env` predicates @@ -6201,6 +6270,10 @@ Rails.env == 'production' |=== | Name | Default value | Configurable values +| Severity +| `warning` +| String + | Environments | `development`, `test`, `production` | Array @@ -6504,7 +6577,7 @@ User.where.not(users: { name: 'Gabe' }) | Yes | No | 2.17 -| - +| 2.18 |=== Identifies calls to `where.not` with multiple hash arguments. @@ -6530,3 +6603,13 @@ User.where.not(role: ['moderator', 'admin']) User.where.not(trashed: true).where.not(role: ['moderator', 'admin']) User.where.not('trashed = ? OR role = ?', true, 'admin') ---- + +=== Configurable attributes + +|=== +| Name | Default value | Configurable values + +| Severity +| `warning` +| String +|=== diff --git a/lib/rubocop/rails/version.rb b/lib/rubocop/rails/version.rb index e6e4e360e0..4517b3af13 100644 --- a/lib/rubocop/rails/version.rb +++ b/lib/rubocop/rails/version.rb @@ -4,7 +4,7 @@ module RuboCop module Rails # This module holds the RuboCop Rails version information. module Version - STRING = '2.17.4' + STRING = '2.18.0' def self.document_version STRING.match('\d+\.\d+').to_s diff --git a/relnotes/v2.18.0.md b/relnotes/v2.18.0.md new file mode 100644 index 0000000000..a0ee38fca2 --- /dev/null +++ b/relnotes/v2.18.0.md @@ -0,0 +1,34 @@ +### New features + +* [#863](https://github.com/rubocop/rubocop-rails/pull/863): Add `Rails/ResponseParsedBody` cop. ([@r7kamura][]) + +### Bug fixes + +* [#824](https://github.com/rubocop/rubocop-rails/pull/824): Fix a false negative for `Rails/ActionControllerTestCase` when the class is namespaced. ([@vlad-pisanov][]) +* [#909](https://github.com/rubocop/rubocop-rails/issues/909): Fix a false positive for `Rails/ActionControllerFlashBeforeRender` when using `flash` before `redirect_to` in `if` branch. ([@koic][]) +* [#898](https://github.com/rubocop/rubocop-rails/issues/898): Fix a false positive for `Rails/ActiveRecordAliases` when arguments of `update_attributes` is empty. ([@koic][]) +* [#920](https://github.com/rubocop/rubocop-rails/pull/920): Fix an error for `Rails/NegateInclude` when there is no receiver. ([@fatkodima][]) +* [#931](https://github.com/rubocop/rubocop-rails/pull/931): Fix error in `Rails/Presence` when ternary operators are used in multiple lines. ([@r7kamura][]) +* [#687](https://github.com/rubocop/rubocop-rails/issues/687): Fix `Rails/HasManyOrHasOneDependent` to correctly handle association methods with receiver. ([@fatkodima][]) +* [#929](https://github.com/rubocop/rubocop-rails/issues/929): Prevent `Rails/SquishedSQLHeredocs` applying when single-line comments are present. ([@john-h-k][]) +* [#887](https://github.com/rubocop/rubocop-rails/issues/887): Fix a false positive for `Rails/NotNullColumn` when adding a `:virtual` column. ([@fatkodima][]) +* [#918](https://github.com/rubocop/rubocop-rails/pull/918): Fix `Rails/FreezeTime` running against Rails < 5.2 apps. ([@DRBragg][]) +* [#895](https://github.com/rubocop/rubocop-rails/pull/895): Fix `Rails/UnusedIgnoredColumns` not recognizing columns added via `+=`. ([@lucthev][]) + +### Changes + +* [#263](https://github.com/rubocop/rubocop-rails/pull/263): Accept actions defined via `alias` in `Rails/LexicallyScopedActionFilter`. ([@fatkodima][]) +* [#902](https://github.com/rubocop/rubocop-rails/pull/902): Ignore `redirect` method for `Style/FormatStringToken` by default. ([@javierjulio][]) +* [#935](https://github.com/rubocop/rubocop-rails/pull/935): Make `Style/InverseMethods` aware of Active Support's `present?`, `blank?`, `include?`, and `exclude?` methods. ([@koic][]) +* [#914](https://github.com/rubocop/rubocop-rails/issues/914): Make `Style/InverseMethods` aware of `valid?` and `invalid?` methods. ([@koic][]) +* [#826](https://github.com/rubocop/rubocop-rails/issues/826): Mark `Rails/Pluck` as unsafe. ([@fatkodima][]) +* [#896](https://github.com/rubocop/rubocop-rails/pull/896): Raise severity of `Rails/ActiveRecordOverride`, `Rails/DeprecatedActiveModelErrorsMethods`, `Rails/DuplicateAssociation`, `Rails/DuplicateScope`, `Rails/TopLevelHashWithIndifferentAccess`, and `Rails/WhereNotWithMultipleConditions` cops to warning. ([@koic][]) + +[@r7kamura]: https://github.com/r7kamura +[@vlad-pisanov]: https://github.com/vlad-pisanov +[@koic]: https://github.com/koic +[@fatkodima]: https://github.com/fatkodima +[@john-h-k]: https://github.com/john-h-k +[@DRBragg]: https://github.com/DRBragg +[@lucthev]: https://github.com/lucthev +[@javierjulio]: https://github.com/javierjulio