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

[Fix #7900] Fix Style/FormatStringToken false positive with formatted input and template style enforced, and add autocorrection #10160

Merged
merged 1 commit into from Jun 23, 2022

Conversation

FnControlOption
Copy link
Contributor

Fixes #7900.

Types of format strings:

  • annotated: '%<name>s' (where s is the format type)
  • template: '%{name}'
  • unannotated: '%s'

The format string '%{name}' is only equivalent to '%<name>s', so when enforcing the template style, no offenses should be reported if the format type is not s (for example, '%<name>f').

The autocorrection from template style %[flags][width][.precision]{name} to annotated style is %<name>[flags][width][.precision]s, and vice versa.

Because unannotated has no names, it can't be autocorrected to annotated or template.

Autocorrecting from either annotated or template to unannotated is possible, but I haven't attempted adding this yet. The method arguments must be updated too, so def on_send would need to be added (maybe as a new cop). For example:

format('%<greeting>s', greeting: 'Hello')
format('%{foo} %{bar} %{foo}', foo: 'foo', bar: 'bar')
# becomes
format('%s', 'Hello')
format('%1$s %2$s %1$s', 'foo', 'bar')

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.

…formatted input and `template` style enforced, and add autocorrection
Comment on lines +120 to +128
return if style == :unannotated

name = detected_sequence.name
return if name.nil?

flags = detected_sequence.flags
width = detected_sequence.width
precision = detected_sequence.precision
type = detected_sequence.style == :template ? 's' : detected_sequence.type
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 realized that these lines can be simplified to:

return if style == :unannotated || detected_sequence.style == :unannotated

name = detected_sequence.name
flags = detected_sequence.flags
width = detected_sequence.width
precision = detected_sequence.precision
type = case detected_sequence.style
       when :annotated then detected_sequence.type
       when :template then 's'
       end

Copy link
Contributor

@marcandre marcandre left a comment

Choose a reason for hiding this comment

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

Looks good to me 👍

@bbatsov bbatsov merged commit 8eb4f1b into rubocop:master Jun 23, 2022
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.

Style/FormatStringToken false positive with formatted input and template style enforced
3 participants