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

Good practice - Heredocs in hash #871

Closed
HS-157 opened this issue Mar 25, 2021 · 2 comments
Closed

Good practice - Heredocs in hash #871

HS-157 opened this issue Mar 25, 2021 · 2 comments

Comments

@HS-157
Copy link

HS-157 commented Mar 25, 2021

Huhu,

I have a multi-line string and I use a heredoc in hash for a cookbook Chef. For check style, I use Cookstyle as linting tool, it raise Style/TrailingCommaInHashLiteral as warning cops.

I would like to know if this is a good practice add comma after first line delimiter when a heredoc use in a hash like add a parenthesis when it's use as argument in a function.

Thank you.

Example

Cookstyle warning

a = {
  toto: 'toto',
  tutu: 'tutu',
  titi: <<~EOS
  Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    Nam euismod erat eget lorem dignissim ornare.
  Ut pellentesque nisi nec ipsum sagittis volutpat vel vitae lorem.
  EOS
}
hs-157@cassiopeia> cookstyle test.rb
Inspecting 1 file
C

Offenses:

test.rb:4:3: C: [Correctable] Style/TrailingCommaInHashLiteral: Put a comma after the last item of a multiline hash.
  titi: <<~EOS
  ^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense auto-correctable

Cookstyle OK

a = {
  toto: 'toto',
  tutu: 'tutu',
  titi: <<~EOS,
  Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    Nam euismod erat eget lorem dignissim ornare.
  Ut pellentesque nisi nec ipsum sagittis volutpat vel vitae lorem.
  EOS
}
hs-157@cassiopeia> cookstyle test.rb
Inspecting 1 file
.

1 file inspected, no offenses detected
@pirj
Copy link
Member

pirj commented Mar 25, 2021

But why if it's also a last element?

a = {
  toto: 'toto',
  titi: <<~EOS
  Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    Nam euismod erat eget lorem dignissim ornare.
  Ut pellentesque nisi nec ipsum sagittis volutpat vel vitae lorem.
  EOS,
  tutu: 'tutu'
 }

Would that cop flag this code and suggest changing to this?

a = {
  toto: 'toto',
-  titi: <<~EOS
+  titi: <<~EOS,
  Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    Nam euismod erat eget lorem dignissim ornare.
  Ut pellentesque nisi nec ipsum sagittis volutpat vel vitae lorem.
-  EOS,
+  EOS
  tutu: 'tutu'
 }

@pirj
Copy link
Member

pirj commented Jul 27, 2021

Closing as stale

@pirj pirj closed this as completed Jul 27, 2021
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

No branches or pull requests

2 participants