Skip to content

Commit

Permalink
Merge pull request #1953 from Earlopain/offense-message-indexed-let
Browse files Browse the repository at this point in the history
Improve offense message for `RSpec/IndexedLet`
  • Loading branch information
pirj authored Aug 27, 2024
2 parents e02576f + f31b268 commit 74d7837
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Master (Unreleased)

- Fix false-negative and error for `RSpec/MetadataStyle` when non-literal args are used in metadata in `EnforceStyle: hash`. ([@cbliard])
- Improve offense message for `RSpec/IndexedLet`. ([@earlopain])

## 3.0.4 (2024-08-05)

Expand Down
7 changes: 4 additions & 3 deletions lib/rubocop/cop/rspec/indexed_let.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class IndexedLet < Base
include AllowedIdentifiers
include AllowedPattern

MSG = 'This `let` statement uses index in its name. Please give it ' \
'a meaningful name.'
MSG = 'This `let` statement uses `%<index>s` in its name. ' \
'Please give it a meaningful name.'

# @!method let_name(node)
def_node_matcher :let_name, <<~PATTERN
Expand All @@ -66,7 +66,8 @@ def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
return unless children

filter_indexed_lets(children).each do |let_node|
add_offense(let_node)
index = let_name(let_node)[INDEX_REGEX]
add_offense(let_node, message: format(MSG, index: index))
end
end

Expand Down
8 changes: 4 additions & 4 deletions spec/rubocop/cli/run_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
== spec/example.rb ==
C: 2: 7: Naming/VariableNumber: Use normalcase for symbol numbers.
C: 3: 7: Naming/VariableNumber: Use normalcase for symbol numbers.
C: 6: 3: RSpec/IndexedLet: This let statement uses index in its name. Please give it a meaningful name.
C: 6: 3: RSpec/IndexedLet: This let statement uses 1 in its name. Please give it a meaningful name.
C: 6: 7: Naming/VariableNumber: Use normalcase for symbol numbers.
C: 7: 3: RSpec/IndexedLet: This let statement uses index in its name. Please give it a meaningful name.
C: 7: 3: RSpec/IndexedLet: This let statement uses 2 in its name. Please give it a meaningful name.
C: 7: 7: Naming/VariableNumber: Use normalcase for symbol numbers.
1 file inspected, 6 offenses detected
Expand Down Expand Up @@ -82,9 +82,9 @@
== spec/example.rb ==
C: 2: 7: Naming/VariableNumber: Use normalcase for symbol numbers.
C: 3: 7: Naming/VariableNumber: Use normalcase for symbol numbers.
C: 6: 3: RSpec/IndexedLet: This let statement uses index in its name. Please give it a meaningful name.
C: 6: 3: RSpec/IndexedLet: This let statement uses 1 in its name. Please give it a meaningful name.
C: 6: 7: Naming/VariableNumber: Use normalcase for symbol numbers.
C: 7: 3: RSpec/IndexedLet: This let statement uses index in its name. Please give it a meaningful name.
C: 7: 3: RSpec/IndexedLet: This let statement uses 2 in its name. Please give it a meaningful name.
C: 7: 7: Naming/VariableNumber: Use normalcase for symbol numbers.
1 file inspected, 6 offenses detected
Expand Down
26 changes: 13 additions & 13 deletions spec/rubocop/cop/rspec/indexed_let_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
expect_offense(<<~RUBY)
describe SomeService do
let(:item_1) { create(:item) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This `let` statement uses index in its name. Please give it a meaningful name.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This `let` statement uses `1` in its name. Please give it a meaningful name.
let(:item_2) { create(:item) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This `let` statement uses index in its name. Please give it a meaningful name.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This `let` statement uses `2` in its name. Please give it a meaningful name.
end
RUBY
end
Expand All @@ -27,9 +27,9 @@
expect_offense(<<~RUBY)
describe SomeService do
let("item_1") { create(:item) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This `let` statement uses index in its name. Please give it a meaningful name.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This `let` statement uses `1` in its name. Please give it a meaningful name.
let("item_2") { create(:item) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This `let` statement uses index in its name. Please give it a meaningful name.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This `let` statement uses `2` in its name. Please give it a meaningful name.
end
RUBY
end
Expand All @@ -38,9 +38,9 @@
expect_offense(<<~RUBY)
describe SomeService do
let(:item1) { create(:item) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This `let` statement uses index in its name. Please give it a meaningful name.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This `let` statement uses `1` in its name. Please give it a meaningful name.
let(:item2) { create(:item) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This `let` statement uses index in its name. Please give it a meaningful name.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This `let` statement uses `2` in its name. Please give it a meaningful name.
end
RUBY
end
Expand All @@ -49,9 +49,9 @@
expect_offense(<<~RUBY)
describe SomeService do
let(:item_1, &block)
^^^^^^^^^^^^^^^^^^^^ This `let` statement uses index in its name. Please give it a meaningful name.
^^^^^^^^^^^^^^^^^^^^ This `let` statement uses `1` in its name. Please give it a meaningful name.
let(:item_2, &block)
^^^^^^^^^^^^^^^^^^^^ This `let` statement uses index in its name. Please give it a meaningful name.
^^^^^^^^^^^^^^^^^^^^ This `let` statement uses `2` in its name. Please give it a meaningful name.
end
RUBY
end
Expand All @@ -72,11 +72,11 @@
expect_offense(<<~RUBY)
context SomeService do
let(:user_1_item_1) { create(:item) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This `let` statement uses index in its name. Please give it a meaningful name.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This `let` statement uses `1` in its name. Please give it a meaningful name.
let(:user_1_item_2) { create(:item) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This `let` statement uses index in its name. Please give it a meaningful name.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This `let` statement uses `1` in its name. Please give it a meaningful name.
let(:user_2_item_1) { create(:item) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This `let` statement uses index in its name. Please give it a meaningful name.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This `let` statement uses `2` in its name. Please give it a meaningful name.
end
RUBY
end
Expand Down Expand Up @@ -112,9 +112,9 @@
expect_offense(<<~RUBY)
describe SomeService do
let(:item_1) { create(:item) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This `let` statement uses index in its name. Please give it a meaningful name.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This `let` statement uses `1` in its name. Please give it a meaningful name.
let("item_2") { create(:item) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This `let` statement uses index in its name. Please give it a meaningful name.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This `let` statement uses `2` in its name. Please give it a meaningful name.
end
RUBY
end
Expand Down

0 comments on commit 74d7837

Please sign in to comment.