Sha256: 555d3f3c8de72604dd4c8ee8f53d74859b88dc59b4f5213f0100952cf2f00903

Contents?: true

Size: 1.22 KB

Versions: 75

Compression:

Stored size: 1.22 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
      # forbidden listing additional delimiters.
      #
      # @example
      #
      #   # good
      #   <<-SQL
      #     SELECT * FROM foo
      #   SQL
      #
      #   # bad
      #   <<-END
      #     SELECT * FROM foo
      #   END
      #
      #   # bad
      #   <<-EOS
      #     SELECT * FROM foo
      #   EOS
      class HeredocDelimiterNaming < Base
        include Heredoc

        MSG = 'Use meaningful heredoc delimiters.'

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

          add_offense(node.loc.heredoc_end)
        end

        private

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

          return false unless /\w/.match?(delimiters)

          forbidden_delimiters.none? do |forbidden_delimiter|
            Regexp.new(forbidden_delimiter).match?(delimiters)
          end
        end

        def forbidden_delimiters
          cop_config['ForbiddenDelimiters'] || []
        end
      end
    end
  end
end

Version data entries

75 entries across 75 versions & 8 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
rubocop-1.29.1 lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
rubocop-1.29.0 lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
rubocop-1.28.2 lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
rubocop-1.28.1 lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
rubocop-1.28.0 lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
rubocop-1.27.0 lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
rubocop-1.26.1 lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
rubocop-1.26.0 lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
rubocop-1.25.1 lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
rubocop-1.25.0 lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
rubocop-1.24.1 lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
rubocop-1.24.0 lib/rubocop/cop/naming/heredoc_delimiter_naming.rb