Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new MigrationClassName cop #644

Merged
merged 1 commit into from
Feb 27, 2022
Merged

Conversation

j-miyake
Copy link
Contributor

@j-miyake j-miyake commented Feb 24, 2022

This cop makes sure that each migration file defines a migration class whose name matches the file name.
(i.e. 20220224111111_create_users.rb should define CreateUsers class.)

The migration will fail when you rename the migration class and forget to rename the file name appropriately. Unfortunately, it's hard to detect this kind of bug with automated tests that use database tables restored from db/schema.rb (e.g. db:setup). The static inspection for the file names could help to prevent the bug.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@j-miyake j-miyake force-pushed the migration_file_name branch 6 times, most recently from 167304e to 34b9016 Compare February 24, 2022 14:30
private

def to_snakecase(word)
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this shouldn't mutate the word, i.e.:

word
  .gsub(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
  .gsub(/([a-z\d])([A-Z])/, '\1_\2')
  .tr('-', '_')
  .downcase

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true. Thanks!

@j-miyake j-miyake marked this pull request as ready for review February 24, 2022 23:57
changelog/new_add_new_migrationfilename_cop.md Outdated Show resolved Hide resolved
lib/rubocop/cop/rails/migration_file_name.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rails/migration_file_name.rb Outdated Show resolved Hide resolved
def on_new_investigation
filepath = processed_source.file_path
@basename = File.basename(filepath, '.rb')
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move these logic in the on_class method and refactor to prevent the use of the instance variable @basename?
https://refactoring.com/catalog/inlineFunction.html

lib/rubocop/cop/rails/migration_file_name.rb Outdated Show resolved Hide resolved
def on_class(node)
class_name = node.loc.name.source
snake_class_name = to_snakecase(class_name)
basename_without_timestamp = @basename.sub(/^[0-9]+_/, '')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
basename_without_timestamp = @basename.sub(/^[0-9]+_/, '')
basename_without_timestamp = @basename.delete_prefix(/\A\d+_/, '')

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops! It was correctly as follows.

-          basename_without_timestamp = @basename.delete_prefix(/\A\d+_/, '')
+          basename_without_timestamp = @basename.sub(/\A\d+_/, '')

spec/rubocop/cop/rails/migration_file_name_spec.rb Outdated Show resolved Hide resolved
spec/rubocop/cop/rails/migration_file_name_spec.rb Outdated Show resolved Hide resolved
it do
expect_offense(<<~RUBY, filename)
class SellBooks < ActiveRecord::Migration[7.0]
^^^^^^^^^ The class name does not match with the file name.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be user-friendly to indicate the class name to replace.

Suggested change
^^^^^^^^^ The class name does not match with the file name.
^^^^^^^^^ Replace with `CreateUsers` that matches the file name.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I think this cop can provide autocorrect the class name.

# class CreateUsers < ActiveRecord::Migration[7.0]
# end
#
class MigrationFileName < Base
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MigrationClassName seems to be more suitable than MigrationFileName.

Suggested change
class MigrationFileName < Base
class MigrationClassName < Base

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's absolutely right! Thanks!

Copy link
Contributor Author

@j-miyake j-miyake Feb 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the title of this pull request as well.

@j-miyake j-miyake changed the title Add new MigrationFileName cop Add new MigrationClassName cop Feb 25, 2022
class MigrationClassName < Base
extend AutoCorrector

MSG = 'Replace with `%<preferred_class_name>s` that matches the file name.'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe "corrected" is better than "preferred" . Because it ’s not a difference in style :-)

Suggested change
MSG = 'Replace with `%<preferred_class_name>s` that matches the file name.'
MSG = 'Replace with `%<corrected_class_name>s` that matches the file name.'

Comment on lines 31 to 32
preferred_class_name = to_camelcase(basename_without_timestamp)
message = format(MSG, preferred_class_name: preferred_class_name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
preferred_class_name = to_camelcase(basename_without_timestamp)
message = format(MSG, preferred_class_name: preferred_class_name)
corrected_class_name = to_camelcase(basename_without_timestamp)
message = format(MSG, corrected_class_name: corrected_class_name)

Comment on lines 34 to 36
add_offense(node.loc.name, message: message) do |corrector|
corrector.replace(node.loc.name, preferred_class_name)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
add_offense(node.loc.name, message: message) do |corrector|
corrector.replace(node.loc.name, preferred_class_name)
end
add_offense(node.identifier, message: message) do |corrector|
corrector.replace(node.identifier, preferred_class_name)
end

@koic
Copy link
Member

koic commented Feb 25, 2022

Nice! Can you squash your commits into one?

@j-miyake
Copy link
Contributor Author

Thanks for reviewing!

By the way, is there something like a wiki for writing better cops? I read source code in rubocop-ast repository to learn what node.identifier exactly does, but I would love to read the guide if it exists.

@koic
Copy link
Member

koic commented Feb 25, 2022

Here is an introductory document, but I'm also learning by reading the source code.
https://docs.rubocop.org/rubocop/development.html

@koic koic merged commit 02406b9 into rubocop:master Feb 27, 2022
@j-miyake j-miyake deleted the migration_file_name branch February 27, 2022 12:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants