Skip to content

Commit

Permalink
Apply autocorrection by RuboCop to files generated by `bin/rails gene…
Browse files Browse the repository at this point in the history
…rate`

## Motivation / Background

RuboCop has now been included by default (rails#50456).
By adding the following tip to the default configuration, user can apply RuboCop's autocorrection to
code generated by `bin/rails generate` (e.g., migration file):

https://github.com/rubocop/rubocop-rails#rails-configuration-tip

This means that the generated files will be formatted according to user's .rubocop.yml custom configuration.

## Detail

Since `bin/rails generate` and `bin/rubocop` are used only in the development environment,
the target files are limited to only `config/environments/development.rb`.

## Additional information

This feature was introduced in Rails 6.1 by rails#38870.
  • Loading branch information
koic committed Dec 31, 2023
1 parent e3da4fc commit 5409a89
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions railties/lib/rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ def after_generate(&block)
@after_generate_callbacks << block
end

def apply_autocorrect_after_generate!
after_generate do |files|
parsable_files = files.filter { |file| file.end_with?(".rb") }
unless parsable_files.empty?
system("bin/rubocop -A --fail-level=E #{parsable_files.shelljoin}", exception: true)
end
end
end

def method_missing(method, *args)
method = method.to_s.delete_suffix("=").to_sym

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,9 @@ Rails.application.configure do
<%- end -%>
# Raise error when a before_action's only/except options reference missing actions
config.action_controller.raise_on_missing_callback_actions = true
<%- unless skip_rubocop? -%>

# Apply autocorrection by RuboCop to files generated by `bin/rails generate`.
config.generators.apply_autocorrect_after_generate!
<%- end -%>
end
9 changes: 9 additions & 0 deletions railties/test/application/generators_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,5 +255,14 @@ def check_expected

assert_match(/# Add comment to model/, File.read(model_file))
end

test "generators with apply_autocorrect_after_generate!" do
with_config do |c|
c.generators.apply_autocorrect_after_generate!
end

output = rails("generate", "model", "post", "title:string", "body:string")
assert_match(/3 files inspected, no offenses detected/, output)
end
end
end
4 changes: 4 additions & 0 deletions railties/test/generators/app_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,7 @@ def test_inclusion_of_a_debugger
def test_inclusion_of_rubocop
run_generator
assert_gem "rubocop-rails-omakase"
assert_file "config/environments/development.rb", %r|# Apply autocorrection by RuboCop to files generated by `bin/rails generate`\.|
end

def test_rubocop_is_skipped_if_required
Expand All @@ -637,6 +638,9 @@ def test_rubocop_is_skipped_if_required
assert_no_gem "rubocop"
assert_no_file "bin/rubocop"
assert_no_file ".rubocop.yml"
assert_file "config/environments/development.rb" do |content|
assert_no_match(%r|# Apply autocorrection by RuboCop to files generated by `bin/rails generate`\.|, content)
end
end

def test_inclusion_of_brakeman
Expand Down

0 comments on commit 5409a89

Please sign in to comment.