Skip to content

Commit

Permalink
[rubocop#8827] Change Style/FormatStringToken style to template
Browse files Browse the repository at this point in the history
  • Loading branch information
pirj committed Jun 19, 2022
1 parent 736e8d6 commit 0207a03
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#8827](https://github.com/rubocop/rubocop/issues/8827): Change the enforced style of `Style/FormatStringToken` to `template`. ([@pirj][])
8 changes: 4 additions & 4 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3635,20 +3635,20 @@ Style/FormatString:
Style/FormatStringToken:
Description: 'Use a consistent style for format string tokens.'
Enabled: true
EnforcedStyle: annotated
EnforcedStyle: template
SupportedStyles:
# Prefer simple looking "template" style tokens like `%{name}`, `%{age}`
- template
# Prefer tokens which contain a sprintf like type annotation like
# `%<name>s`, `%<age>d`, `%<score>f`
- annotated
# Prefer simple looking "template" style tokens like `%{name}`, `%{age}`
- template
- unannotated
# `MaxUnannotatedPlaceholdersAllowed` defines the number of `unannotated`
# style token in a format string to be allowed when enforced style is not
# `unannotated`.
MaxUnannotatedPlaceholdersAllowed: 1
VersionAdded: '0.49'
VersionChanged: '1.0'
VersionChanged: '<<next>>'
IgnoredMethods: []

Style/FrozenStringLiteralComment:
Expand Down
18 changes: 9 additions & 9 deletions docs/modules/ROOT/pages/cops_style.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4208,7 +4208,7 @@ puts '%10s' % 'hoge'
| Yes
| No
| 0.49
| 1.0
| <<next>>
|===

Use a consistent style for named format string tokens.
Expand All @@ -4227,28 +4227,28 @@ if the number of them is less than or equals to

=== Examples

==== EnforcedStyle: annotated (default)
==== EnforcedStyle: template (default)

[source,ruby]
----
# bad
format('%{greeting}', greeting: 'Hello')
format('%<greeting>s', greeting: 'Hello')
format('%s', 'Hello')
# good
format('%<greeting>s', greeting: 'Hello')
format('%{greeting}', greeting: 'Hello')
----

==== EnforcedStyle: template
==== EnforcedStyle: annotated

[source,ruby]
----
# bad
format('%<greeting>s', greeting: 'Hello')
format('%{greeting}', greeting: 'Hello')
format('%s', 'Hello')
# good
format('%{greeting}', greeting: 'Hello')
format('%<greeting>s', greeting: 'Hello')
----

==== EnforcedStyle: unannotated
Expand Down Expand Up @@ -4300,8 +4300,8 @@ redirect('foo/%{bar_id}')
| Name | Default value | Configurable values

| EnforcedStyle
| `annotated`
| `annotated`, `template`, `unannotated`
| `template`
| `template`, `annotated`, `unannotated`

| MaxUnannotatedPlaceholdersAllowed
| `1`
Expand Down
12 changes: 6 additions & 6 deletions lib/rubocop/cop/style/format_string_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ module Style
#
# This cop can be customized ignored methods with `IgnoredMethods`.
#
# @example EnforcedStyle: annotated (default)
# @example EnforcedStyle: template (default)
#
# # bad
# format('%{greeting}', greeting: 'Hello')
# format('%<greeting>s', greeting: 'Hello')
# format('%s', 'Hello')
#
# # good
# format('%<greeting>s', greeting: 'Hello')
# format('%{greeting}', greeting: 'Hello')
#
# @example EnforcedStyle: template
# @example EnforcedStyle: annotated
#
# # bad
# format('%<greeting>s', greeting: 'Hello')
# format('%{greeting}', greeting: 'Hello')
# format('%s', 'Hello')
#
# # good
# format('%{greeting}', greeting: 'Hello')
# format('%<greeting>s', greeting: 'Hello')
#
# @example EnforcedStyle: unannotated
#
Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/cop/style/format_string_token_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
it_behaves_like 'maximum allowed unannotated', token
end

context 'when enforced style is template' do
context 'when enforced style is template (default)' do
let(:enforced_style) { :template }

it 'registers offenses for annotated style' do
Expand Down

0 comments on commit 0207a03

Please sign in to comment.