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

[Fix #704] Add new Rails/StripHeredoc cop #706

Merged
merged 1 commit into from Jun 12, 2022

Conversation

koic
Copy link
Member

@koic koic commented May 27, 2022

Closes #704.

Enforces the use of squiggly heredoc over strip_heredoc.

# bad
<<EOS.strip_heredoc
  some text
EOS

# bad
<<-EOS.strip_heredoc
  some text
EOS

# good
<<~EOS
  some text
EOS

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.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@koic koic force-pushed the add_new_rails_strip_heredoc_cop branch 3 times, most recently from c3a9c0c to c64a2a5 Compare May 27, 2022 17:59
@koic koic force-pushed the add_new_rails_strip_heredoc_cop branch from c64a2a5 to 85f75fd Compare May 28, 2022 07:54
koic added a commit to koic/rails-style-guide that referenced this pull request May 28, 2022
Follow up rubocop/rubocop-rails#706.

This PR adds "Prefer using squiggly heredoc over `strip_heredoc`" rule.
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.

Thanks!

Checked on a real-world repo:

$ rubocop --only Rails/StripHeredoc /Users/pirj/source/real-world-rspec/
57497 files inspected, 258 offenses detected, 258 offenses autocorrectable

All offences look legitimate.

@koic koic force-pushed the add_new_rails_strip_heredoc_cop branch from 85f75fd to d8e0511 Compare May 28, 2022 22:23
minimum_target_ruby_version 2.3

def on_send(node)
return unless (receiver = node.receiver)
Copy link
Member

Choose a reason for hiding this comment

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

Even though there's some stability in RESTRICT_ON_SEND, accoring to:

The on_send callback is the most used and can be optimized by restricting the acceptable method names with a constant RESTRICT_ON_SEND.

Do you think it makes sense to keep this line just to be on the safe side?

return unless node.method?(:strip_heredoc)

Copy link
Member

Choose a reason for hiding this comment

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

My suggestion might be overly paranoid.
I can't tell the exact share, but some cops seem to have this duplication, even when there's just one short method name:

https://github.com/rubocop/rubocop/blob/cdf0d4b9abbbefdff63aab9c87c74b20bd8ed8af/lib/rubocop/cop/style/array_join.rb#L27
lib/rubocop/cop/style/float_division.rb

I'm not aware of any practice regarding this. Please don't hesitate to ignore the above suggestion completely as you see fit.

Copy link
Member Author

@koic koic May 30, 2022

Choose a reason for hiding this comment

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

AFAIK, method? may be redundant with RESTRICT_ON_SEND because it only checks a method name. So, it is different from the def_node_matcher that checks other than a method name.

Closes rubocop#704.

Enforces the use of squiggly heredoc over `strip_heredoc`.

```ruby
# bad
<<EOS.strip_heredoc
  some text
EOS

# bad
<<-EOS.strip_heredoc
  some text
EOS

# good
<<~EOS
  some text
EOS
```
@koic koic force-pushed the add_new_rails_strip_heredoc_cop branch from d8e0511 to 25c2e6b Compare May 30, 2022 01:52
@koic koic merged commit e0d2a47 into rubocop:master Jun 12, 2022
@koic koic deleted the add_new_rails_strip_heredoc_cop branch June 12, 2022 08:17
Worldboss-tech added a commit to Worldboss-tech/rails-style-guide that referenced this pull request Aug 15, 2023
Follow up rubocop/rubocop-rails#706.

This PR adds "Prefer using squiggly heredoc over `strip_heredoc`" rule.
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.

Cop idea: replace #strip_heredoc with squiggly heredoc in heredoc declarations
3 participants