Commit Graph

145 Commits

Author SHA1 Message Date
Earlopain
720c7d14dd
Revert RuboCop workaround
Was done in 3e08223ece75ab47a17459f9696ce1ebca68ffa5 to work around a RuboCop bug.
The newest version has this fixed.
2024-05-23 14:56:53 +02:00
Rafael Mendonça França
3e08223ece
Fix rubocop
See https://github.com/rubocop/rubocop/issues/12898 for the issue.
2024-05-13 18:04:01 +00:00
zzak
833c1c9a4d
Enable Lint/Debugger
This will prevent issues like be0cb4e8f9, which would have resulted in:

```
guides/rails_guides/generator.rb:16:1: W: Lint/Debugger: Remove debugger entry point require "debug".
require "debug"
^^^^^^^^^^^^^^^
```

Disabled the cop in actionpack tests for screenshot_helper and page_dump_helper:

```
actionpack/test/controller/integration_test.rb:1369:9: W: Lint/Debugger: Remove debugger entry point save_and_open_page.
        save_and_open_page
        ^^^^^^^^^^^^^^^^^^
actionpack/test/controller/integration_test.rb:1381:11: W: Lint/Debugger: Remove debugger entry point save_and_open_page.
          save_and_open_page
          ^^^^^^^^^^^^^^^^^^
actionpack/test/controller/integration_test.rb:1391:39: W: Lint/Debugger: Remove debugger entry point save_and_open_page.
      assert_raise(InvalidResponse) { save_and_open_page }
                                      ^^^^^^^^^^^^^^^^^^
```

```
actionpack/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb:111:13: W: Lint/Debugger: Remove debugger entry point page.save_page(absolute_html_path).
            page.save_page(absolute_html_path)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
actionpack/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb:115:13: W: Lint/Debugger: Remove debugger entry point page.save_screenshot(absolute_image
_path).
            page.save_screenshot(absolute_image_path)
```

The DebuggerRequires option was first available in rubocop v1.63.0, in rubocop/rubocop#12766.
2024-05-13 13:41:18 +09:00
fatkodima
041de49399 Remove usage of OpenStruct 2024-04-09 21:35:08 +03:00
Jean Boussier
e922c59207 Implement Active Job enqueue_after_transaction_commit
A fairly common mistake with Rails is to enqueue a job from inside a
transaction, with a record as argumemnt, which then lead to a RecordNotFound
error when picked up by the queue.

This is even one of the arguments advanced for job runners backed by the
database such as `solid_queue`, `delayed_job` or `good_job`.

But relying on this is undesirable in my opinion as it makes the Active Job
abstraction leaky, and if in the future you need to migrate to another backend
or even just move the queue to a separate database, you may experience a lot of
race conditions of the sort.

To resolve this problem globally, we can make Active Job optionally transaction
aware, and automatically defer job queueing to `after_commit`.

Co-Authored-By: Cristian Bica <cristian.bica@gmail.com>
2024-04-03 16:32:16 +02:00
Yasuo Honda
5f04fa99dc Enable Layout/DefEndAlignment cop
Follow-up #46188

- After reverting the merge commit via #46188

```
$ git revert -m 1 4328d0e16028a46bba79ab775e509a743ceaf18c
[enable_layout_def_end_alignment 8445594965] Revert "Merge pull request #46188 from yahonda/follow_up_45081"
 1 file changed, 1 insertion(+), 1 deletion(-)
$ bundle exec rubocop
Inspecting 3041 files
... snip ...
Offenses:

activerecord/test/cases/query_logs_formatter_test.rb:10:5: W: [Correctable] Layout/DefEndAlignment: end at 10, 4 is not aligned with def at 6, 2.
    end
    ^^^

3041 files inspected, 1 offense detected, 1 offense auto-correctable
$
```
2024-01-15 11:00:32 +09:00
Rafael Mendonça França
c003ed7c69
Merge pull request #50655 from Earlopain/enable-lint-safe-navigation-chain
Enable `Lint/SafeNavigationChain` rubocop cop
2024-01-08 14:51:18 -05:00
Earlopain
0a9dcb0d20
Enable Lint/SafeNavigationChain rubocop cop 2024-01-08 20:14:58 +01:00
Earlopain
97398e4a7d
Enable Style/ArrayIntersect rubocop cop 2024-01-08 09:21:53 +01:00
Rafael Mendonça França
8b4e92f4be
Point rubocop to ruby 3.1 2024-01-03 19:02:32 +00:00
Yasuo Honda
6384eec00f
Enable Minitest/NonExecutableTestMethod cop
This pull request enables `Minitest/NonExecutableTestMethod` cop
to find non-executed test that is out of `ActiveSupport::TestCase` and its subclasses.

