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 Rails/I18nLazyLookup cop #326

Merged
merged 1 commit into from
Jan 25, 2022

Conversation

fatkodima
Copy link
Contributor

@fatkodima fatkodima force-pushed the i18n_lazy_lookup-cop branch 2 times, most recently from c0b9a10 to 216afa2 Compare August 12, 2020 14:14
"#{path}.#{action_name}.#{key}"
end

def controller_path(controller)
Copy link
Member

Choose a reason for hiding this comment

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

Why is this only for controllers? How about mailers, models, serializers etc?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I remember correctly, "Lazy Lookup" works only inside views and controllers? https://guides.rubyonrails.org/i18n.html#lazy-lookup

Copy link
Member

@pirj pirj Dec 25, 2021

Choose a reason for hiding this comment

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

Fair enough. Sadly, the guide confusingly tells "Rails implements a convenient way to look up the locale inside views". - edit they also tell "can also be used in controllers", so it's fine.

Views are unlikely something we can parse with RuboCop, can we?

So it's all good here 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I haven't seen that rubocop has any view checking code in any cops, so do not know.

Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

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

Fantastic!

config/default.yml Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
docs/modules/ROOT/pages/cops.adoc Outdated Show resolved Hide resolved
lib/rubocop/cop/rails/i18n_lazy_lookup.rb Show resolved Hide resolved
lib/rubocop/cop/rails/i18n_lazy_lookup.rb Outdated Show resolved Hide resolved
expect_no_offenses(<<~RUBY)
class FooController
def action
t ['foo.action.key']
Copy link
Member

Choose a reason for hiding this comment

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

Is this legal?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Rails' t helpers delegate to I18n.translate and this is used for bulk translates.

Copy link
Member

Choose a reason for hiding this comment

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

Tested this and found a couple of notable things:

  1. t ['.key'] won't work as doesn't Rails allow lazy lookup for bulk lookup. So this example in question is perfectly fine.

  2. What troubled me is the implementation of translate in controller code:

> ['.a', '.b'].start_with?('.')
NoMethodError: undefined method `start_with?' for [".a", ".b"]:Array

It would work before Rails 7, because it was implemented as:

if key.to_s.first == "."

where key is ['.a', '.b'], but I suspect it would fail in Rails 7.

  1. AbstractController::Translation is not included to ActionController::API, only to ActionController::Base](https://github.com/rails/rails/blob/main/actionpack/lib/action_controller/base.rb). It doesn't make the cop unsafe, since it's not us who suggest using t, it's already there in the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for investigation 👍
Yes, I think no one actually uses this bulk feature, since, as you mentioned, it doesn't work.

This test actually tests that no offenses are generated for this case. Only when the keys is a string or a symbol.

So, wdyt, should I remove this test case or left it as is?

Copy link
Member

Choose a reason for hiding this comment

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

It's fine to keep it, just to make sure we don't add to the existing mess 😄

@fatkodima
Copy link
Contributor Author

Updated.

@fatkodima
Copy link
Contributor Author

Updated.

@koic
Copy link
Member

koic commented Jan 5, 2022

I will merge this new cop after the next bug fix release. Thank you.

koic added a commit that referenced this pull request Jan 6, 2022
…of multiline

Follow up to #326 (comment).

For now this repository can be set to `SafeMultiline: false`.
@koic koic merged commit c4116f4 into rubocop:master Jan 25, 2022
@koic
Copy link
Member

koic commented Jan 25, 2022

Thanks!

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

4 participants