Skip to content

Commit

Permalink
[Fix rubocop#5630] Fix false positive Style/FormatStringToken
Browse files Browse the repository at this point in the history
Fixes rubocop#5630.

This PR fixes a false positive for `Style/FormatStringToken` when using
placeholder arguments in `format` method.
  • Loading branch information
koic committed Mar 9, 2018
1 parent d4032d0 commit 2cd03ef
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

* [#5623](https://github.com/bbatsov/rubocop/pull/5623): Fix `Bundler/OrderedGems` when a group includes duplicate gems. ([@colorbox][])
* [#5633](https://github.com/bbatsov/rubocop/pull/5633): Fix broken `--fail-fast`. ([@mmyoji][])
* [#5630](https://github.com/bbatsov/rubocop/issues/5630): Fix false positive for `Style/FormatStringToken` when using placeholder arguments in `format` method. ([@koic][])

### Changes
* [#5626](https://github.com/bbatsov/rubocop/pull/5626): Change `Naming/UncommunicativeMethodParamName` add `to` to allowed names in default config. ([@unused][])
Expand Down Expand Up @@ -3235,4 +3236,4 @@
[@elebow]: https://github.com/elebow
[@colorbox]: https://github.com/colorbox
[@mmyoji]: https://github.com/mmyoji
[@unused]: https://github.com/unused
[@unused]: https://github.com/unused
8 changes: 8 additions & 0 deletions lib/rubocop/cop/style/format_string_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class FormatStringToken < Cop
}.freeze

def on_str(node)
return if placeholder_argument?(node)
return if node.each_ancestor(:xstr, :regexp).any?

tokens(node) do |detected_style, token_range|
Expand Down Expand Up @@ -156,6 +157,13 @@ def slice_source(source_range, new_begin, new_end)
new_end
)
end

def placeholder_argument?(node)
return false unless node.parent
return true if node.parent.pair_type?

placeholder_argument?(node.parent)
end
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/style/format_string_token_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@
RUBY
end

it 'ignores placeholder argumetns' do
expect_no_offenses(<<-RUBY.strip_indent)
format(
'%<day>s %<start>s-%<end>s',
day: open_house.starts_at.strftime('%a'),
start: open_house.starts_at.strftime('%l'),
end: open_house.ends_at.strftime('%l %p').strip
)
RUBY
end

it 'handles __FILE__' do
expect_no_offenses('__FILE__')
end
Expand Down

0 comments on commit 2cd03ef

Please sign in to comment.