This cop is based on the request since there was a test that is not executed found
at https://github.com/rails/rails/pull/50334#issuecomment-1851411434
and implemented to RuboCop Minitest 0.34.0 via https://github.com/rubocop/rubocop-minitest/issues/279

This cop works as follows.
As of right now, there is no offenses by reverting the merge commit via #50334

```
$ git revert -m 1 9517841
$ bundle exec rubocop
Inspecting 3254 files
... snip ...

Offenses:

activerecord/test/cases/assertions/query_assertions_test.rb:27:5: W: Minitest/NonExecutableTestMethod: Test method should be defined inside a test class to ensure execution.
    def test_assert_no_queries ...
    ^^^^^^^^^^^^^^^^^^^^^^^^^^

3254 files inspected, 1 offense detected
$
```

* `Gemfile.lock` has been updated as follows
```
bundle update rubocop rubocop-minitest --conservative
```
2024-01-03 17:04:25 +00:00
Akhil G Krishnan
6499683ebd Removed Performance/UnfreezeString cop 2023-11-20 20:31:17 +05:30
fatkodima
af8f002c88 Enable Lint/InterpolationCheck rubocop's cop 2023-10-26 15:33:59 +03:00
Nikita Vasilevsky
19f8ab2e7d
[Tests only] Enable Minitest/AssertPredicate rule 2023-10-13 19:26:47 +00:00
fatkodima
b8829cabec Enable Style/RedundantDoubleSplatHashBraces rubocop cop 2023-10-11 14:55:00 +03:00
fatkodima
a7ee313d91 Enable raising when running rubocop-md against invalid ruby snippets 2023-09-27 23:12:48 +03:00
fatkodima
7ef86b6a49 Enable Lint/RedundantSafeNavigation rubocop cop 2023-09-27 14:55:07 +03:00
zzak
5c2a112d37
Import rails-bin to internal tools/
Co-authored-by: zzak <zzakscott@gmail.com>
2023-09-13 01:57:16 +00:00
Shouichi Kamiya
51ac8b9f6f Enable Minitest/LiteralAsActualArgument
There are assertions that expected/actual arguments are passed in the
reversed order by mistake. Enabling the LiteralAsActualArgument rule
prevents this mistake from happening.

The existing tests were auto-corrected by rubocop with a bit of
indentation adjustment.

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2023-09-13 10:09:32 +09:00
zzak
dadf46de36
Add rubocop-md for linting guides snippets 2023-03-15 10:48:19 +09:00
Hartley McGuire
11c59f7243
Regenerate Active Storage dummy app
Generated using the following script and manually reviewed after:
skipkayhil/rails-bin@cbad7eddcb

The active_storage configuration had to be moved to the dummy app
because of the changed eager_load config. Now, the whole app is eagerly
loaded when the app's environment file is required which means that
changing configuration in the test_helper is too late.
2023-02-18 17:51:27 -05:00
Hartley McGuire
978993e880
Enable Style/EvalWithLocation
Ref: ad39d6b

Ensure that all evals include file and line number to identify their
source.

Two of the evals reported by this cop were unneccesary and replaced with
non-eval alternatives: xml is set as a local variable in
Template::Handlers::Builder#call, and instance_eval isn't needed to get
the current binding.

There are additionally 27 offenses in test directories, but since it
seems less important to fix those they are currently ignored.
2023-01-11 18:46:09 -05:00
Koichi ITO
d0ef331087 Enable Lint/DuplicateMagicComment cop
This PR enables `Lint/DuplicateMagicComment` cop to resolve
the background of https://github.com/rubocop/rubocop/issues/11043.

And this PR bumps RuboCop version because the cop was introduced in RuboCop 1.37.0:
https://github.com/rubocop/rubocop/blob/master/CHANGELOG.md#1370-2022-10-20
2022-11-14 16:04:25 +09:00
Hartley McGuire
d68e36beab
Enable Minitest/SkipEnsure
I believe this was supposed to be included in 1b736a8
2022-09-20 21:20:17 -04:00
Hartley McGuire
c62dcf54eb
fix remaining asserts that should be assert_equal
Found using Minitest/AssertWithExpectedArgument.

Also enabled the rule per feedback and fixed 29 additional violations
2022-09-09 19:22:21 -04:00
Koichi ITO
9c56d087fc Enable Minitest/AssertRaisesWithRegexpArgument cop
Follow up #45965.

The commit to enable `Minitest/AssertRaisesWithRegexpArgument` cop was forgotten.
2022-09-08 18:25:57 +09:00
fatkodima
3158bbb9f6 Update rubocop-performance and enable more performance-related cops 2022-08-26 15:07:11 +03:00
fatkodima
86519996f9 Enable Style/RedundantCondition cop 2022-08-11 11:38:14 +03:00
Koichi ITO
819871cc4e Enable Style/MapToHash cop
Ruby 2.6 added block argument processing to `Enumerable#to_h`.
https://bugs.ruby-lang.org/issues/15143

Rails 7 requires Ruby 2.7.0 or higher, so the new feature can use it.
`Style/MapToHash` cop will detect it. And this cop in the `Style` department,
but this seems to improve performance as follows:

```ruby
# map_to_hash.rb
require 'benchmark/ips'

ARRAY = (1..100).to_a
HASH = {foo: 1, bar: 2}

Benchmark.ips do |x|
  x.report('array.map.to_h') { ARRAY.map { |v| [v, v * 2] }.to_h }
  x.report('array.to_h')     { ARRAY.to_h { |v| [v, v * 2] } }

  x.compare!
end

Benchmark.ips do |x|
  x.report('hash.map.to_h') { HASH.map { |k, v| [k.to_s, v * 2] }.to_h }
  x.report('hash.to_h')     { HASH.to_h { |k, v| [k.to_s, v * 2] } }

  x.compare!
end
```

```console
% ruby map_to_hash.rb
Warming up --------------------------------------
      array.map.to_h     9.063k i/100ms
          array.to_h     9.609k i/100ms
Calculating -------------------------------------
      array.map.to_h     89.063k (± 3.9%) i/s -    453.150k in  5.096572s
          array.to_h     96.449k (± 1.7%) i/s -    490.059k in  5.082529s

Comparison:
          array.to_h:    96448.7 i/s
      array.map.to_h:    89063.4 i/s - 1.08x  (± 0.00) slower

Warming up --------------------------------------
       hash.map.to_h   106.284k i/100ms
           hash.to_h   149.354k i/100ms
Calculating -------------------------------------
       hash.map.to_h      1.102M (± 2.2%) i/s -      5.527M in   5.019657s
           hash.to_h      1.490M (± 0.9%) i/s -      7.468M in   5.013264s

Comparison:
           hash.to_h:  1489707.0 i/s
       hash.map.to_h:  1101561.5 i/s - 1.35x  (± 0.00) slower
```

`Style/MapToHash` cop ... https://docs.rubocop.org/rubocop/1.25/cops_style.html#stylemaptohash
2022-02-26 04:31:03 +09:00
Gannon McGibbon
03293da4e9 Enable Performance/OpenStruct 2022-02-17 17:16:42 -05:00
Nikita Vasilevsky
8a943a27d8 Enable Lint/EnsureReturn cop 2022-02-05 23:58:07 +00:00
Nikita Vasilevsky
2a00c89a7d
Enable Lint/DuplicateMethods rubocop rule 2021-11-15 13:51:28 -05:00
Jean Boussier
532ef0d13c Add back Lint/UselessAssignment
Fix: #43169
2021-09-06 16:24:37 +02:00
Jean Boussier
c91c266872 Enable Style/ExplicitBlockArgument cop
This reduce the stack size which is beneficial for
exceptions performance.

See: https://gist.github.com/byroot/cb3bcadcc3701c2518d002fb8d3a4e7a

