From f31b26862a73f2489f648e4b62a2e4a7cb774fc0 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Mon, 26 Aug 2024 21:33:18 +0200 Subject: [PATCH] Improve offense message for `RSpec/IndexedLet` Ran into this offense and thought: > Where exactly does my name contain "index"? I believe this message format is more clear. --- CHANGELOG.md | 1 + lib/rubocop/cop/rspec/indexed_let.rb | 7 +++--- spec/rubocop/cli/run_spec.rb | 8 +++---- spec/rubocop/cop/rspec/indexed_let_spec.rb | 26 +++++++++++----------- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3266b77bb..201f6b6b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/rubocop/cop/rspec/indexed_let.rb b/lib/rubocop/cop/rspec/indexed_let.rb index 546d56d3e..dab5d0e97 100644 --- a/lib/rubocop/cop/rspec/indexed_let.rb +++ b/lib/rubocop/cop/rspec/indexed_let.rb @@ -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 `%s` in its name. ' \ + 'Please give it a meaningful name.' # @!method let_name(node) def_node_matcher :let_name, <<~PATTERN @@ -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 diff --git a/spec/rubocop/cli/run_spec.rb b/spec/rubocop/cli/run_spec.rb index b8ae33670..4b0f6ba7b 100644 --- a/spec/rubocop/cli/run_spec.rb +++ b/spec/rubocop/cli/run_spec.rb @@ -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 @@ -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 diff --git a/spec/rubocop/cop/rspec/indexed_let_spec.rb b/spec/rubocop/cop/rspec/indexed_let_spec.rb index deff26c93..9f6cdbf4f 100644 --- a/spec/rubocop/cop/rspec/indexed_let_spec.rb +++ b/spec/rubocop/cop/rspec/indexed_let_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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