rubocop/rubocop-rails

Lint to forbid single-line comments in `.squish`ed SQL heredocs

Opened this issue · 0 comments

Is your feature request related to a problem? Please describe.

The Rails/SquishedSQLHeredocs lint requires SQL heredocs to be .squished, which, as the docs mention, can change the meaning of the statement, especially when it contains single-line comments. Consider the following snippet:

execute <<-SQL.squish
  DELETE FROM
  records a
      USING records b
  WHERE a.updated_at < b.updated_at -- keep most recently updated record
  AND a.field = b.field;
SQL

If you're aware of this lint, you're probably writing <<-SQL.squish by default, and might not think about how it would affect your SQL.

Describe the solution you'd like

I think there should be a lint to forbid single-line comments in squished SQL heredocs. There are three reasons why they might exist:

  • The author didn't consider the implications. In this case, the comment could easily cause a bug.
  • The author is aware of the fact that the comment will affect the following lines as well, and is fine with this. I would argue that this is obtuse, and a multi-line comment should be used instead.
  • It doesn't matter, for example when the comment is at the very end of an SQL snippet. In this case, I think it's fine to require a multi-line comment anyway.

I assume that the biggest challenge with implementing this would be parsing the SQL, especially considering the possibility of string interpolation.

Describe alternatives you've considered

Expect users not to make this mistake.