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

Improve offense message for RSpec/IndexedLet #1953

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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. ' \
Copy link
Member

Choose a reason for hiding this comment

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

Should we keep the work “index” eg “uses 3 index in its name”?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

index doesn't seem like the right word to use. I know the cop is called IndexedLet but I wonder if that is a misnomer, NumberedLet (or similar) seems more accurate to what it does.

Personally I would leave it as is but overall not that important to me if you want it changed. Let me know.

'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