Sha256: f515d59cfc13fd5ad342b717891fbacd8b9937554b2e8522dde95d86a8713188

Contents?: true

Size: 1.32 KB

Versions: 9

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Naming
      # This cop checks that your heredocs are using meaningful delimiters.
      # By default it disallows `END` and `EO*`, and can be configured through
      # blacklisting additional delimiters.
      #
      # @example
      #
      #   # good
      #   <<-SQL
      #     SELECT * FROM foo
      #   SQL
      #
      #   # bad
      #   <<-END
      #     SELECT * FROM foo
      #   END
      #
      #   # bad
      #   <<-EOS
      #     SELECT * FROM foo
      #   EOS
      class HeredocDelimiterNaming < Cop
        include Heredoc

        MSG = 'Use meaningful heredoc delimiters.'.freeze

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

          add_offense(node, location: :heredoc_end)
        end

        private

        def meaningful_delimiters?(node)
          delimiters = delimiters(node)

          return false unless delimiters =~ /\w/

          blacklisted_delimiters.none? do |blacklisted_delimiter|
            delimiters =~ Regexp.new(blacklisted_delimiter)
          end
        end

        def delimiters(node)
          node.source.match(OPENING_DELIMITER).captures.first
        end

        def blacklisted_delimiters
          cop_config['Blacklist'] || []
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rubocop-0.57.1 lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
rubocop-0.57.0 lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
rubocop-0.56.0 lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
rubocop-0.55.0 lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
rubocop-0.54.0 lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
rubocop-0.53.0 lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
rubocop-0.52.1 lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
rubocop-0.52.0 lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
rubocop-0.51.0 lib/rubocop/cop/naming/heredoc_delimiter_naming.rb