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

Add RSpec/IndexedLet cop #1615

Merged
merged 9 commits into from Apr 16, 2023
Merged

Add RSpec/IndexedLet cop #1615

merged 9 commits into from Apr 16, 2023

Conversation

DmitryTsepelev
Copy link
Contributor

@DmitryTsepelev DmitryTsepelev commented Apr 4, 2023

Adds the cop that tries to find indexed let statements (item_1, item_2, ...):

# bad
let(:item_1) { create(:item) }
let(:item_2) { create(:item) }

let(:item_1) { create(:item, visible: true) }
let(:item_2) { create(:item, visible: false) }

before do
  item_1 = create(:item)
  item_2 = create(:item)
end

# good
let(:items) { create_list(:item, 2) }

let(:visible_item) { create(:item, visible: true) }
let(:invisible_item) { create(:item, visible: false) }

before do
  create_list(:item, 2)
end

Before submitting the PR make sure the following are checked:

  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Updated documentation.
  • Added an entry to the CHANGELOG.md if the new code introduces user-observable changes.
  • The build (bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).

If you have created a new cop:

  • Added the new cop to config/default.yml.
  • The cop is configured as Enabled: pending in config/default.yml.
  • The cop is configured as Enabled: true in .rubocop.yml.
  • The cop documents examples of good and bad code.
  • The tests assert both that bad code is reported and that good code is not reported.
  • Set VersionAdded: "<<next>>" in default/config.yml.

If you have modified an existing cop's configuration options:

  • Set VersionChanged: "<<next>>" in config/default.yml.

@DmitryTsepelev
Copy link
Contributor Author

Not sure why that check fails, works fine locally

docs/modules/ROOT/pages/cops_rspec.adoc Outdated Show resolved Hide resolved
lib/rubocop/cop/rspec/indexed_let.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rspec/indexed_let.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rspec/indexed_let.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rspec/indexed_let.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rspec/indexed_let.rb Show resolved Hide resolved
config/default.yml Outdated Show resolved Hide resolved
spec/rubocop/cop/rspec/indexed_let_spec.rb Outdated Show resolved Hide resolved
spec/rubocop/cop/rspec/indexed_let_spec.rb Outdated Show resolved Hide resolved
config/default.yml Outdated Show resolved Hide resolved
@bquorning
Copy link
Collaborator

I see create_list and think this probably should be a FactoryBot cop: RSpec/FactoryBot/IndexedLet?

@pirj
Copy link
Member

pirj commented Apr 5, 2023

Not necessarily, create can be replaced with fixture(:...), and the cop would still inspect the same way.

@DmitryTsepelev
Copy link
Contributor Author

@pirj Addressed everything, ready for another round 🙂

@DmitryTsepelev DmitryTsepelev requested a review from pirj April 8, 2023 17:39
let(:max_repeats) { 1 }
let(:cop_config) { { 'MaxRepeats' => max_repeats } }

specify do
Copy link
Member

@ydah ydah Apr 9, 2023

Choose a reason for hiding this comment

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

It sometimes outputs like following, so it seems better to pass a description to it or specify like other specs.

RuboCop::Cop::RSpec::IndexedLet
  is expected to eq describe SomeService do
  let(:item_1) { create(:item) }
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This `let` ...ame. Please give it a meaningful names, use create_list or move creation to the `before` block.
end

@ydah
Copy link
Member

ydah commented Apr 9, 2023

I think it would be good to have the following as test cases as well.

describe SomeService do
  let(:item_1) { create(:item) }
  let(:foo_item_1) { create(:item) }
end

describe SomeService do
  let(:item_1) { create(:item) }
  let("item_2") { create(:item) }
end

@DmitryTsepelev DmitryTsepelev requested a review from ydah April 9, 2023 10:40
lib/rubocop/cop/rspec/indexed_let.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rspec/indexed_let.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rspec/indexed_let.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rspec/indexed_let.rb Outdated Show resolved Hide resolved
DmitryTsepelev and others added 5 commits April 10, 2023 00:32
Co-authored-by: Benjamin Quorning <22333+bquorning@users.noreply.github.com>
Co-authored-by: Benjamin Quorning <22333+bquorning@users.noreply.github.com>
Co-authored-by: Benjamin Quorning <22333+bquorning@users.noreply.github.com>
Co-authored-by: Benjamin Quorning <22333+bquorning@users.noreply.github.com>
@DmitryTsepelev
Copy link
Contributor Author

Applied all suggesstions, updated specs and docs, thanks 🙂

Copy link
Member

@ydah ydah left a comment

Choose a reason for hiding this comment

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

Thank you for doing a great work ❤️

Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

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

Thank you!
A few nitpicks below.

config/default.yml Outdated Show resolved Hide resolved
lib/rubocop/cop/rspec/indexed_let.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rspec/indexed_let.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rspec/indexed_let.rb Show resolved Hide resolved
@DmitryTsepelev
Copy link
Contributor Author

Updated 🙂

@pirj pirj merged commit db2e9cc into rubocop:master Apr 16, 2023
23 checks passed
@pirj
Copy link
Member

pirj commented Apr 16, 2023

Thank you, @DmitryTsepelev !

@bquorning
Copy link
Collaborator

Oops, it looks like we forgot to squash commits before merging 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants