Sha256: 39b08fa5b633eb97e90cc988fb0ca0d80f1e2ba9ac77ce8c980dfc613ebb6c3c

Contents?: true

Size: 1.21 KB

Versions: 58

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Naming
      # 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

58 entries across 51 versions & 8 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.52.1/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.54.1/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.54.1/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.52.1/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
rubocop-1.54.1 lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
rubocop-1.54.0 lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
rubocop-1.53.1 lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
rubocop-1.53.0 lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.52.1/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.52.1/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb
rubocop-1.52.1 lib/rubocop/cop/naming/heredoc_delimiter_naming.rb