Class: RuboCop::Cop::Rails::SquishedSQLHeredocs

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
Heredoc
Defined in:
lib/rubocop/cop/rails/squished_sql_heredocs.rb

Overview

Checks SQL heredocs to use ‘.squish`.

Examples:

# bad
<<-SQL
  SELECT * FROM posts;
SQL

<<-SQL
  SELECT * FROM posts
    WHERE id = 1
SQL

execute(<<~SQL, "Post Load")
  SELECT * FROM posts
    WHERE post_id = 1
SQL

# good
<<-SQL.squish
  SELECT * FROM posts;
SQL

<<~SQL.squish
  SELECT * FROM table
    WHERE id = 1
SQL

execute(<<~SQL.squish, "Post Load")
  SELECT * FROM posts
    WHERE post_id = 1
SQL

Constant Summary collapse

SQL =
'SQL'
SQUISH =
'.squish'
MSG =
'Use `%<expect>s` instead of `%<current>s`.'
SQL_IDENTIFIER_MARKERS =
/(".+?")|('.+?')|(\[.+?\])/.freeze

Instance Method Summary collapse

Instance Method Details

#on_heredoc(node) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/rubocop/cop/rails/squished_sql_heredocs.rb', line 52

def on_heredoc(node)
  return unless offense_detected?(node)

  add_offense(node) do |corrector|
    corrector.insert_after(node, SQUISH)
  end
end