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 new Style/EmptyHeredoc cop #10820

Merged
merged 1 commit into from Jul 18, 2022

Conversation

koic
Copy link
Member

@koic koic commented Jul 16, 2022

Summary

Checks for using empty heredoc to reduce redundancy.

# bad
<<~EOS
EOS

<<-EOS
EOS

<<EOS
EOS

# good
''

# bad
do_something(<<~EOS)
EOS

do_something(<<-EOS)
EOS

do_something(<<EOS)
EOS

# good
do_something('')

Other Information

I noticed the following redundancy in the RuboCop test code:

expect_correction(<<~RUBY)
RUBY

This cop solves it generically.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

include RangeHelp
extend AutoCorrector

MSG = "Use `''` instead of heredoc."
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess that will be tied to whether the user selected single or double quoted string literals in their config. You may cheap by saying "an empty string literal".

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure! I've updated!

Copy link

Choose a reason for hiding this comment

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

Similarly, the autocorrector says corrector.replace(node, "''"). Though maybe the autocorrector will see the '' and swap it for "" because it just inserted a violation of a different rule. I'm not sure how the autocorrector works. Just, the code looks like it'll have the same problem as the MSG did.

Copy link
Member Author

Choose a reason for hiding this comment

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

That makes sense, I've updated it:
743f6f7

@koic koic force-pushed the add_new_style_empty_heredoc_cop branch 2 times, most recently from fb0d683 to 718bc35 Compare July 17, 2022 13:42
## Summary

Checks for using empty heredoc to reduce redundancy.

```ruby
# bad
<<~EOS
EOS

<<-EOS
EOS

<<EOS
EOS

# good
''

# bad
do_something(<<~EOS)
EOS

do_something(<<-EOS)
EOS

do_something(<<EOS)
EOS

# good
do_something('')
```

## Other Information

I noticed the following redundancy in the RuboCop test code:

```ruby
expect_correction(<<~RUBY)
RUBY
```

This cop solves it generically.
@koic koic force-pushed the add_new_style_empty_heredoc_cop branch from 718bc35 to b3ba8a3 Compare July 17, 2022 13:54
@bbatsov bbatsov merged commit 2c4215f into rubocop:master Jul 18, 2022
@koic koic deleted the add_new_style_empty_heredoc_cop branch July 18, 2022 14:26
koic added a commit that referenced this pull request Jul 22, 2022
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

3 participants