Sha256: 276ae33972ade74eb0f369b824c7e8c24a57a0fd2d3efeb5304be2912c2c4486

Contents?: true

Size: 1.59 KB

Versions: 171

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for redundant heredoc delimiter quotes.
      #
      # @example
      #
      #   # bad
      #   do_something(<<~'EOS')
      #     no string interpolation style text
      #   EOS
      #
      #   # good
      #   do_something(<<~EOS)
      #     no string interpolation style text
      #   EOS
      #
      #   do_something(<<~'EOS')
      #     #{string_interpolation_style_text_not_evaluated}
      #   EOS
      #
      #   do_something(<<~'EOS')
      #     Preserve \
      #     newlines
      #   EOS
      #
      class RedundantHeredocDelimiterQuotes < Base
        include Heredoc
        extend AutoCorrector

        MSG = 'Remove the redundant heredoc delimiter quotes, use `%<replacement>s` instead.'
        STRING_INTERPOLATION_OR_ESCAPED_CHARACTER_PATTERN = /#(\{|@|\$)|\\/.freeze

        def on_heredoc(node)
          return if need_heredoc_delimiter_quotes?(node)

          replacement = "#{heredoc_type(node)}#{delimiter_string(node)}"

          add_offense(node, message: format(MSG, replacement: replacement)) do |corrector|
            corrector.replace(node, replacement)
          end
        end

        private

        def need_heredoc_delimiter_quotes?(node)
          heredoc_delimiter = node.source.delete(heredoc_type(node))
          return true unless heredoc_delimiter.start_with?("'", '"')

          node.loc.heredoc_end.source.strip.match?(/\W/) ||
            node.loc.heredoc_body.source.match?(STRING_INTERPOLATION_OR_ESCAPED_CHARACTER_PATTERN)
        end
      end
    end
  end
end

Version data entries

171 entries across 170 versions & 19 rubygems

Version Path
rubocop-1.74.0 lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb
rubocop-1.73.2 lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb
siteimprove_api_client-1.0.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.73.1/lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb
rubocop-1.73.1 lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb
rubocop-1.73.0 lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb
rubocop-1.72.2 lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb
rubocop-1.72.1 lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb
rubocop-1.72.0 lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb
rubocop-1.71.2 lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb
tailscale_middleware-0.0.3 vendor/cache/ruby/3.4.0/gems/rubocop-1.71.1/lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb
rubocop-1.71.1 lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb
rubocop-1.71.0 lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb
rubocop-1.70.0 lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb
rubocop-1.69.2 lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb
rubocop-1.69.1 lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb
rubocop-1.69.0 lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb
rubocop-1.68.0 lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb
rubocop-1.67.0 lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb
rubocop-1.66.1 lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb