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

Style/FormatStringToken does not respect IgnoredMethods for values in nested structures #10203

Closed
foobear opened this issue Oct 21, 2021 · 0 comments · Fixed by #10235
Closed

Comments

@foobear
Copy link

foobear commented Oct 21, 2021

The Style/FormatStringToken cop does not respect IgnoredMethods for values which are nested, e.g. into an options hash.

Suggested solution

I've investigated why that is, and the culprit seems to be FormatStringToken#use_ignored_method? where we we only check if the found node (the offending string) has a method parent (send type). In case of a value inside a hash, the parent is not the method itself.

Maybe it makes sense to allow climbing up the node tree as long as parents are pair or hash nodes (maybe others?), and only then check if the surrounding node is of type send.

Please let me know if that approach sounds reasonable and I can try implementing a fix.


Expected behavior

Given the following config,

Style/FormatStringToken:
  EnforcedStyle: annotated
  IgnoredMethods:
    - example

and the following Ruby code,

example("Hello %{name}")
example("Hello %{name}", bye: "Bye %{name}")

both calls to example should be ignored.

Actual behavior

example("Hello %{name}") is ignored, but example("Hello %{name}", bye: "Bye %{name}") raises an offense.

Offenses:

test.rb:2:36: C: Style/FormatStringToken: Prefer annotated tokens (like %<foo>s) over template tokens (like %{foo}).
example("Hello %{name}", bye: "Bye %{name}")
                                   ^^^^^^^

Steps to reproduce the problem

Run Rubocop with example config from above against a file with example Ruby code from above.

RuboCop version

$ bundle exec rubocop -V
1.22.1 (using Parser 3.0.2.0, rubocop-ast 1.12.0, running on ruby 2.7.3 x86_64-linux)
  - rubocop-performance 1.11.5
  - rubocop-rails 2.12.4
  - rubocop-rspec 2.5.0
tejasbubane added a commit to tejasbubane/rubocop that referenced this issue Nov 4, 2021
dvandersluis added a commit that referenced this issue Nov 5, 2021
[Fix #10203] Fix `Style/FormatStringToken` to respect `IgnoredMethods` with nested structures
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant