-
-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #291 from mobilutz/ll-squished-heredocs
Add new `Rails/SquishedSQLHeredocs` cop
- Loading branch information
Showing
7 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# | ||
# Checks SQL heredocs to use `.squish`. | ||
# | ||
# @example | ||
# # 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 | ||
# | ||
class SquishedSQLHeredocs < Cop | ||
include Heredoc | ||
|
||
SQL = 'SQL' | ||
SQUISH = '.squish' | ||
MSG = 'Use `%<expect>s` instead of `%<current>s`.' | ||
|
||
def on_heredoc(node) | ||
return unless offense_detected?(node) | ||
|
||
add_offense(node) | ||
end | ||
|
||
def autocorrect(node) | ||
lambda do |corrector| | ||
corrector.insert_after(node, SQUISH) | ||
end | ||
end | ||
|
||
private | ||
|
||
def offense_detected?(node) | ||
sql_heredoc?(node) && !using_squish?(node) | ||
end | ||
|
||
def sql_heredoc?(node) | ||
delimiter_string(node) == SQL | ||
end | ||
|
||
def using_squish?(node) | ||
node.parent&.send_type? && node.parent&.method?(:squish) | ||
end | ||
|
||
def message(node) | ||
format( | ||
MSG, | ||
expect: "#{node.source}#{SQUISH}", | ||
current: node.source | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::SquishedSQLHeredocs, :config do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
context 'with multi line heredoc' do | ||
it 'registers an offense and corrects it' do | ||
expect_offense(<<~RUBY) | ||
<<~SQL | ||
^^^^^^ Use `<<~SQL.squish` instead of `<<~SQL`. | ||
SELECT * FROM posts | ||
WHERE id = 1 | ||
SQL | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
<<~SQL.squish | ||
SELECT * FROM posts | ||
WHERE id = 1 | ||
SQL | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense' do | ||
expect_no_offenses(<<~RUBY) | ||
<<-SQL.squish | ||
SELECT * FROM posts | ||
WHERE id = 1 | ||
SQL | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with single line heredoc' do | ||
it 'registers an offense and corrects it' do | ||
expect_offense(<<~RUBY) | ||
<<-SQL | ||
^^^^^^ Use `<<-SQL.squish` instead of `<<-SQL`. | ||
SELECT * FROM posts; | ||
SQL | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
<<-SQL.squish | ||
SELECT * FROM posts; | ||
SQL | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense' do | ||
expect_no_offenses(<<~RUBY) | ||
<<-SQL.squish | ||
SELECT * FROM posts; | ||
SQL | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with heredocs as method parameters' do | ||
it 'registers an offense and corrects it' do | ||
expect_offense(<<~RUBY) | ||
execute(<<~SQL, "Post Load") | ||
^^^^^^ Use `<<~SQL.squish` instead of `<<~SQL`. | ||
SELECT * FROM posts | ||
WHERE post_id = 1 | ||
SQL | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
execute(<<~SQL.squish, "Post Load") | ||
SELECT * FROM posts | ||
WHERE post_id = 1 | ||
SQL | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense' do | ||
expect_no_offenses(<<~RUBY) | ||
execute(<<~SQL.squish, "Post Load") | ||
SELECT * FROM posts | ||
WHERE post_id = 1 | ||
SQL | ||
RUBY | ||
end | ||
end | ||
end |