Sha256: 0a56b94a98f98f84945197e779c72941548bcd5ddd3c8a27e74f39d4cd04ad58

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # This cop checks for string literal concatenation at
      # the end of a line.
      #
      # @example
      #
      #   # bad
      #   some_str = 'ala' +
      #              'bala'
      #
      #   # good
      #   some_str = 'ala' \
      #              'bala'
      #
      class LineEndConcatenation < Cop
        MSG = 'Use \\ instead of + to concatenate those strings.'

        def on_send(node)
          add_offense(node, :selector) if offending_node?(node)
        end

        def autocorrect(node)
          @corrections << lambda do |corrector|
            # replace + with \
            corrector.replace(node.loc.selector, '\\')
          end
        end

        private

        def offending_node?(node)
          receiver, method, arg = *node

          # TODO: Report Emacs bug.
          return false unless :+ == method

          return false unless string_type?(receiver.type)

          return false unless string_type?(arg.type)

          plus_at_line_end?(node.loc.expression.source)
        end

        def plus_at_line_end?(expression)
          # check if the first line of the expression ends with a +
          expression =~ /.+\+\s*$/
        end

        def string_type?(node_type)
          [:str, :dstr].include?(node_type)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-0.19.1 lib/rubocop/cop/style/line_end_concatenation.rb
rubocop-0.19.0 lib/rubocop/cop/style/line_end_concatenation.rb