Sha256: 67be763ff6dc4ece9234fe6c12b3c89b669fd5b4fce5c1c089ce8d651f067b14

Contents?: true

Size: 1.83 KB

Versions: 13

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      #
      # Checks SQL heredocs to use `.squish`.
      # Some SQL syntax (e.g. PostgreSQL comments and functions) requires newlines
      # to be preserved in order to work, thus auto-correction for this cop is not safe.
      #
      # @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 < Base
        include Heredoc
        extend AutoCorrector

        SQL = 'SQL'
        SQUISH = '.squish'
        MSG = 'Use `%<expect>s` instead of `%<current>s`.'

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

          add_offense(node) 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

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
rubocop-rails-2.12.4 lib/rubocop/cop/rails/squished_sql_heredocs.rb
rubocop-rails-2.12.3 lib/rubocop/cop/rails/squished_sql_heredocs.rb
rubocop-rails-2.12.2 lib/rubocop/cop/rails/squished_sql_heredocs.rb
rubocop-rails-2.12.1 lib/rubocop/cop/rails/squished_sql_heredocs.rb
rubocop-rails-2.12.0 lib/rubocop/cop/rails/squished_sql_heredocs.rb
rubocop-rails-2.11.3 lib/rubocop/cop/rails/squished_sql_heredocs.rb
rubocop-rails-2.11.2 lib/rubocop/cop/rails/squished_sql_heredocs.rb
rubocop-rails-2.11.1 lib/rubocop/cop/rails/squished_sql_heredocs.rb
rubocop-rails-2.11.0 lib/rubocop/cop/rails/squished_sql_heredocs.rb
rubocop-rails-2.10.1 lib/rubocop/cop/rails/squished_sql_heredocs.rb
rubocop-rails-2.10.0 lib/rubocop/cop/rails/squished_sql_heredocs.rb
rubocop-rails-2.9.1 lib/rubocop/cop/rails/squished_sql_heredocs.rb
rubocop-rails-2.9.0 lib/rubocop/cop/rails/squished_sql_heredocs.rb