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

Scattered let autocorrect #903

Merged
merged 2 commits into from Apr 29, 2020
Merged

Scattered let autocorrect #903

merged 2 commits into from Apr 29, 2020

Conversation

Darhazer
Copy link
Member

@Darhazer Darhazer commented Apr 28, 2020

I realized that we don't have an autocorrect support for scattered let, but it's quite easy to do, as we already move nodes around.

I also refactored the autocorrect part, as I had to copy/paste 3 methods one more time, and we had enough copies already.

It seems that rubocop-rspec deals with the structure much more than rubocop itself - I found only one cop there that would benefit from the new helper, that's why I left it as part of rubocop-rspec instead.


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 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 documents examples of good and bad code.
  • The tests assert both that bad code is reported and that good code is not reported.

@Darhazer Darhazer requested review from bquorning and pirj April 28, 2020 12:57
@bquorning
Copy link
Collaborator

@Darhazer FYI the build failed.

Copy link
Contributor

@jonatas jonatas left a comment

Choose a reason for hiding this comment

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

Very cool @Darhazer ! I was thinking that maybe we could inherit Layout/ClassStructure and provide some RSpec/Structure with the defaults in the proper order. I remember when we opted by swapping the nodes and it would automatically adjust the spacing because the nodes do not hold the spaces.

@Darhazer
Copy link
Member Author

@bquorning fixed. Sorry, I wanted to share the code with a colleague and forgot to actually run the rake task

Copy link
Collaborator

@bquorning bquorning left a comment

Choose a reason for hiding this comment

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

Awesome, thanks @Darhazer

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.

MoveNode is incredible!

corrector.remove(node_range_with_surrounding_space(node))
RuboCop::RSpec::Corrector::MoveNode.new(
node, corrector, processed_source
).move_before(first_example)
Copy link
Member

Choose a reason for hiding this comment

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

👏

spec/rubocop/cop/rspec/scattered_let_spec.rb Outdated Show resolved Hide resolved
@@ -12,6 +12,76 @@
^^^^^^^^^^^^^ Group all let/let! blocks in the example group together.
end
RUBY

expect_correction(<<-RUBY)
RSpec.describe User do
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to stress on RSpec?


end
end
RUBY
end
Copy link
Member

Choose a reason for hiding this comment

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

Just in case, maybe add an example for let!?

@bquorning
Copy link
Collaborator

Possible conflict with #904 – or at least there’s an extra test case to autocorrect.

@Darhazer
Copy link
Member Author

Yup I'll update with the test case from #904 and with the improvements that @pirj suggested

@Darhazer
Copy link
Member Author

Updated

@bquorning bquorning merged commit 0f7721e into master Apr 29, 2020
@bquorning bquorning deleted the scattered-let-autocorrect branch April 29, 2020 22:36
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