However the cop is unsafe because it might change the block arity,
so it can run into some false positives.
2021-09-05 17:06:19 +02:00
David Heinemeier Hansson
82e4432058
Javascript generator option with choices (#43160)
* Switch to a single controller option for choosing JavaScript approach

* Remove remnants of webpacker specific work within Rails

* No longer used

* Missing space

* Raise if unknown option is passed

* Style

* Use latest versions

* Make channels setup generic to all node setups

* Make Action Text installer work with any node package manager

* Explaining variables are not useless

* Rubocop pleasing

* Don't rely on Rails.root

Tests don't like it!

* Rubocopping

* Assume importmap

* No longer relevant

* Another cop

* Style

* Correct installation notice

* Add dependencies for action cable when adding a channel

* Fix paths to be relative to generator

* Just go straight to yarn, forget about binstub

* Fix tests

* Fixup installer, only yarn once

* Test generically with run

* Style

* Fix reference and reversibility

* Style

* Fix test

* Test pinning dependencies

* Remove extra space

* Add more tests

* Use latest dependencies

* Relegated this to controllers

* Refactor ChannelGenerator + more tests

Use a uniform level of abstraction
2021-09-04 11:53:57 +02:00
Koichi ITO
65af100ddd Tweak unreachable assertion tests in the block of assert_raises
I found an unexpected use of assertion in the block of `assert_raise`
when I implemented https://github.com/rubocop/rubocop-minitest/pull/137.
It is expected to be asserted after an exception is raised in
`assert_raise` block, but in actually it is not asserted after an
exception is raised. Therefore, this PR removes or updates assertions
that have not been asserted after an exception has raised.

This PR will add `rubocop-minitest` and enable
`Minitest/UnreachableAssertion` cop to able similar auto-detection,
but will remove `rubocop-minitest` from this PR if you don't like it.
2021-08-17 20:33:08 +09:00
Ryuta Kamizono
d7fce6c996 Fix odd closing parenthesis by enabling the Layout/ClosingParenthesisIndentation cop 2021-07-02 18:01:50 +09:00
Hartley McGuire
a3e5e8d909 use ruby 2.7's filter_map instead of select + map
Related: c3d7794, bbbc861
2021-04-26 17:55:33 -04:00
Ryuta Kamizono
c0c77e28ad Enable Layout/EndOfLine to prevent \r\n is included in the future
Follow up to a1afc7726b3f9a59eb2dbc21fbd7aa59927cc889.
2021-04-24 23:59:19 +09:00
Ryuta Kamizono
bbbc861f71 Enable Performance/MapCompact cop
Follow up to #42053.
2021-04-23 16:33:02 +09:00
Ryuta Kamizono
c4b944a273 Enable Performance/StringReplacement cop
Follow up to #42054.
2021-04-23 16:17:55 +09:00
David Heinemeier Hansson
ab8e3d22cb
Extract ActiveStorage::Streaming so your own controllers can use it (#41440)
Extract ActiveStorage::Streaming so your own controller can use it
2021-02-19 15:40:56 +01:00
Ryuta Kamizono
6515d6985d Use bind_call(obj, ...) instead of bind(obj).call(...)
`bind_call(obj, ...)` is a faster alternative to `bind(obj).call(...)`.

https://bugs.ruby-lang.org/issues/15955

Also, enable `Performance/BindCall` cop to detect those in the future.
2021-02-08 17:01:23 +09:00
Ryuta Kamizono
2e9c0e04c5 Update TargetRubyVersion to 2.7 in .rubocop.yml 2021-02-05 12:23:33 +09:00
Ryuta Kamizono
ba97e9f918 Remove Lint/ShadowingOuterLocalVariable cop
"shadowing outer local variable" warning was removed in Ruby 2.6.

c9d720b873
https://bugs.ruby-lang.org/issues/12490
2021-02-05 12:08:14 +09:00
Rafael Mendonça França
458f19a0f7
Disable rubocop suggestions 2021-01-27 00:28:55 +00:00
Ryuta Kamizono
cbf72c119e Enable Lint/DuplicateRequire cop
Closes #40644.

Co-authored-by: luccafort <konishi.tatsuro@moneyforward.co.jp>
2021-01-09 14:40:47 +09:00
KapilSachdev
a908d06c85
feat(rubocop): Add Style/RedundantRegexpEscape
- This cop will help in removing unnecessary escaping inside Regexp literals.
2020-12-08 18:57:09 +00:00
Ryuta Kamizono
92ff708476 Re-enable Layout/SpaceAroundOperators cop
We prefer space around operators, but `Layout/SpaceAroundOperators` cop
was temporarily disabled in #36943 since that cop changed to check
alignment strictly somehow.

In RuboCop 1.0.0, that is fixed by https://github.com/rubocop-hq/rubocop/pull/8906.

Related https://github.com/rails/rails/pull/38034#discussion_r359845661,
https://github.com/rails/rails/pull/39770#discussion_r448829561.
2020-10-23 16:12:15 +09:00
Utkarsh Gupta
cd9ab098c4 Update RuboCop to v0.90
Signed-off-by: Utkarsh Gupta <utkarsh@debian.org>
2020-09-06 06:37:04 +05:30