Skip to content

Commit f3a0cd7

Browse files
committed
Allow FormatParameterMismatch to be called using Kernel
1 parent 381e626 commit f3a0cd7

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* [#2213](https://github.com/bbatsov/rubocop/issues/2213): Write to cache with binary encoding to avoid transcoding exceptions in some locales. ([@jonas054][])
1313
* [#2218](https://github.com/bbatsov/rubocop/issues/2218): Fix loading config error when safe yaml is only partially loaded. ([@maxjacobson][])
1414
* [#2161](https://github.com/bbatsov/rubocop/issues/2161): Allow an explicit receiver (except `Kernel`) in `Style/SignalException`. ([@lumeet][])
15+
* [#2237](https://github.com/bbatsov/rubocop/pull/2237): Allow `Lint/FormatParameterMismatch` to be called using `Kernel.format` and `Kernel.sprintf`. ([@rrosenblum][])
1516

1617
## 0.34.0 (05/09/2015)
1718

lib/rubocop/cop/lint/format_parameter_mismatch.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class FormatParameterMismatch < Cop
1717
# rubocop:disable Metrics/LineLength
1818
FIELD_REGEX = /(%(([\s#+-0\*])?(\d*)?(.\d+)?(\.)?[bBdiouxXeEfgGaAcps]|%))/
1919
NAMED_FIELD_REGEX = /%\{[_a-zA-Z][_a-zA-Z]+\}/
20+
KERNEL = 'Kernel'.freeze
2021

2122
def fields_regex
2223
FIELD_REGEX
@@ -87,8 +88,11 @@ def count_matches(node)
8788
def format_method?(name, node)
8889
receiver, method_name, *args = *node
8990

90-
# commands have no explicit receiver
91-
return false unless !receiver && method_name == name
91+
if receiver && receiver.const_type?
92+
return false unless receiver.loc.name.is?(KERNEL)
93+
end
94+
95+
return false unless method_name == name
9296

9397
args.size > 1 && :str == args.first.type
9498
end

spec/rubocop/cop/lint/format_parameter_mismatch_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@
55
describe RuboCop::Cop::Lint::FormatParameterMismatch do
66
subject(:cop) { described_class.new }
77

8+
it 'registers an offense when calling Kernel.format ' \
9+
'and the fields do not match' do
10+
inspect_source(cop, 'Kernel.format("%s %s", 1)')
11+
expect(cop.offenses.size).to eq(1)
12+
13+
msg = ['Number arguments (1) to `format` mismatches expected fields (2).']
14+
expect(cop.messages).to eq(msg)
15+
end
16+
17+
it 'registers an offense when calling Kernel.sprintf ' \
18+
'and the fields do not match' do
19+
inspect_source(cop, 'Kernel.sprintf("%s %s", 1)')
20+
expect(cop.offenses.size).to eq(1)
21+
22+
msg = ['Number arguments (1) to `sprintf` mismatches expected fields (2).']
23+
expect(cop.messages).to eq(msg)
24+
end
25+
826
it 'registers an offense when there are less arguments than expected' do
927
inspect_source(cop, 'format("%s %s", 1)')
1028
expect(cop.offenses.size).to eq(1)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy