rubocop / rails-style-guide

A community-driven Ruby on Rails style guide
http://rails.rubystyle.guide
6.48k stars 1.06k forks source link

Fix code in the "Prefer using squiggly heredoc over strip_heredoc" section #352

Closed ydakuka closed 1 year ago

ydakuka commented 1 year ago

The original example is invalid, so I fixed it.

andyw8 commented 1 year ago

What makes you think it's invalid?

% bin/rails c
Loading development environment (Rails 7.0.8)
irb(main):001" <<EOS.strip_heredoc
irb(main):002"   some text
irb(main):003> EOS
=> "some text\n"
ydakuka commented 1 year ago

I have the following code:

class User < ApplicationRecord
  def my_method
    <<EOS.strip_heredoc
      some text
    EOS
  end
end

Terminal:

ydakuka@yauhenid:~/Work/project$ bin/rails_docker rails c

Traceback (most recent call last):
  73: from /usr/local/bin/rails:23:in `<main>'
  72: from /usr/local/bin/rails:23:in `load'
  71: from /usr/local/lib/ruby/gems/2.7.0/gems/railties-6.0.6.1/exe/rails:10:in `<top (required)>'
  70: from /usr/local/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:83:in `require'
  69: from /usr/local/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:83:in `require'
  68: from /usr/local/lib/ruby/gems/2.7.0/gems/railties-6.0.6.1/lib/rails/cli.rb:7:in `<top (required)>'
  67: from /usr/local/lib/ruby/gems/2.7.0/gems/railties-6.0.6.1/lib/rails/app_loader.rb:48:in `exec_app'
  66: from /usr/local/lib/ruby/gems/2.7.0/gems/railties-6.0.6.1/lib/rails/app_loader.rb:48:in `loop'
  65: from /usr/local/lib/ruby/gems/2.7.0/gems/railties-6.0.6.1/lib/rails/app_loader.rb:59:in `block in exec_app'
  64: from /usr/local/lib/ruby/gems/2.7.0/gems/bootsnap-1.15.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
  63: from /usr/local/lib/ruby/gems/2.7.0/gems/bootsnap-1.15.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
  62: from /usr/local/lib/ruby/gems/2.7.0/gems/railties-6.0.6.1/lib/rails/commands.rb:18:in `<main>'
  61: from /usr/local/lib/ruby/gems/2.7.0/gems/railties-6.0.6.1/lib/rails/command.rb:46:in `invoke'
  60: from /usr/local/lib/ruby/gems/2.7.0/gems/railties-6.0.6.1/lib/rails/command/base.rb:69:in `perform'
  59: from /usr/local/lib/ruby/gems/2.7.0/gems/thor-1.2.1/lib/thor.rb:392:in `dispatch'
  58: from /usr/local/lib/ruby/gems/2.7.0/gems/thor-1.2.1/lib/thor/invocation.rb:127:in `invoke_command'
  57: from /usr/local/lib/ruby/gems/2.7.0/gems/thor-1.2.1/lib/thor/command.rb:27:in `run'
  56: from /usr/local/lib/ruby/gems/2.7.0/gems/railties-6.0.6.1/lib/rails/commands/console/console_command.rb:101:in `perform'
  55: from /usr/local/lib/ruby/gems/2.7.0/gems/railties-6.0.6.1/lib/rails/command/actions.rb:15:in `require_application_and_environment!'
  54: from /usr/local/lib/ruby/gems/2.7.0/gems/railties-6.0.6.1/lib/rails/command/actions.rb:28:in `require_environment!'
  53: from /usr/local/lib/ruby/gems/2.7.0/gems/railties-6.0.6.1/lib/rails/application.rb:339:in `require_environment!'
  52: from /usr/local/lib/ruby/gems/2.7.0/gems/activesupport-6.0.6.1/lib/active_support/dependencies.rb:324:in `require'
  51: from /usr/local/lib/ruby/gems/2.7.0/gems/activesupport-6.0.6.1/lib/active_support/dependencies.rb:291:in `load_dependency'
  50: from /usr/local/lib/ruby/gems/2.7.0/gems/activesupport-6.0.6.1/lib/active_support/dependencies.rb:324:in `block in require'
  49: from /usr/local/lib/ruby/gems/2.7.0/gems/zeitwerk-2.6.6/lib/zeitwerk/kernel.rb:38:in `require'
  48: from /usr/local/lib/ruby/gems/2.7.0/gems/bootsnap-1.15.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
  47: from /usr/local/lib/ruby/gems/2.7.0/gems/bootsnap-1.15.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
  46: from /app/config/environment.rb:7:in `<main>'
  45: from /usr/local/lib/ruby/gems/2.7.0/gems/railties-6.0.6.1/lib/rails/railtie.rb:190:in `method_missing'
  44: from /usr/local/lib/ruby/gems/2.7.0/gems/railties-6.0.6.1/lib/rails/railtie.rb:190:in `public_send'
  43: from /usr/local/lib/ruby/gems/2.7.0/gems/railties-6.0.6.1/lib/rails/application.rb:363:in `initialize!'
  42: from /usr/local/lib/ruby/gems/2.7.0/gems/railties-6.0.6.1/lib/rails/initializable.rb:60:in `run_initializers'
  41: from /usr/local/lib/ruby/2.7.0/tsort.rb:205:in `tsort_each'
  40: from /usr/local/lib/ruby/2.7.0/tsort.rb:226:in `tsort_each'
  39: from /usr/local/lib/ruby/2.7.0/tsort.rb:347:in `each_strongly_connected_component'
  38: from /usr/local/lib/ruby/2.7.0/tsort.rb:347:in `call'
  37: from /usr/local/lib/ruby/2.7.0/tsort.rb:347:in `each'
  36: from /usr/local/lib/ruby/2.7.0/tsort.rb:349:in `block in each_strongly_connected_component'
  35: from /usr/local/lib/ruby/2.7.0/tsort.rb:431:in `each_strongly_connected_component_from'
  34: from /usr/local/lib/ruby/2.7.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
  33: from /usr/local/lib/ruby/2.7.0/tsort.rb:228:in `block in tsort_each'
  32: from /usr/local/lib/ruby/gems/2.7.0/gems/railties-6.0.6.1/lib/rails/initializable.rb:61:in `block in run_initializers'
  31: from /usr/local/lib/ruby/gems/2.7.0/gems/railties-6.0.6.1/lib/rails/initializable.rb:32:in `run'
  30: from /usr/local/lib/ruby/gems/2.7.0/gems/railties-6.0.6.1/lib/rails/initializable.rb:32:in `instance_exec'
  29: from /usr/local/lib/ruby/gems/2.7.0/gems/railties-6.0.6.1/lib/rails/application/finisher.rb:129:in `block in <module:Finisher>'
  28: from /usr/local/lib/ruby/gems/2.7.0/gems/activesupport-6.0.6.1/lib/active_support/lazy_load_hooks.rb:51:in `run_load_hooks'
  27: from /usr/local/lib/ruby/gems/2.7.0/gems/activesupport-6.0.6.1/lib/active_support/lazy_load_hooks.rb:51:in `each'
  26: from /usr/local/lib/ruby/gems/2.7.0/gems/activesupport-6.0.6.1/lib/active_support/lazy_load_hooks.rb:52:in `block in run_load_hooks'
  25: from /usr/local/lib/ruby/gems/2.7.0/gems/activesupport-6.0.6.1/lib/active_support/lazy_load_hooks.rb:66:in `execute_hook'
  24: from /usr/local/lib/ruby/gems/2.7.0/gems/activesupport-6.0.6.1/lib/active_support/lazy_load_hooks.rb:61:in `with_execution_control'
  23: from /usr/local/lib/ruby/gems/2.7.0/gems/activesupport-6.0.6.1/lib/active_support/lazy_load_hooks.rb:68:in `block in execute_hook'
  22: from /usr/local/lib/ruby/gems/2.7.0/gems/factory_bot_rails-6.2.0/lib/factory_bot_rails/railtie.rb:22:in `block in <class:Railtie>'
  21: from /usr/local/lib/ruby/gems/2.7.0/gems/factory_bot-6.2.1/lib/factory_bot/find_definitions.rb:15:in `find_definitions'
  20: from /usr/local/lib/ruby/gems/2.7.0/gems/factory_bot-6.2.1/lib/factory_bot/find_definitions.rb:15:in `each'
  19: from /usr/local/lib/ruby/gems/2.7.0/gems/factory_bot-6.2.1/lib/factory_bot/find_definitions.rb:19:in `block in find_definitions'
  18: from /usr/local/lib/ruby/gems/2.7.0/gems/factory_bot-6.2.1/lib/factory_bot/find_definitions.rb:19:in `each'
  17: from /usr/local/lib/ruby/gems/2.7.0/gems/factory_bot-6.2.1/lib/factory_bot/find_definitions.rb:20:in `block (2 levels) in find_definitions'
  16: from /usr/local/lib/ruby/gems/2.7.0/gems/factory_bot-6.2.1/lib/factory_bot/find_definitions.rb:20:in `load'
  15: from /app/spec/factories/service_accounts.rb:3:in `<main>'
  14: from /usr/local/lib/ruby/gems/2.7.0/gems/factory_bot-6.2.1/lib/factory_bot/syntax/default.rb:7:in `define'
  13: from /usr/local/lib/ruby/gems/2.7.0/gems/factory_bot-6.2.1/lib/factory_bot/syntax/default.rb:37:in `run'
  12: from /usr/local/lib/ruby/gems/2.7.0/gems/factory_bot-6.2.1/lib/factory_bot/syntax/default.rb:37:in `instance_eval'
  11: from /app/spec/factories/service_accounts.rb:4:in `block in <main>'
  10: from /usr/local/lib/ruby/gems/2.7.0/gems/factory_bot-6.2.1/lib/factory_bot/syntax/default.rb:18:in `factory'
   9: from /usr/local/lib/ruby/gems/2.7.0/gems/factory_bot-6.2.1/lib/factory_bot/syntax/default.rb:18:in `instance_eval'
   8: from /app/spec/factories/service_accounts.rb:5:in `block (2 levels) in <main>'
   7: from /usr/local/lib/ruby/gems/2.7.0/gems/zeitwerk-2.6.6/lib/zeitwerk/kernel.rb:30:in `require'
   6: from /usr/local/lib/ruby/gems/2.7.0/gems/bootsnap-1.15.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
   5: from /usr/local/lib/ruby/gems/2.7.0/gems/bootsnap-1.15.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
   4: from /app/app/models/account.rb:3:in `<main>'
   3: from /app/app/models/account.rb:13:in `<module:Account>'
   2: from /usr/local/lib/ruby/gems/2.7.0/gems/zeitwerk-2.6.6/lib/zeitwerk/kernel.rb:30:in `require'
   1: from /usr/local/lib/ruby/gems/2.7.0/gems/bootsnap-1.15.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
/usr/local/lib/ruby/gems/2.7.0/gems/bootsnap-1.15.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require': /app/app/models/user.rb:5: can't find string "EOS" anywhere before EOF (SyntaxError)
/app/app/models/user.rb:5: syntax error, unexpected end-of-input, expecting `end'

or

ydakuka@yauhenid:~/Work/project$ bin/rails_docker rubocop app/models/user.rb 
Inspecting 1 file
F

Offenses:

app/models/user.rb:5:5: F: Lint/Syntax: unterminated string meets end of file
(Using Ruby 2.7 parser; configure using TargetRubyVersion parameter, under AllCops)
    <<EOS.strip_heredoc
    ^

1 file inspected, 1 offense detected

However, I have no problem with the corrected version.

andyw8 commented 1 year ago

The plain << syntax doesn't allow whitespace in front of the herdoc marker, so it would need to be written as:

class User < ApplicationRecord
  def my_method
    <<EOS.strip_heredoc
      some text
EOS
  end
end
ydakuka commented 1 year ago

I see, thanks for the explanation. I'll close the PR.