Commit Graph

117 Commits

Author SHA1 Message Date
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 a1afc7726b.
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
dug c74c52c4b0
Remove redundant Exclude in .rubocop.yml
All `/tmp/` dirs are already excluded by line 11.
2020-06-29 16:06:48 -07:00
fatkodima e24d6ecbfd Update rubocop-performance gem and enable Performance/DeletePrefix and Performance/DeleteSuffix cops 2020-05-24 12:51:35 +03:00
Eugene Kenny a9fc2d4daa Enable Rails/IndexBy and Rails/IndexWith cops
Followup to d3599d8aff, which applied this
style to the existing codebase.
2020-04-19 23:36:05 +01:00
Tejas Bubane cf1abb775e
Bump rubocop to 0.82 (#38980)
* Bump rubocop to 0.82

The `Layout/Tab` cop has been renamed to `Layout/IndentationStyle`
ref: https://github.com/rubocop-hq/rubocop/releases/tag/v0.82.0

No other code changes required.

* Bump rubocop-rails to 2.5.2

No code-changes required.

[Tejas Bubane + Rafael Mendonça França]
2020-04-17 14:24:33 -04:00
Eugene Kenny 9e40348645 Enable HashTransformKeys and HashTransformValues cops
Followup to 88fe76e693.

These are new in RuboCop 0.80.0, and enforce a style we already prefer
for performance reasons (see df81f2e5f5).
2020-02-20 22:37:32 +00:00
Yasuo Honda 78c209a693 Bump RuboCop version to 0.77
https://github.com/rubocop-hq/rubocop/releases/tag/v0.77.0

* Update cop names to changed as follows:

```
$ bundle exec rubocop
Error: The `Layout/IndentFirstArgument` cop has been renamed to `Layout/FirstArgumentIndentation`.
(obsolete configuration found in .rubocop.yml, please update it)
The `Layout/TrailingBlankLines` cop has been renamed to `Layout/TrailingEmptyLines`.
(obsolete configuration found in .rubocop.yml, please update it)
The `Lint/StringConversionInInterpolation` cop has been renamed to `Lint/RedundantStringCoercion`.
(obsolete configuration found in .rubocop.yml, please update it)
$
```

* Auto correct these offenses:

```
$ bundle exec rubocop -a
... snip ...
Offenses:

activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb:104:11: C: [Corrected] Style/RedundantReturn: Redundant return detected.
          return true
          ^^^^^^
activerecord/lib/active_record/reflection.rb:622:15: C: [Corrected] Style/RedundantReturn: Redundant return detected.
              return inverse_name
              ^^^^^^
activerecord/lib/active_record/table_metadata.rb:48:9: C: [Corrected] Style/RedundantReturn: Redundant return detected.
        return self
        ^^^^^^

2812 files inspected, 3 offenses detected, 3 offenses corrected
$
```
2019-11-27 23:58:49 +00:00
Ryuta Kamizono 9b8fb3fc04 Enable `Layout/ClosingHeredocIndentation` cop 2019-11-24 09:44:32 +09:00
Yasuo Honda ce9a220fdc Enabled GitHub Actions to run the latest RuboCop 0.76.0
Updated the default Ruby workflow:

* Trigger this action for pull request and push
* Install requried OS packages for Active Record connection adapters
* Removing `gem install bundler` because Ruby 2.6 already installs it
* Run RuboCop in parallel by `bundle exec rubocop --parallel`
* Rename `ruby.yml` to `rubocop.yml` to represent the workflow

Bump RuboCop to 0.76.0 and rename `Style/UnneededPercentQ` to `Style/RedundantPercentQ`

```ruby
$ bundle exec rubocop
Error: The `Style/UnneededPercentQ` cop has been renamed to `Style/RedundantPercentQ`.
(obsolete configuration found in .rubocop.yml
```

Removed `.codeclimate.yml`
2019-10-30 23:21:09 +00:00
Kasper Timm Hansen 245b037734
Less Pure Copping: cop-out tmp folders for generated Rails apps
After having run the railties tests locally and running Rubocop, you'd
see lots of violations for the Rails apps generated in tmp folders.

Don't consider those violations as violating.
2019-10-07 02:15:32 +02:00
Ryuta Kamizono 0770c025bd Do not enforce no braces for hash argument
Non-kwargs parameters should be to be braced for https://github.com/ruby/ruby/pull/2395.
See https://bugs.ruby-lang.org/issues/14183 for details.

`Style/BracesAroundHashParameters` cop conflicts with that.

This removes `Style/BracesAroundHashParameters` cop and auto-correct to
following changes.

d94263f...5665fb5
2019-09-04 08:49:36 +09:00
Koichi ITO 1c66e047f6 Bump RuboCop to 0.74.0
### Summary

RuboCop 0.74.0 has been released.
https://github.com/rubocop-hq/rubocop/releases/tag/v0.74.0

And rubocop-0-74 channel is available in Code Climate.
https://github.com/codeclimate/codeclimate/releases/tag/v0.85.5

This PR specifies the same RuboCop Performance (1.3.0) and RuboCop Rails (2.0.0)
versions as Code Climate's Gemfile.lock.
https://github.com/codeclimate/codeclimate-rubocop/blob/channel/rubocop-0-74/Gemfile.lock#L51-L55

Also, the `EnforcedStyle` of `Layout/IndentationConsistency` has been renamed
from `EnforcedStyle: rails` to `EnforcedStyle: indented_internal_methods`

- https://github.com/rubocop-hq/rubocop/pull/7113
- https://github.com/rubocop-hq/rubocop/pull/7163

And this commit disables `Layout/SpaceAroundOperators`
that was changed from RuboCop 0.74 by rubocop-hq/rubocop#7211.

cf. https://github.com/rails/rails/pull/36943#issuecomment-521659529
2019-08-15 23:28:06 +09:00
Ryuta Kamizono c81af6ae72 Enable `Layout/EmptyLinesAroundAccessModifier` cop
We sometimes say "✂️ newline after `private`" in a code review (e.g.
https://github.com/rails/rails/pull/18546#discussion_r23188776,
https://github.com/rails/rails/pull/34832#discussion_r244847195).

Now `Layout/EmptyLinesAroundAccessModifier` cop have new enforced style
`EnforcedStyle: only_before` (https://github.com/rubocop-hq/rubocop/pull/7059).

That cop and enforced style will reduce the our code review cost.
2019-06-13 12:00:45 +09:00
Abhay Nikam 00b3b68602 Bump rubocop to 0.71 2019-06-06 15:34:50 +05:30
Koichi ITO c6379fd27f Bump RuboCop to 0.67.2
Performance cops will be extracted from RuboCop to RuboCop Performance
when next RuboCop 0.68 will be released.
https://github.com/rubocop-hq/rubocop/issues/5977

RuboCop 0.67 is its transition period.

Since rails/rails repository uses Performance cops, This PR added
rubocop-performance gem to Gemfile.

And this PR fixes some offenses using the following auto-correct.

```console
% bundle exec rubocop -a

Offenses:

activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb:212:26:
C: [Corrected] Layout/SpaceAroundOperators: Operator =
> should be surrounded by a single space.
              "primary"  => { adapter: "sqlite3", database: "db/primary.sqlite3" }
                         ^^
activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb:239:26:
C: [Corrected] Layout/SpaceAroundOperators: Operator => should be
surrounded by a single space.
              "primary"  => { adapter: "sqlite3", database: "db/primary.sqlite3" }
                         ^^
actionview/test/template/resolver_shared_tests.rb:1:1: C: [Corrected]
Style/FrozenStringLiteralComment: Missing magic comment #
frozen_string_literal: true.
module ResolverSharedTests
^
actionview/test/template/resolver_shared_tests.rb:10:33: C: [Corrected]
Layout/SpaceAroundEqualsInParameterDefault: Surrounding space missing in
default value assignment.
  def with_file(filename, source="File at #{filename}")
                                ^
actionview/test/template/resolver_shared_tests.rb:106:5: C: [Corrected]
Rails/RefuteMethods: Prefer assert_not_same over refute_same.
    refute_same a, b
    ^^^^^^^^^^^

2760 files inspected, 5 offenses detected, 5 offenses corrected
```
2019-04-16 17:58:24 +09:00
kei 68cad40c09 Layout/SpaceBeforeComment Enabled: true 2019-04-12 01:20:25 +09:00
yuuji.yaginuma a35a7603c2 Exclude all `node_modules` from the RuboCop check
For avoiding check against `node_modules` in railties's test dir.
2019-04-10 12:19:45 +09:00
Ryuta Kamizono 4353adab89 Enable `Lint/AmbiguousOperator` and `Lint/AmbiguousRegexpLiteral` cops
To avoid newly adding the warnings, which are frequently addressed.

ac721c8552
951383bd9a
8a0f235fd3

c33c03e80c
424b201983
2019-03-06 10:01:33 +09:00
Ryuta Kamizono 6b8daad038 Enable `Performance/ReverseEach` cop to avoid newly adding `reverse.each`
https://github.com/rails/rails/pull/35451#discussion_r262746834
2019-03-06 09:41:06 +09:00
Ryuta Kamizono 7ebfb319ff Enable `Lint/ErbNewArguments` cop to avoid the deprecated arguments warning
Related 5754a29a97.

And follows Ruby standard library style https://github.com/ruby/ruby/commit/3406c5d.
2019-02-01 14:20:11 +09:00
bogdanvlviv 2bad3f46cd
Add foreign key to active_storage_attachments for `blob_id` via new migration
We need this in order to be able to add this migration for users that
use ActiveStorage during update their apps from Rails 5.2 to Rails 6.0.

Related to #33405

`rake app:update` should update active_storage

`rake app:update` should execute `rake active_storage:update`
if it is used in the app that is being updated.
It will add new active_storage's migrations to users' apps during update Rails.

Context https://github.com/rails/rails/pull/33405#discussion_r204239399

Also, see a related discussion in the Campfire:
https://3.basecamp.com/3076981/buckets/24956/chats/12416418@1236713081
2019-01-16 13:13:23 +00:00
Ryuta Kamizono ea65d92f19
Enable `Lint/UselessAssignment` cop to avoid unused variable warnings (#34904)
* Enable `Lint/UselessAssignment` cop to avoid unused variable warnings

Since we've addressed the warning "assigned but unused variable"
frequently.

370537de05
3040446cec
5ed618e192
76ebafe594

And also, I've found the unused args in c1b14ad which raises no warnings
by the cop, it shows the value of the cop.
2019-01-09 18:09:01 +09:00
yuuji.yaginuma 5df737b7e8 Enable `Lint/DeprecatedClassMethods` cop to avoid using deprecated methods 2019-01-09 12:00:08 +09:00
George Claghorn 0decd2ddc4 Import Action Text 2019-01-04 22:22:49 -05:00
Ryuta Kamizono 6d959017bd Enable `Lint/ShadowingOuterLocalVariable` cop to avoid newly adding the warning
Since we've addressed the warning "warning: shadowing outer local
variable" frequently.

2c325182b8
df76eaa4f1
b86c2a6767
b658743ac2
b18f2fe96d
2018-12-28 07:48:26 +09:00
George Claghorn a5b2fff64c Import Action Mailbox 2018-12-25 21:32:35 -05:00
Ryuta Kamizono 892e38c78e Enable `Style/RedundantBegin` cop to avoid newly adding redundant begin block
Currently we sometimes find a redundant begin block in code review
(e.g. https://github.com/rails/rails/pull/33604#discussion_r209784205).

I'd like to enable `Style/RedundantBegin` cop to avoid that, since
rescue/else/ensure are allowed inside do/end blocks in Ruby 2.5
(https://bugs.ruby-lang.org/issues/12906), so we'd probably meets with
that situation than before.
2018-12-21 06:12:42 +09:00
Kasper Timm Hansen 1b7c3222e8
Require Ruby 2.5 for Rails 6.
Generally followed the pattern for https://github.com/rails/rails/pull/32034

* Removes needless CI configs for 2.4
* Targets 2.5 in rubocop
* Updates existing CHANGELOG entries for fewer merge conflicts
* Removes Hash#slice extension as that's inlined on Ruby 2.5.
* Removes the need for send on define_method in MethodCallAssertions.
2018-12-19 21:47:50 +01:00
Ryuta Kamizono f907b418ae Enable `Layout/SpaceAfterSemicolon` cop to avoid newly adding odd spacing
Ref 59ff1ba30d (diff-38fb97fba84b1ef0f311c4110a597c44R35)
2018-12-13 18:06:04 +09:00